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

标签 ios swift

我正在使用 CLLocationManager didupdatelocations 像这样:

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

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

}

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

locations.first

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

最佳答案

要计算距离,您需要两个 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

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

希望对你有帮助!

关于ios - CLLocation Manager 如何在一定距离后更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813008/

相关文章:

ios - Xcode - 禁用所有元素的可访问性

ios - UITableView 在初始加载时未正确绘制单元格 - Swift

swift - 单击触摸开始时,我的对象的物理主体不会跳跃

swift - 什么是 Swift 中的多次序列?

ios - Xcode 错误 "Profile doesn' t include the beta-reports-active entitlement"for adhoc profile

iphone - 自定义 UITableViewCell,其样式看起来非常像 UITableViewCellStyleValue2

iPhone iOS : Can you add crosshairs or other visual indicator to ZBar barcode scanning?

iphone - 使用NSDateComponents的月份的最后一个工作日

ios - 单独类中的图像选择器 Controller 委托(delegate)不起作用

ios - UISearchController 的自定义单元格