正在根据用户航向更新 iOS 地图
Updating iOS map based on user heading
我在启用地图自动跟踪用户航向时遇到问题。我已经请求了所有必要的授权,并且能够捕获用户位置信息。我目前正在调用此功能以以用户的位置为中心并且它正在运行。如果我添加包含 map.setUserTrackingMode 的行,则会出错。
我是不是漏掉了什么?
我正在使用的代码:
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius, regionRadius)
map.setRegion(coordinateRegion, animated: true)
map.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading, animated: true)
}
调用此函数的代码:
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
if let loc = userLocation.location {
centerMapOnLocation(loc)
}
}
可能是 setUserTrackingMode 干扰了 setRegion。这两种方法都将地图置于位置中心,尝试推迟跟踪模式的设置,正如此处也建议的那样,或者您也可以使用特殊按钮 MKUserTrackingBarButtonItem 实现跟踪模式。
我在启用地图自动跟踪用户航向时遇到问题。我已经请求了所有必要的授权,并且能够捕获用户位置信息。我目前正在调用此功能以以用户的位置为中心并且它正在运行。如果我添加包含 map.setUserTrackingMode 的行,则会出错。
我是不是漏掉了什么?
我正在使用的代码:
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius, regionRadius)
map.setRegion(coordinateRegion, animated: true)
map.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading, animated: true)
}
调用此函数的代码:
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
if let loc = userLocation.location {
centerMapOnLocation(loc)
}
}
可能是 setUserTrackingMode 干扰了 setRegion。这两种方法都将地图置于位置中心,尝试推迟跟踪模式的设置,正如此处也建议的那样,或者您也可以使用特殊按钮 MKUserTrackingBarButtonItem 实现跟踪模式。