仅更新一次位置

Jos*_*nor 0 iphone mapkit ios swift

我正在为我的应用程序添加一个 mapKit,并将地图的中间设置为人员当前位置(以纬度和经度为单位)。我能够让它工作,但是,它经常更新。我只希望它在加载应用程序时更新一次,然后不再更新。可能每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()
}
Run Code Online (Sandbox Code Playgroud)

Mir*_*ekE 5

如果您仅针对 iOS 9,请查看新requestLocation()方法。它解决了您的问题。如果您需要针对旧版本的 iOS,您可以添加stopMonitoringLocation()

func locationManager(
    manager: CLLocationManager!, 
    didUpdateLocations locations: [AnyObject]!
)
Run Code Online (Sandbox Code Playgroud)

一旦你得到一个读数,这将停止监控......