我找到了一个解决方案:
我使用可用的航向信息旋转地图
[mapView setTransform:CGAffineTransformMakeRotation(heading.magneticHeading * M_PI / -180.0)];
因此,“光束”始终指向设备的顶部。我现在只是在地图顶部显示一个 ImageView 并更改它在 locationManager:didUpdateToLocation:fromLocation 中的位置:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// scroll to new location
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
[self.mapView setRegion:region animated:YES];
// set position of "beam" to position of blue dot
self.headingAngleView.center = [self.mapView convertCoordinate:newLocation.coordinate toPointToView:self.view];
// slightly adjust position of beam
self.headingAngleView.frameTop -= self.headingAngleView.frameHeight/2 + 8;
}
其中 frameTop 和 frameHeight 是 frame.origin.y 和 frame.size.height 的快捷方式。当点改变它的位置时,它并不理想,有时会缺少一点点,但我对它的解决方案 b/c 很满意。
看看我的 OpenSource 框架 MTLocation,它可以完成这一切(以及许多其他与地图相关的酷东西):
MTLocation