在iOS中,我有一个MKMapView
和CLLocationManager
来更新标题和位置。无论我做什么,我都无法让地图视图停止放大用户的位置。我在地图上有注释,我希望在任何时候都可见,我希望在用户旋转设备时更新标题。这是使用mapView.setRegion
工作,但稍后,地图锁定在北方,不再旋转。以下是我正在使用的CLLocationManager
代码:
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
if data != nil{
setRegion(locationManager?.location?.coordinate.latitude)!, longitude: (locationManager?.location?.coordinate.longitude)!), data: data!)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if !regionSet && data != nil{
setRegion(location: CLLocationCoordinate2D(latitude: (locationManager?.location?.coordinate.latitude)!, longitude: (locationManager?.location?.coordinate.longitude)!), data: data!)
regionSet = true
}
}
setRegion计算要放大的地图范围并适当设置setRegion函数。
下面是setRegion。我通过查找注释的最大纬度/长度以及函数中的使用来加载注释时计算zoomWindowDegrees变量。
private func setRegion(location:CLLocationCoordinate2D, towers:[Tower]){
let center = location
let coordinateSpan = MKCoordinateSpan(latitudeDelta: zoomWindowDegrees, longitudeDelta: zoomWindowDegrees)
let region = MKCoordinateRegion(center: center, span: coordinateSpan)
mapView.setRegion(region, animated: false)
}