MKMapView 和 CLLocationManager
我想使用 MKMapView 使用默认的呼吸蓝色图钉来显示用户当前位置,并且我想同时记录用户移动。当我们启用 MKMapView 来显示用户位置时,有什么方法可以使用 MKMapView 使用的 GPS 管理器(不确定这是否是 CLLocationManager)?
我知道我可以创建自己的 CLLocationManager。但这感觉就像给我的应用程序增加了开销,我希望地图和我的跟踪保持同步。
我已经探索了以下想法,但没有成功:
- 使用
[MKMapView showUserLocation:YES]
并在userLocation
字段上添加 KVO。这不起作用,我想知道这是否是由于userLocation
字段是只读的。 - 使用
[MKMapView showUserLocation:YES]
,创建一个MKMapViewDelegate
,并在请求用户位置的注释视图时添加跟踪。这不起作用,因为注释视图显然只请求一次??? - 使用 CLLocationManager 并尝试手动添加蓝色图钉。不幸的是,我没有在可用的引脚类型中找到蓝色引脚,因此我尝试手动创建用户注释,但没有成功。
有谁知道我如何实现这一目标并仍然受益于蓝色图钉,或者是我使用 CLLocationManager 并创建自己的图钉的唯一解决方案?
I want to use a MKMapView to display the user current location using the default breathing blue pin and I want to record the user movement at the same time. Is there any way that I could use the GPS manager (not sure if this is a CLLocationManager) the MKMapView uses when we enabled it to show user location?
I know that I can create my own CLLocationManager. But this feels like adding an overhead to my application and I would like the map and my tracking to remain in sync.
I already explored the following ideas without success:
- Use the
[MKMapView showUserLocation:YES]
and add KVO on theuserLocation
field. This does not work and I am wondering if this is due to the fact that theuserLocation
field is read only. - Use the
[MKMapView showUserLocation:YES]
, create aMKMapViewDelegate
and add the tracking when the annotation view for the user location is requested. This does not work, because the annotation view is apparently requested only once??? - Use a
CLLocationManager
and try to add the blue pin manually. Unfortunately, I did not find the blue pin in the available pin types, so I tried to create a user annotation manually without success.
Does anyone has any idea how I can achieve this and still benefit from the blue pin or is my only solution to use a CLLocationManager
and create my own pin?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CLLocationManager
在其所有实例中使用相同的数据。MKMapView
在内部使用CLLocationManager
数据。也就是说,完成您想做的事情的解决方案是让MKMapView
就showUserLocation:
做自己的事情。同时,创建 CLLocationManager 及其委托的实例。委托消息将为您提供
MKMapView
蓝色图钉的 GPS 坐标位置。一切都将彼此同步。CLLocationManager
uses the same data across all of its instances.MKMapView
usesCLLocationManager's
data internally. That said the solution to do what you want to do is letMKMapView
do its own thing with regards toshowUserLocation:
. At the same time, create an instance ofCLLocationManager
and its delegate.The delegate messages will give you the GPS coordinate location of
MKMapView's
blue pin. Everything will be in sync with each other.