仅更新一次位置

时间:2015-06-25 22:09:07

标签: ios iphone swift mapkit

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

2 个答案:

答案 0 :(得分:4)

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

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

一旦你读完一个就会停止监控...

答案 1 :(得分:1)

我不确定你的首席经理是什么,但我想你可能想看一下:

startMonitoringSignificantLocationChanges()

启动您的位置管理员。以下是关于该主题的Apple文档:https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringSignificantLocationChanges