计算我的位置和Swift上的MapKit引脚之间的距离 [英] Calculate distance between my location and a MapKit pin on Swift

查看:134
本文介绍了计算我的位置和Swift上的MapKit引脚之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助,我正在开发一个应用程序,我有一些针脚(位置),我想要的是获得每个人和我的位置之间的距离。我的代码如下:

$ p $ let annotation = MKPointAnnotation()
let annotationTwo = MKPointAnnotation()
let saintPaulHospitalBC = MKPointAnnotation()

覆盖func viewDidLoad(){
super.viewDidLoad()

mapita.showsUserLocation = true // Mapita是MapView的名称。

annotation.coordinate = CLLocationCoordinate2D(纬度:25.647399800,经度:-100.334304500)
mapita.addAnnotation(注释)

annotationTwo.coordinate = CLLocationCoordinate2D(纬度:25.589339000 ,经度:-100.257724800)
mapita.addAnnotation(annotationTwo)

saintPaulHospitalBC.coordinate = CLLocationCoordinate2D(纬度:49.280524700,经度:-123.128232600)
mapita.addAnnotation(SaintPaulHospitalBC)
}

当我运行代码时,地图会显示引脚,但是我还能做开始计算距离?谢谢!

解决方案

创建一个帮助函数来计算用户位置和给定位置之间的距离 MKPointAnnotation pin:

  ///返回距离(以米为单位)从
///用户位置到指定点。
private func userDistance(from point:MKPointAnnotation) - >双? {
guard let userLocation = mapita.userLocation.location else {
return nil //用户位置未知!
}
let pointLocation = CLLocation(
latitude:point.coordinate.latitude,
longitude:point.coordinate.longitude

return userLocation.distance (from:pointLocation)
}

最后,圣保罗医院



 如果let distance = userDistance(来自:saintPaulHospitalBC){
//使用这里距离......
}






地理位置跟踪延迟。尽管如此,用户距离可能并不总是始终可用,因为MapKit / CoreLocation地理位置跟踪可能仍在后台运行。



解决这个问题的一个方法是符合 MKMapViewDelegate

code>协议
并等待 mapView(_:didUpdate:)回调,最后计算距离。


I need your help, I'm working on an App where I have some pins (locations) and what I want is to get the distance between each one and my location. My code is the following

let annotation = MKPointAnnotation()
let annotationTwo = MKPointAnnotation()
let saintPaulHospitalBC = MKPointAnnotation()

override func viewDidLoad() {
    super.viewDidLoad()

    mapita.showsUserLocation = true // Mapita is the name of the MapView.

    annotation.coordinate = CLLocationCoordinate2D(latitude: 25.647399800, longitude: -100.334304500)
    mapita.addAnnotation(annotation)

    annotationTwo.coordinate = CLLocationCoordinate2D(latitude: 25.589339000, longitude: -100.257724800)
    mapita.addAnnotation(annotationTwo)

    saintPaulHospitalBC.coordinate = CLLocationCoordinate2D(latitude: 49.280524700, longitude:  -123.128232600)
    mapita.addAnnotation(SaintPaulHospitalBC)
}

When I run the code, the map shows the pins, but what else can I do to start calculating the distance? Thank you!

解决方案

Create a helper function to compute the distance between the user location and a given MKPointAnnotation pin:

/// Returns the distance (in meters) from the
/// user's location to the specified point.
private func userDistance(from point: MKPointAnnotation) -> Double? {
    guard let userLocation = mapita.userLocation.location else {
        return nil // User location unknown!
    }
    let pointLocation = CLLocation(
        latitude:  point.coordinate.latitude, 
        longitude: point.coordinate.longitude
    )
    return userLocation.distance(from: pointLocation)
}

Finally, to get the user distance to Saint Paul hospital:

if let distance = userDistance(from: saintPaulHospitalBC) {
    // Use distance here...
}


Geolocation tracking latency. There is a catch though: the user distance might not always be available at first, since MapKit/CoreLocation geolocation tracking might still be running in the background.

One way around this, is to conform to the MKMapViewDelegate protocol and wait for the mapView(_:didUpdate:) callback before finally computing your distances.

这篇关于计算我的位置和Swift上的MapKit引脚之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆