3

我正在使用地图。我有个问题。我使用以下代码从 stackOverFlow 中此链接的引用中缩放

缩放地图很容易。
但是现在,我无法放大和缩小地图。这意味着我不能改变或找到另一个地方。它只关注当前位置。它的行为就像图像修复一样。我不明白该怎么办?请帮忙。我的代码如下。

- (void) viewDidLoad
{
[self.mapView.userLocation addObserver:self 
                            forKeyPath:@"location" 
                               options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
                               context:nil];
}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;  

MKCoordinateSpan span; 
span.latitudeDelta  = 1; // Change these values to change the zoom
span.longitudeDelta = 1; 
region.span = span;

[self.mapView setRegion:region animated:YES];
}
4

1 回答 1

2

我认为问题在于您正在收听用户位置更改(很可能每秒发生多次)并且您正在将地图区域设置为该区域。

您需要做的是在地图上添加一个按钮(如 Apple 地图的左上角),它将地图模式切换为自由外观或固定到用户位置。

当用户按下按钮时,您可以删除/添加 KVO。或在您的代码中切换布尔标志。当标志为真时,您不会更改地图区域。就像是:

@implementation YourController{
    BOOL _followUserLocation;
}

- (IBAction) toggleMapMode:(id)sender{
    _followUserLocation = !_followUserLocation;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary             *)change context:(void *)context{
   if(_followUserLocation){
        MKCoordinateRegion region;
        region.center = self.mapView.userLocation.coordinate;  

        MKCoordinateSpan span; 
        // retain the span so when the map is locked into user location they can still zoom
        span.latitudeDelta  = self.mapView.region.span.latitudeDelta;
        span.longitudeDelta = self.mapView.region.span.longitudeDelta; 

        region.span = span;

        [self.mapView setRegion:region animated:YES];
    }
}

@end

也许您不想要这一切,而您所需要的只是:

        // retain the span so when the map is locked into user location they can still zoom
        span.latitudeDelta  = self.mapView.region.span.latitudeDelta;
        span.longitudeDelta = self.mapView.region.span.longitudeDelta; 
于 2013-08-30T13:30:17.703 回答