【问题标题】:Turning user location annotation with heading用标题转动用户位置注释
【发布时间】:2013-01-04 14:16:31
【问题描述】:

我正在尝试更改我的应用程序中的用户注释,以便它显示通常的蓝点,但它会出现一个三角形以显示用户面对的方向(我宁愿旋转用户注释而不是整个地图,这就是 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- 如果我滚动地图,注释会弹回正北,然后快速旋转到正确的航向,导致令人讨厌的闪烁效果。

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

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

【问题讨论】:

  • 关于行为 1,请参阅 stackoverflow.com/questions/11432746/…。关于行为 3:在GPSButtonPressed 中,我不建议在地图视图的 userLocation 上调用 removeAnnotation(只需将 showUserLocation 设置为 NO)。另见stackoverflow.com/questions/8555903/…
  • 谢谢,安娜卡列尼娜!这是一个巨大的帮助——我正在使用 CLLocationManager 而不是 MapView 的 showUserLocation,所以注释现在随着我移动并正确旋转。 2 号行为仍然存在,但这仍然比今天早上早些时候要好:)
  • 您找到解决问题 2 的方法了吗?我正在使用自定义 userLocation 图标并手动旋转地图而不是使用 followWithHeading。与此类似:stackoverflow.com/questions/7036246/… 似乎当地图区域更改时,所有注释都会闪烁到原始旋转,直到下一个标题更改来自位置管理器。我已经尝试缓存最后一个旋转值并在 regionWillChange 和 regionDidChange 中重置它,但没有奏效。
  • 是的,到目前为止仍然没有运气。不过,我把问题放在了次要位置,并没有过多关注。不过,一位使用较新 iPhone 的朋友告诉我,当我将应用程序发送给他进行测试时,他没有发现问题。我想知道这是不是……
  • 哦,有趣。谢谢。

标签: ios mapkit cllocationmanager mkannotationview


【解决方案1】:

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

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

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

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

到 mapView:viewForAnnotation。

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

【讨论】:

    猜你喜欢
    • 2016-09-02
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多