更新位置时,MKmapview会重置

时间:2016-06-03 21:41:54

标签: swift

我有这段代码:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations.last


            let center = CLLocationCoordinate2D(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03))
            self.map.setRegion(region, animated: true)

    }

但是当我与地图进行交互并且位置更新时,会稍微回到当前位置的“中心”。是否有可能仅在第一次这样做时,并且当我与它交互时它不会移动地图?

1 个答案:

答案 0 :(得分:0)

您有什么理由可以设置属性,然后在第一次更新位置时重置它?

var updateRegion = true


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations.last


            let center = CLLocationCoordinate2D(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03))
            if updateRegion {
               self.map.setRegion(region, animated: true)
               updateRegion = false
            }



    }