只更新一次位置

Update location only once

我正在为我的应用程序添加一个 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()
}

我不确定你的位置管理器是用什么开始的,但我想你可能想看看使用:

startMonitoringSignificantLocationChanges()

启动您的位置管理器。这是苹果关于这个主题的文档:https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringSignificantLocationChanges

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

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

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