如何在地图移动时停止地图反弹回用户位置? 负责的内容如下:
func locationAuthStatus(){
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
map.showsUserLocation = true
} else {
locationManager.requestWhenInUseAuthorization()
}
}
func centerMapOnLocation(location:CLLocation){
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2 , regionRadius * 2)
map.setRegion(coordinateRegion, animated: true)
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation){
if let location = userLocation.location {
//this is the place where you get the new location
print("\(location.coordinate.latitude) ")
print("\(location.coordinate.longitude)")
}
if let loc = userLocation.location {
centerMapOnLocation(location: loc)
locationManager.stopUpdatingLocation()
}
}
我认为locationManager.stopUpdationglocation()会处理这个问题,但好像他在移动地图时会立即更新它。 虽然它仍然应该像地图一样不时地更新用户位置。
答案 0 :(得分:0)
这是因为您在mapview update Delegate上调用 centerMapOnLocation 。因此,每次mapview重新加载时都会调用它。
所以只在为用户位置分配注释引脚的时候调用此函数。
喜欢:
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: CLLocationDegrees(Double(strLatitude)!) , longitude: CLLocationDegrees(Double(strLogitude)!)), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
self.mapView.setRegion(region, animated: true)