使用标题转换用户位置注释 [英] Turning user location annotation with heading
问题描述
我正在尝试更改我的应用中的用户注释,以便它显示通常的蓝点,但是有一个三角形从它上面显示用户面向哪个方向(我宁愿旋转用户注释而不是整个地图,这是MKUserTrackingModeFollowWithHeading所做的事情)。我有一个基本的版本,但它有一些奇怪的行为。
I'm trying to change the User Annotation in my app so that it shows the usual blue dot, but with a triangle coming off of it to show which direction the user is facing (I'd rather rotate the user annotation than the entire map, which is what MKUserTrackingModeFollowWithHeading does). I've got a rudimentary version working, but it has some weird behavior.
首先,一些代码:
-(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];
}
}
}
用户位置注释图像显示正常,并根据标题旋转,但这是有趣的行为:
The user location annotation image shows up fine, and rotates based on the heading, but here are the funny behaviors:
1-注释不符合我的位置 - - 它只停留在一个地方,但以正确的标题旋转。如果我使用GPS按钮关闭GPS,然后将其重新打开,注释会显示在正确的位置,但在我走路时仍然不会跟随。
1- The annotation does not follow my location-- it only stays in one place, but rotates with the correct heading. If I turn off the GPS with the "GPS Button", then turn it back on, the annotation shows up in the correct place, but still won't follow as I walk.
2-如果我滚动地图,注释会弹回到正北方,然后快速旋转到正确的标题,导致恼人的闪烁效果。
2- If I scroll the map, the annotation pops back to due north, then quickly rotates to the correct heading, causing an annoying flickering effect.
3-如果我关闭GPS并删除用户位置,注释将按预期删除,但如果我滚动地图,注释会弹回屏幕而不是隐藏。
3- If I turn off the GPS and remove the user location, the annotation is removed as intended, but if I then scroll the map, the annotation pops back to the screen rather than staying hidden.
我做错了什么?感谢任何有用的提示!
What am I doing wrong? Thanks for any helpful hints!
推荐答案
即使很可能你已经修复了你的问题,我仍然希望分享解决方案,它帮助我解决了类似的问题。
Even if it is likely that you already fixed your 2. problem I still want to share the solution, which helped me with a similar problem.
我遇到了同样的问题:当用户滚动或平移时,我的自定义用户位置箭头旋转总是弹回北方地图。我在这里找到了解决方法: https://stackoverflow.com/a/12753320/2030629
I came across same problem: my custom user location arrow rotation was always popping back to north when the user scrolled or panned the map. I found the fix here: https://stackoverflow.com/a/12753320/2030629
根据作者的说法,它是一个但在ios6中可以通过添加
according to the author it is a but in ios6 and can be fixed by adding
if(is6orMore) {
[annView setTransform:CGAffineTransformMakeRotation(.001)]; //iOS6 BUG WORKAROUND !!!!!!!
}
到mapView:viewForAnnotation。
to mapView:viewForAnnotation.
我还在 - mapView:regionDidChangeAnimated:中再次设置了变换。
希望这会让别人感到高兴,因为它让我:)
I also set the transform in - mapView:regionDidChangeAnimated: again. Hope this will make someone else as happy as it made me :)
这篇关于使用标题转换用户位置注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!