17

我正在使用一个MKUserTrackingBarButtonItem按钮来允许用户自动跟踪他们在地图上的位置。问题是当他们点击这个按钮时,它被放大得太远了。我希望它以指定的缩放级别(即跨度)开始。我怎样才能做到这一点?

当用户点击按钮更改为 时MKUserTrackingModeFollow,它似乎使用了与用户上次手动更改为相同的缩放级别(即使用地图上的手势)。尝试通过setRegion或指定不同的缩放级别setVisibleMapRect不会影响模式更改为 时将使用的缩放级别MKUserTrackingModeFollow

尝试override mapView:didChangeUserTrackingMode:设置区域会导致模式更改回MKUserTrackingModeNone。例子:

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
    if (mode == MKUserTrackingModeFollow) {
        CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
        MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
        [mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
        // [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO];
    }
}

如果我在设置区域后立即尝试重置模式,如果用户静止,它会正常工作,但如果用户移动,它会缩小。

最简单的解决方案是,如果有一种方法可以通过发送我的跨度值来简单地为 MKUserTraking 指定缩放级别。但是,既然那似乎不存在,我还能做什么?

4

2 回答 2

10

我遇到了同样的问题,并使用了不同的方法来解决它。您可以为此使用 MapCamera 功能而不是该按钮。

在每个新位置执行以下操作:

 MKMapCamera *newCamera = [MKMapCamera cameraLookingAtCenterCoordinate:[newLocation coordinate]
 fromEyeCoordinate:[oldLocation coordinate]
 eyeAltitude:2000];

 [mapView setCamera:newCamera animated:TRUE];

和眼睛一起玩Altitude。

如果用户手动放大或缩小,您可以从 mapview.camera.altitude 读取高度值,当用户手动使用地图时,也不会更新相机。

于 2014-11-13T14:10:52.920 回答
0

根据此处使用的苹果文档

https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode

将跟踪模式设置为跟随或跟随标题会导致地图视图以该位置为中心,并开始跟踪用户的位置。如果地图被缩小,地图视图会自动放大用户的位置,有效地改变当前的可见区域。

由于这个原因,在这里更改区域不会影响您的可见区域。

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
    CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
    [mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
    // [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO];
 }
}

所以你只需要改变中心坐标didChangeUserTrackingMode而不是改变整个区域

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
   [self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
   }
 }

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
  [self.mapView setCenterCoordinate:mapViewuserLocation.location.coordinate animated:YES];
}

单击 MKUserTrackingBarButtonItem 更改缩放级别

 CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
[mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
于 2017-03-29T05:26:53.180 回答