CLLocation Manager如何在一定距离后更新

Posted

技术标签:

【中文标题】CLLocation Manager如何在一定距离后更新【英文标题】:CLLocation Manager how to update after certain distance 【发布时间】:2015-06-12 22:22:48 【问题描述】:

我正在使用 CLLocationManager didupdatelocations 像这样:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) 
    location = locations.last as? CLLocation

    NSNotificationCenter.defaultCenter().postNotificationName("location", object: self)


这一切正常,但我只想在该位置与原始位置相距一定距离时发布通知。我可以用吗

locations.first

并将其与 locations.last 进行比较,似乎只有在用户继续在城市周围移动时才会更新原始版本。

【问题讨论】:

Calculate Total Traveled Distance ios Swift的可能重复 locations.first 和locations.last 相同。位置只有 1 项 您不应该在每次收到位置更新时都发布通知 【参考方案1】:

要计算距离,您需要两个CLLocation(比如说newLocationoldLocation)。您可以使用以下方法计算这两个位置之间的距离:

let distance = Double(newLocation.distanceFromLocation(oldLocation))

之后只需添加逻辑来决定何时发布通知:

if distance > myMinimum distance
    NSNotificationCenter.defaultCenter().postNotificationName("location", object: self)

注意,这是计算到点之间的最短距离(直线)它不计算路线距离。

如果您想计算两点之间的路线距离,您需要使用 MKDirectionsRequest,这将返回一条或多条从 A 点到 B 点的路线,并提供分步说明:

class func caculateDistance()
    var directionRequest = MKDirectionsRequest()
    var sourceCoord = CLLocationCoordinate2D(latitude: -36.7346287, longitude: 174.6991812)
    var destinationCoord = CLLocationCoordinate2D(latitude: -36.850587, longitude: 174.7391745)
    var mkPlacemarkOrigen = MKPlacemark(coordinate: sourceCoord, addressDictionary: nil)
    var mkPlacemarkDestination = MKPlacemark(coordinate: destinationCoord, addressDictionary: nil)
    var source:MKMapItem = MKMapItem(placemark: mkPlacemarkOrigen)
    var destination:MKMapItem = MKMapItem(placemark: mkPlacemarkDestination)
    directionRequest.setSource(source)
    directionRequest.setDestination(destination)
    var directions = MKDirections(request: directionRequest)
    directions.calculateDirectionsWithCompletionHandler 
        (response, error) -> Void in
        if error != nil  println("Error calculating direction - \(error.localizedDescription)") 
        else 
            for route in response.routes
                println("Distance = \(route.distance)")
                for step in route.steps!
                    println(step.instructions)
                  
            
        
    

此示例代码将返回:

Disntance
Distance = 16800.0

Step by Step instructions
Start on the route
At the end of the road, turn left onto Bush Road
Turn right onto Albany Expressway
At the roundabout, take the first exit onto Greville Road toward 1, Auckland
At the roundabout, take the third exit to merge onto 1 toward Auckland
Keep left
Take exit 423 onto Shelly Beach Road
Continue onto Shelly Beach Road
At the end of the road, turn right onto Jervois Road
Turn left onto Islington Street
Keep right on Islington Street
Arrive at the destination

可以轻松修改该函数以接收两个位置并返回距离和任何其他需要的信息。

希望对你有帮助!

【讨论】:

以上是关于CLLocation Manager如何在一定距离后更新的主要内容,如果未能解决你的问题,请参考以下文章

计算行程时间 CLLocation Swift

未调用 CLLocation Manager didStartMonitoringForRegion 委托方法

CLLocation Manager 有时会给出不正确的位置

CLLocation Manager 使用默认位置而不是使用实际位置进行更新

CLLocation Manager 检查 requestAlwaysAuthorization 如果不接受退出应用

根据距离生成随机地理坐标