【发布时间】:2011-11-23 07:49:39
【问题描述】:
我似乎与这里发布的许多关于MKMapView 的人有相反的问题。而不是不能让它放大并显示当前位置,我似乎无法让它 stop 这样做。以下是发生的事情:
- 我启动应用程序
- MKMapView 用蓝点显示我的位置
- 我用手指在地图上拉远拉远
- 几秒钟后,MKMapView 突然重新放大到我当前位置的中心
我尝试将CLLocationManager 告诉stopUpdatingLocation(没有效果,因为MKMapView 知道如何使用CoreLocation),我尝试将MKMapView 告诉setShowsUserLocation:NO(没有完全显示蓝点,这不是我想要的)。我什至尝试删除我的CLLocationManager(无效)。这是什么原因造成的,我该如何阻止它?
是的,我确实在-loadView 中设置了位置管理器的准确度和距离。
我没有实现-mapViewDidFinishLoadingMap:。这是我的实现
-locationManager:didUpdateToLocation:fromLocation:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// How many seconds ago was this new location created?
NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
// CLLocationManagers will return the last found location of the device first,
// you don't want that data in this case. If this location was made more than 3
// minutes ago, ignore it.
if (t < -180)
{
// This is cached data, you don't want it, keep looking
return;
}
[locationManager stopUpdatingLocation];
}
我认为这就是你要问的中心化问题,@Anna Karenina:
- (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)u
{
CLLocationCoordinate2D loc = [u coordinate];
// Display the region 500 meters square around the current location
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
[mv setRegion:region animated:YES];
}
【问题讨论】:
-
你在哪里做居中?在
mapView:didUpdateUserLocation:?你能发布那个代码吗? -
每次用户位置更改或获得新读数时,都会调用 didUpdateUserLocation 方法。您可以添加一个可以在方法中检查/设置的 bool ivar,以便只进行一次居中或您可以根据 u.location.horizontalAccuracy 或 u.location.timestamp 有条件地居中。
-
好的,这是有道理的,但是......我现在有 -mapView:didUpdateUserLocation: 如果 u.location.horizontalAccuracy 是 > locationManager.desiredAccuracy ,则有条件地居中并且它仍在发生。如果我反转条件,我不会得到初始缩放。
-
记录 HorizontalAccuracy 和 desiredAccuracy 值以查看您得到的结果并相应地设置条件。第一个用户位置可能需要几秒钟才能出现。
-
我不应该将 u.location.horizontalAccuracy 与 locationManager.desiredAccuracy 进行比较,因为它始终为 -1。现在我正在与 50.0 进行比较。初始显示很好,但我仍然遇到问题。
标签: iphone location mkmapview zooming