2

我的应用程序包含一个MKMapView将用户位置显示为蓝色项目符号的内容。

现在我做了一个按钮(就像在普通地图应用程序中一样),按下时应该将地图视图居中到用户位置,但我不知道该怎么做。

4

5 回答 5

10

您可以将地图视图用户跟踪模式设置为 MKUserTrackingModeFollow。它会自动在用户位置上设置地图中心。

- (IBAction)centerMapOnUserButtonClicked:(id)sender {
    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; 
}
于 2013-10-26T12:29:33.630 回答
7
[self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES];
于 2013-10-26T15:10:18.030 回答
3

我今天一直在解决这个问题。

可以将 MKUserTrackingBarButtonItem 按钮添加到工具栏以从 iOS 地图应用程序复制功能。按下按钮时,它将打开和关闭跟踪。

- (void)viewDidLoad
{
    [super viewDidLoad];    
    MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.map];
    self.navigationItem.rightBarButtonItem = buttonItem;
}

更完整的答案可在此处获得。

于 2014-03-12T11:31:10.490 回答
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;
于 2013-10-26T12:25:39.847 回答
0

斯威夫特 3 解决方案

@IBAction func centerUserButton(_ sender: Any) {
    self.mapView.setCenter(self.mapView.userLocation.coordinate, animated: true)
}
于 2017-01-23T06:20:06.643 回答