ios - didUpdateUserLocation 当 userLocation 的 View 是自定义时不调用

标签 ios geolocation mkmapview mapkit mkmapviewdelegate

我有一个 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/

相关文章:

iOS 配置文件名称

javascript - 如何将经度和纬度转换为街道地址

d3.js - 如何使用我的 geo json 文件以 3D 形式渲染城市

android - 浏览器地理定位 - Android 2.3.4 忽略 GPS 卫星。怎么修?

ios - 如何使用 JSONEncoder 对嵌套数组进行编码

ios - 使用 Xcode bot 构建时“没有处理文件的规则......对于体系结构 i386”

ios - 如果滚动 UITableView 时出现 popViewController,应用程序崩溃

ios - 是否可以根据 map View 的中心抓取城市等信息

ios - MKMapView 中的箭头图标

iphone - 控制iOS6中MKMapView的动画速度