如何在 Mapkit MapView 上显示指南针/航向
在 iPhone 3GS 上的“地图”应用程序中,您可以单击该图标,该图标通常会显示您的位置两次,蓝点会获得看起来像头灯的光束,基本上会向您显示您在地图上面对的方向并旋转图像因此。
使用 MapKit MapView 可以使用此选项吗?
我知道我可以通过类似
- (void)locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *) newHeading {
// 如果准确性有效,则处理事件来获取标题。
if (newHeading.headingAccuracy > 0) {
CLLocationDirection theHeading = newHeading.magneticHeading;
...
}
但
我不知道如何在 Mapkit 中获得漂亮的头灯效果,而且似乎没有任何文档。
有什么想法吗?
on the iPhone 3GS in the "Maps" app you can click the icon which usually shows your position twice and the blue dot gains what looks like a beam from a headlamp, basically showing you the direction you are facing on the map and rotating the image accordingly.
Is this option available using MapKit MapView ?
I'm know that I can get my heading with something like
- (void)locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *) newHeading {
// If the accuracy is valid, process the event.
if (newHeading.headingAccuracy > 0) {
CLLocationDirection theHeading = newHeading.magneticHeading;
...
}
}
but I don't know how to get that nice headlamp effect in Mapkit and there doesn't seem to be any documentation.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
添加用户跟踪模式也有帮助。我知道我迟到了,但可能对像我这样的其他开发人员有帮助:)
Adding user tracking mode also helps. I know I am late, but possibly a help to other developers like me :)
我找到了一个解决方案:
我使用可用的标题信息旋转地图,
因此“光束”始终指向设备的顶部。我现在只是在地图顶部显示一个 ImageView 并更改它在 locationManager:didUpdateToLocation:fromLocation: 中的位置:
其中,frameTop 和frameHeight 是frame.origin.y 和frame.size.height 的快捷方式。
它并不理想,有时当点改变其位置时会缺少一点,但我对解决方案很满意,因为它有效。
看看我的开源框架 MTLocation,它完成了这一切(以及为您提供的许多其他很酷的地图相关内容):
MT位置
I found a solution:
I rotate the map using the available heading-information with
Therefore the "beam" always points to the top of the device. I now just display an ImageView on top of the map and change it's position in locationManager:didUpdateToLocation:fromLocation:
Whereby frameTop and frameHeight are shortcuts for frame.origin.y and frame.size.height.
It is not ideal and sometimes lacks a little bit when the dot changes it's position, but I'm happy with the solution b/c it works.
Have a look at my OpenSource framework MTLocation which does this all (and a lot of other cool Map-related stuff for you):
MTLocation
其他答案的轮换逻辑很好,但是依赖位置管理器委托方法不会产生好的解决方案。 “光束”图像很少会与蓝点位于同一位置,并且会多次反弹。
最好的解决方案是获取对蓝点视图的引用,并将“光束”图像作为子视图添加到其中。我选择了一个正方形的光束图像,光束在顶部,周围都是透明的。想象一下蓝点位于图像的中心。
The rotating logic of the other answers are good, however relying on the location manager delegate methods won't result in a good solution. The "beam" image will rarely be in the same place as the blue dot and will bounce around a lot.
The best solution is to get a reference to the blue dot view, and add the "beam" image as a subview to it. I went with a beam image that was a square, with the beam up top and transparency all around it. Imagine the blue dot being in the center of the image.
我可以想到一种方法,虽然我没有实施,但可能对你有一点帮助。
didUpdateHeading
中,使用 x 和 y 值来计算方向的角度。请发布您对上述方法的建议/更改。
I could think of one method, though I have not implemented but may help you a bit.
didUpdateHeading
use the x and y values to calculate the angle of the direction.Please post your suggestions/changes for the above approach.
Swift 3 实施
Swift 3 implementation