我的应用程序包含一个MKMapView
将用户位置显示为蓝色项目符号的内容。
现在我做了一个按钮(就像在普通地图应用程序中一样),按下时应该将地图视图居中到用户位置,但我不知道该怎么做。
您可以将地图视图用户跟踪模式设置为 MKUserTrackingModeFollow。它会自动在用户位置上设置地图中心。
- (IBAction)centerMapOnUserButtonClicked:(id)sender {
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
[self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES];
我今天一直在解决这个问题。
可以将 MKUserTrackingBarButtonItem 按钮添加到工具栏以从 iOS 地图应用程序复制功能。按下按钮时,它将打开和关闭跟踪。
- (void)viewDidLoad
{
[super viewDidLoad];
MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.map];
self.navigationItem.rightBarButtonItem = buttonItem;
}
更完整的答案可在此处获得。
就是这个 :
locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];;
MKCoordinateRegion region;
region.center=coordinate;
MKCoordinateSpan span;
span.latitudeDelta=10.015; // Vary as you need the View for
span.longitudeDelta=10.015;
region.span=span;
[mapView setRegion:region];
self.mapView.showsUserLocation = YES;
斯威夫特 3 解决方案
@IBAction func centerUserButton(_ sender: Any) {
self.mapView.setCenter(self.mapView.userLocation.coordinate, animated: true)
}