2015-10-09 141 views
9

我正在使用mapkit。我想讓地圖縮放以顯示用戶的位置和註釋點,而不是僅縮放到用戶的當前位置。使地圖放大到用戶位置和註釋(swift 2)

目前我有:

      let annotation = MKPointAnnotation() 
          annotation.coordinate = CLLocationCoordinate2DMake(mapLat, mapLon) 
          annotation.title = mapTitle 
          self.map.addAnnotation(annotation) 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let userLoction: CLLocation = locations[0] 
    let latitude = userLoction.coordinate.latitude 
    let longitude = userLoction.coordinate.longitude 
    let latDelta: CLLocationDegrees = 0.05 
    let lonDelta: CLLocationDegrees = 0.05 
    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta) 
    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude) 
    let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span) 
    self.map.setRegion(region, animated: true) 
    self.map.showsUserLocation = true 
} 

它只是將縮放到用戶的位置和其被鎖定的位置。當我嘗試滾動時,它只是跳回到用戶的位置。我究竟做錯了什麼?如何讓用戶在地圖上縮放或移動,而不會跳回到當前位置?

我試圖放大顯示註釋和用戶在同一視圖上的位置。即使註釋很遠,我也希望它縮放以顯示用戶和註釋。

回答

10

它只是跳回到用戶的位置,因爲didUpdateLocations方法被多次調用。有兩種解決方案。

1)如果你使用requestLocation方法,而不是startUpdatingLocation使用requestLocation

didUpdateLocations方法被調用一次

if #available(iOS 9.0, *) { 
    locationManager.requestLocation() 
} else { 
    // Fallback on earlier versions 
} 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let userLoction: CLLocation = locations[0] 
    let latitude = userLoction.coordinate.latitude 
    let longitude = userLoction.coordinate.longitude 
    let latDelta: CLLocationDegrees = 0.05 
    let lonDelta: CLLocationDegrees = 0.05 
    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta) 
    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude) 
    let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span) 
    self.map.setRegion(region, animated: true) 
    self.map.showsUserLocation = true 
} 

2)如果你用requestLocation去使用標誌

var isInitialized = false 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if !isInitialized { 
     // Here is called only once 
     isInitialized = true 

     let userLoction: CLLocation = locations[0] 
     let latitude = userLoction.coordinate.latitude 
     let longitude = userLoction.coordinate.longitude 
     let latDelta: CLLocationDegrees = 0.05 
     let lonDelta: CLLocationDegrees = 0.05 
     let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta) 
     let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude) 
     let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span) 
     self.map.setRegion(region, animated: true) 
     self.map.showsUserLocation = true 
    } 
} 
+0

,你也必須添加一個失敗的函數:'func locationManager(manager:CLLocationManager,didFailWithError error:NSError) { //做某事 }' – Dohab