Mapkit - 缩放级别保持重置

时间:2010-10-27 15:00:45

标签: iphone mkmapview

我有以下代码:

-(void)viewDidLoad 
{
    //Set Zoom level using Span
    MKCoordinateSpan span;
    span.latitudeDelta = 0.05;
    span.longitudeDelta = 0.05;
    region.span = span;
}


-(void)locationChange:(CLLocation *)newLocation: (CLLocation *)oldLocation
{
    // This zooms in on the users current loation.
    curlocation = newLocation.coordinate;
    region.center = curlocation;

    [_mapView setRegion:region animated:TRUE];
}

最初根据ViewDidLoad中的代码设置缩放级别。如何存储用户放大或缩小的缩放级别ID,因为每次收到新的位置更新时,缩放级别都会重置。

有没有办法检测用户是否已放大或缩小?

更新

我添加了regionDidChangeAnimated方法,如下所示:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    NSLog(@"Region DID change.   Center is now %f,%f,  Deltas=%f,%f", 
          region.center.latitude, region.center.longitude,  
          region.span.latitudeDelta, region.span.longitudeDelta);
}

日志中的输出如下:

2010-11-01 15:17:29.317 Legginit [2948:307]地区DID更改 中心现在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:30.553 Legginit [2948:307]地区DID更改 中心现在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:31.063 Legginit [2948:307]地区DID更改 中心现在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:31.653 Legginit [2948:307]地区DID更改 中心现在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:32.582 Legginit [2948:307]地区DID更改 中心现在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:33.608 Legginit [2948:307]地区DID更改 中心现在是54.181150,-8.483177,Deltas = 0.050000,0.050000

当我放大手机时,我期待Delta值发生变化,但仍保持在0.05。我误解了这是如何工作的。我以为我可以捕获Delta值并存储它们,这样我就可以在用户退出并重新进入地图时重置缩放级别。

此致 斯蒂芬

2 个答案:

答案 0 :(得分:2)

在locationChange中,尝试在调用setRegion之前将区域的跨度更新为_mapView的当前跨度:

region.span = _mapView.region.span;

如果您确实需要检测缩放变化,请实现MKMapViewDelegate方法regionWillChangeAnimated或regionDidChangeAnimated。

修改
您需要从mapView获取新区域并将区域实例变量设置为它(地图视图不会自动设置您的实例变量):

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    region = mapView.region;  //<-- add this

    NSLog(@"Region DID change.   Center is now %f,%f,  Deltas=%f,%f",
          region.center.latitude, region.center.longitude,  
          region.span.latitudeDelta, region.span.longitudeDelta);
}

答案 1 :(得分:0)

不要使用区域,只需更改中心坐标即可。我发现设置区域本身可以稍微改变缩放级别,即使告诉mapView使用与当前mapView相同的区域。

[_mapView setCenterCoordinate:newLocation.coordinate animated:YES];