4

我正在尝试更改我的应用程序中的用户注释,以便它显示通常的蓝点,但是从它上面出现一个三角形以显示用户面对的方向(我宁愿旋转用户注释而不是整个地图,这就是 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 并删除用户位置,注释会按预期删除,但如果我随后滚动地图,注释会弹回屏幕而不是保持隐藏状态。

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

4

1 回答 1

2

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

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

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

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

映射视图:viewForAnnotation。

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

于 2013-02-26T10:30:17.747 回答