我有一个 MKMapView
,它应该使用自定义 View (不是蓝点)跟踪用户的位置。为了用这个 View 代替蓝点,我这样返回:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (annotation == [mapView userLocation])
{
return userLocationView;
}
}
为了初始化跟踪,我调用
[mapView setShowsUserLocation: YES];
[mapView setUserTrackingMode: MKUserTrackingModeFollow animated: NO];
[mapView setDelegate: self];
正如预期的那样,-mapView:didUpdateUserLocation:
在应用加载时被调用一次。不幸的是,除非我将 -mapView:viewForAnnotation:
更改为具有以下实现,否则它再也不会被调用:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (annotation == [mapView userLocation])
{
return nil;
}
}
通过这些更改, map 加载蓝点作为用户位置的指示符,并且 -mapView:didUpdateUserLocation:
会像预期的那样被频繁调用。
在跟踪用户位置和自定义用户位置 View 方面是否存在某种互斥性?我怎样才能让两者都发生?
来源
这个项目演示了这个问题。 https://dl.dropbox.com/u/2338382/MapKitFuckery.zip
错误
这很可能是一个错误,我已将其作为雷达归档。在此期间,接受的答案应该足够了。然而,值得一提的是,我不得不完全放弃 [mapView userLocation]
和 [mapView showsUserLocation]
,转而使用简单的自定义注释和 CLLocationManager
。
最佳答案
与其依赖 map View 的位置更新,不如启动一个 CLLocationManager
, 设置它的 delegate
并等待 -locationManager:didUpdateToLocation:fromLocation:
(在 iOS 5 及更低版本中)或 -locationManager:didUpdateLocations:
(iOS 6)。与使用 map View 的委托(delegate)方法相比,您将获得更加可靠和丰富的信息。您可能知道执行此操作的方法,但它是:
#import <CoreLocation/CoreLocation.h>
- (void)viewWillAppear:(BOOL)animated
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager startUpdatingLocation];
}
// Deprecated in iOS 6
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Check the age of the newLocation isn't too long ago using newLocation.timestamp
// Set the map dot using newLocation.coordinate
// Set an MKCircle to represent accuracy using newLocation.horizontalAccuracy
}
我查看了进入 mapView 委托(delegate)的委托(delegate)调用,并返回了除 nil
以外的任何内容。停止调用 -mapView:didUpdateUserLocation:
, 像你说的。以下是按到达顺序排列的电话:
- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
大概是 MKUserLocation
对象,而不是 MKMapView
是负责通过更新调用调用委托(delegate)的对象。如果您检查 showsUserLocation
的状态和 mapView.userLocation
,它们看起来都不错:
NSLog(@"%d %@", mapView.showsUserLocation, mapView.userLocation);
返回 1
和一个非零对象( 1 <MKUserLocation: 0x1e02e580>
)。也许是 mapView
查询其 userLocation
对象获取当前位置,然后将其发送给委托(delegate)。如果该对象已经消失,它将无法工作。
这有点奇怪,但正如我所说,您会从 CLLocationManager
获得更好的更新的更新。
关于ios - didUpdateUserLocation 当 userLocation 的 View 是自定义时不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13333339/