Mapkit - 缩放级别不断重置

发布于 2024-09-29 18:35:23 字数 1812 浏览 0 评论 0原文

我有以下代码:

-(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] Region DID change。
中心现在为 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:30.553 Legginit[2948:307] 区域确实发生了变化。
中心现在是 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:31.063 Legginit[2948:307] 区域确实发生了变化。
中心现在是 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:31.653 Legginit[2948:307] 区域确实发生了变化。
中心现在为 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:32.582 Legginit[2948:307] 区域确实发生了变化。
中心现在是 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:33.608 Legginit[2948:307] 区域确实发生了变化。
中心现在为 54.181150,-8.483177,Deltas=0.050000,0.050000

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

问候, 斯蒂芬

I have the following code in place:

-(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];
}

Initially the zoom level is set as per the code in ViewDidLoad. How do I store the zoom level id the user zooms in or out, as everytime a new location update is received the zoom level is reset.

Is there a way of detecting the user has zoomed in or out ?

UPDATE

I've add the regionDidChangeAnimated method as follows:

- (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);
}

Output in the log looks like:

2010-11-01 15:17:29.317 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:30.553 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:31.063 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:31.653 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:32.582 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000

2010-11-01 15:17:33.608 Legginit[2948:307] Region DID change.
Center is now 54.181150,-8.483177, Deltas=0.050000,0.050000

As I zoom in on the phone I was expecting the Delta Values to change but they remain at 0.05. Am I misunderstanding how this works. I thought I could capture the Delta values and store them so I could reset the zoom level if the user exits and re-enters the map.

Regards,
Stephen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找我们的幸福 2024-10-06 18:35:23

在 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);
}

In locationChange, try updating your region's span to the _mapView's current span before calling setRegion:

region.span = _mapView.region.span;

If you really need to detect zoom changes as they happen, implement the MKMapViewDelegate method regionWillChangeAnimated or regionDidChangeAnimated.

Edit:
You need to get the new region from the mapView and set your region instance variable to it (the map view won't set your instance variable automatically):

- (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);
}
不一样的天空 2024-10-06 18:35:23

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

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

Instead of using the region, just change the center coordinate. I've found that setting the region itself can change the zoom level slightly, even when telling the mapView to use the same region as the current mapView.

[_mapView setCenterCoordinate:newLocation.coordinate animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文