如何放大和居中用户位置 (Objective-C)

How to zoom in and center the user location (Objective-C)

如何放大和居中用户位置 "dot"?我已经尝试了在这里和其他网站上找到的所有内容,但我似乎无法正确处理。

请帮帮我!

编辑:我试图在 viewDidAppear 中实现这行代码。我在某处的旧线程中找到它,但它似乎不再起作用了...

[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];

您是否尝试过为地图设置区域,我列出了所有步骤以防您遗漏任何内容,让我知道您遗漏了哪些步骤,我会提供代码。

viewDidLoadviewDidAppear 中,以适合您的流程为准。一旦地图初始化并监听位置更新,您必须启动地图并挂钩更新位置 didUpdateUserLocation 您需要像这样设置区域

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //Mapview is your map on display,      
    CLLocationCoordinate2D coord = self.mapView.userLocation.coordinate;

    //Create a region with a 1000x1000 meter around the user location
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coord, 1000, 1000);

    //Set the region of your mapview so the user location is in center and update
    [self.mapView setRegion:viewRegion];

}

我以编程方式创建了地图,而不是使用 MapKit 视图,它运行得非常好!

代码如下:

    [super viewDidLoad];

self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.mapView setShowsUserLocation:YES];
[self.mapView setDelegate:self];
[self.view addSubview:self.mapView];
[mapView setMapType:MKMapTypeHybrid];

}

#pragma mark - MKMapViewDelegate Methods

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

[self.mapView setRegion:MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1))animated:YES];

}