【发布时间】: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