使用指南针方向旋转 MapView
是否可以让嵌入的 MKMapView 旋转以始终面向 iPhone 所面向的方向? 基本上我想在我自己的应用程序上模仿地图应用程序旋转功能。
我发现 iPhone SDK 没有公开该功能。 但是,我想知道使用 CGAffineTransformMakeRotate 旋转整个视图是否有效。 会影响点击和缩放吗? 有没有更好的办法?
Is it possible to have an embedded MKMapView
rotate to always face the direction the iPhone is facing? Basically I want to mimic the Map app rotation feature on my own app.
I see that the iPhone SDK does not expose the functionality. However, I wonder if it would work to rotate the entire view using CGAffineTransformMakeRotate
. Would it affect tapping and zooming? Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你使用 navigationVontroller 试试这个:
if you use a navigationVontroller try this:
如果您查看
设备支持
下的 3.0 iPhone 应用程序编程指南,您将找到有关磁力计(又名指南针)的信息。 一旦开始获取标题信息(通过didUpdateHeading
方法),您应该能够获取指南针数据,然后使用它来计算正确的旋转变换值以应用于地图视图。不确定它是否可以处理缩放。 在标准地图应用程序中,我注意到一旦开始捏缩放,罗盘航向跟踪就会停止。
另外,请记住,位置方向以度为单位,而变换旋转角度以弧度为单位。
If you look in the 3.0 iPhone Application Programming Guide under
Device Support
you'll find information on the magnetometer (aka compass). Once you start getting heading information (through thedidUpdateHeading
method) you should be able to get the compass data then use that to calculate the proper rotation transform value to apply to the map view.Not sure if it handles zooming. In the standard map application I've noticed that compass heading tracking stops as soon as you start pinch-zooming.
Also, keep in mind that location directions are in degrees whereas transform rotation angles are in radians.
swift 3.0 中的简单解决方案。 确保将该行放在
mapViewDidFinishLoadingMap
中,否则它将忽略它。如果您不希望地图以用户位置为中心,您可以执行类似以下操作
Simple solution in swift 3.0. Make sure to put the line in
mapViewDidFinishLoadingMap
or it will ignore itIf you don't want the map to center on the user location you might do something like this
我可以确认它工作正常。 这是我正在使用的代码:
mapView
是我的 MKMapView 实例I can confirm that it works fine. Here's the code that I'm using:
mapView
is my MKMapView instance要旋转地图视图而不旋转注释,您可以使用以下代码来补偿地图旋转。
另一个解决方案是使用 iOS 5 中添加到 MKMapView 的新方法。
看一下: http://developer.apple.com/ Library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
- (void)setUserTrackingMode:(MKUserTrackingMode)模式动画:(BOOL)animated;
To rotate the mapView but not the annotations you could use the following code to compensate for the maps rotation.
Another sollution is using a new method that has been added in iOS 5 to MKMapView.
Take a look at: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;