ios - 使用标题转动用户位置注释

标签 ios mapkit cllocationmanager mkannotationview

我正在尝试更改我的应用程序中的用户注释,以便它显示通常的蓝点,但有一个三角形从它上面脱落以显示用户面对的方向(我宁愿旋转用户注释而不是整个 map ,这是 MKUserTrackingModeFollowWithHeading 所做的)。我有一个基本版本可以工作,但它有一些奇怪的行为。

首先,一些代码:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
     if ([annotation isKindOfClass:[MKUserLocation class]]) {
          _userLocationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"userLocationIdentifier"];
          //use a custom image for the user annotation
          _userLocationView.image = [UIImage imageNamed:@"userLocationCompass.png"];

          return _userLocationView;

     } else {
          return nil;
     }

}

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

     //rotate user location annotation based on heading from location manager.
     if (!_locatorButton.hidden) {
          CLLocationDirection direction = newHeading.magneticHeading;

          CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadians(direction));
          _userLocationView.transform = transform;
     }

}

-(void)GPSButtonPressed:(id)sender {
    if (self.GPSEnabled) {
        //if GPS is already on, disable it.
        _mapview.showsUserLocation = NO;
        [_mapview removeAnnotation:_mapview.userLocation];
        self.GPSEnabled = NO;

        [_locationManager stopUpdatingHeading];

    } else {
        //enable GPS.
        _mapview.showsUserLocation = YES;
        self.GPSEnabled = YES;

        if ([CLLocationManager headingAvailable]) {
            [_locationManager startUpdatingHeading];
        }


    }
}

用户位置注释图像显示正常,并根据标题旋转,但这里有一些有趣的行为:

1- 注释没有跟随我的位置——它只停留在一个地方,但会以正确的航向旋转。如果我使用“GPS 按钮”关闭 GPS,然后重新打开,注释会显示在正确的位置,但我走路时仍然无法跟随。

2- 如果我滚动 map ,注释会弹回正北,然后快速旋转到正确的航向,造成令人讨厌的闪烁效果。

3- 如果我关闭 GPS 并删除用户位置,注释会按预期删除,但如果我随后滚动 map ,注释会弹出回到屏幕上,而不是保持隐藏状态。

我做错了什么?感谢您提供任何有用的提示!

最佳答案

即使您可能已经解决了 2. 问题,我仍然想分享解决方案,它帮助我解决了类似的问题。

我遇到了同样的问题:当用户滚动或平移 map 时,我的自定义用户位置箭头旋转总是弹出回到北方。我在这里找到了修复:https://stackoverflow.com/a/12753320/2030629

根据作者的说法,它是一个但在 ios6 中,可以通过添加来修复

if(is6orMore) {
        [annView setTransform:CGAffineTransformMakeRotation(.001)]; //iOS6 BUG WORKAROUND !!!!!!!
 }

到 mapView:viewForAnnotation。

我还再次在 - mapView:regionDidChangeAnimated: 中设置了变换。 希望这会让其他人和我一样快乐:)

关于ios - 使用标题转动用户位置注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14158812/

相关文章:

iphone - TableView 单元格中 TextView 的可变高度

ios - Firebase/Swift - 从 Firebase 输出数据出错

android - Cordova 的 device.uuid 值可以用于发送 Firebase 推送通知吗?

ios - 缩小以显示用户位置/注释

objective-c - horizo​​ntalAccuracy 究竟是什么意思?

ios - 如何构建具有不同外观/品牌的不同版本的应用程序?

ios - 如何将来自 MapKit 注释的信息发送到新的 View Controller

ios - 我怎样才能让我的图钉落在带有动画的 MKMapView 上(并且可能 - 比平时慢)

ios - 从 AppDelegate 和 ViewController 访问 locaitonManager

ios - 如何将 PFGeoPoint 转换为 CLLocation