我的应用包含MKMapView
,可将用户位置显示为蓝色子弹。
现在我创建了一个按钮(就像在普通的地图应用程序中一样)按下时应该将地图视图居中到用户位置,但我不知道该怎么做。
答案 0 :(得分:10)
您可以将地图视图用户跟踪模式设置为MKUserTrackingModeFollow。它会自动在用户位置设置地图中心。
- (IBAction)centerMapOnUserButtonClicked:(id)sender {
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
答案 1 :(得分:7)
[self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES];
答案 2 :(得分:3)
我今天刚刚解决这个问题。
可以将MKUserTrackingBarButtonItem按钮添加到工具栏以从iOS Maps应用程序复制功能。按下按钮时,它将打开和关闭跟踪。
- (void)viewDidLoad
{
[super viewDidLoad];
MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.map];
self.navigationItem.rightBarButtonItem = buttonItem;
}
可以获得更全面的答案here。
答案 3 :(得分:0)
就是这样:
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;
答案 4 :(得分:0)
Swift 3解决方案
@IBAction func centerUserButton(_ sender: Any) {
self.mapView.setCenter(self.mapView.userLocation.coordinate, animated: true)
}