2015-06-25 133 views
0

我爲我的應用程序添加了一個mapKit,並且我將地圖的中間位置設置爲lat和lng中的當前位置。我能夠得到它的工作,但是,它經常更新。我只希望它在應用程序加載時更新一次,然後不再更新。也許每2分鐘更新一次。這裏是我的代碼:只更新位置一次

func locationManager(manager: CLLocationManager!, 
    didUpdateLocations locations: [AnyObject]!) 
{ 
    var latestLocation = locations.last as! CLLocation 


    let location = CLLocationCoordinate2D(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.longitude) 
    let span = MKCoordinateSpanMake(0.015, 0.015) 

    //Let our point be the center of our span 
    //region holds value of span and location 
    let region = MKCoordinateRegion(center: location, span: span) 

    //set up region on mapView 
    mapView.setRegion(region, animated: true) 

    //MKPointAnnotation defines a concrete annotation 
    let annotation = MKPointAnnotation() 
} 

回答

4

如果你只針對iOS 9,看看新的requestLocation()方法。它解決你的問題。如果您需要針對舊版iOS,您可以添加stopMonitoringLocation()

func locationManager(
    manager: CLLocationManager!, 
    didUpdateLocations locations: [AnyObject]! 
) 

,一旦你得到一個閱讀這將停止監控...