用户跟踪模式为MKUserTrackingModeFollowWithHeading时,MKMapView无法正确缩放 [英] MKMapView doesn't zoom correctly while user tracking mode is MKUserTrackingModeFollowWithHeading

查看:439
本文介绍了用户跟踪模式为MKUserTrackingModeFollowWithHeading时,MKMapView无法正确缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用几行代码和两个组件创建了一个测试项目: MKMapView UIButton 。我勾选 mapView 选项 - 显示用户位置。我还为按钮定义了一个动作,它将地图缩放到用户位置。

I created a test project with few lines of code and with two components: MKMapView and UIButton. I ticked mapView option - Shows user location. Also I defined an action for the button, it zooms the map to user location.

以下是来自控制器的代码:

Here is code from controller:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
    self.mapView.delegate = self;
}

- (IBAction)changeRegion:(id)sender {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 200.0f, 200.0f);
    [self.mapView setRegion:region animated:YES];
}

非常简单明了,不是吗?但是当我点击按钮时,我看到了奇怪的行为:地图视图缩放到指定区域然后返回到原始缩放。有什么问题?如何同时保持缩放和跟踪用户位置?

Pretty simple and straightforward, isn't it? But when I tap the button I see weird behaviour: map view zooms to specified region then returns back to original zoom. What's the problem? How can I keep zooming and track user location at the same time?

我注意到与MKUserTrackingModeFollow跟踪模式类似的行为。

I notice similar behaviour with MKUserTrackingModeFollow tracking mode.

P.S。我忘了提到它主要是针对iOS7的问题

P.S. I forgot to mention that it's a problem mostly for iOS7

推荐答案

来自apple文档:


将跟踪模式设置为MKUserTrackingModeFollow或
MKUserTrackingModeFollowWithHeading使地图视图将
地图置于该位置的中心,并开始跟踪用户的位置。如果
地图缩小,地图视图会自动放大用户的
位置,有效地更改当前可见区域。

Setting the tracking mode to MKUserTrackingModeFollow or MKUserTrackingModeFollowWithHeading causes the map view to center the map on that location and begin tracking the user’s location. If the map is zoomed out, the map view automatically zooms in on the user’s location, effectively changing the current visible region.

如果您想同时调整区域并跟踪用户,我建议您检查位置更新并相应地调整缩放。

If you want both to adjust the region and to track the user, I suggest you check for location updates and adjust zoom accordingly.

例如:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 200.0f, 200.0f);
    [self.mapView setRegion:region animated:YES];
}

编辑

不要设置区域,只需设置中心,

Instead of setting the region, try just setting the center,

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

让你的按钮操作设置缩放,保持相同的中心:

and let your button action set the zoom, keeping the same center:

- (IBAction)changeRegion:(id)sender {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.centerCoordinate, 200.0f, 200.0f);
    [self.mapView setRegion:region animated:YES];
}

非常重要:不要将mapView设置为跟踪用户。禁用跟踪用户,因为现在您正在自己跟踪它。我认为默认值是 MKUserTrackingModeNone

And very important: do not set your mapView to track user. Disable tracking user because now you are tracking it yourself. I think the default is MKUserTrackingModeNone .

这篇关于用户跟踪模式为MKUserTrackingModeFollowWithHeading时,MKMapView无法正确缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆