gpt4 book ai didi

swift - 从当前位置到注释的距离。 (火力基地)

转载 作者:行者123 更新时间:2023-11-30 12:09:37 24 4
gpt4 key购买 nike

我想要知道从我的当前位置到 FireData Base 中的注释的距离。我试图用文字表达出来,但我不能;(。我想在变量中得到两个位置之间的距离。我希望你们能帮助我。

func reload(){
//get data
Database.database().reference().child("Rollerbanken").observe(.value, with: { (snapshot) in



for item in snapshot.children{

if let value = snapshot.value as? Dictionary<String, Any> {

for key in value.keys {
if let itemDict = value[key] as? Dictionary<String, AnyObject> {

let annotation = MKPointAnnotation()
annotation.title = itemDict["TypeControle"] as! String
let tijd = itemDict["Tijd"] as! String
annotation.subtitle = "Geplaatst om \(tijd)"

let getLatitude = itemDict["Latitude"] as? String
let getLongitude = itemDict["Longitude"] as? String
if let lat = getLatitude, let long = getLongitude {
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(long)!)

self.map.addAnnotation(annotation)

let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem.forCurrentLocation()
if #available(iOS 10.0, *) {
directionRequest.destination = MKMapItem(placemark: MKPlacemark.init(coordinate: CLLocationCoordinate2DMake(Double(lat)!, Double(long)!)))
} else {
// Fallback on earlier versions
}
directionRequest.transportType = .walking
let direction = MKDirections(request: directionRequest)
direction.calculate(completionHandler: { (response, error) in
if error != nil {
print("Error while build route")
} else {
let route = response?.routes.last
let distance = route?.distance
print(distance)
}
})
}
}
}
}
}
})
}

这是我的结构: Structure

最佳答案

尝试使用此代码。不要忘记在 map 上启用您的当前位置

let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem.forCurrentLocation()
directionRequest.destination = MKMapItem(placemark: MKPlacemark.init(coordinate: CLLocationCoordinate2DMake(YOURPOINTLATITUDE, YOURPOINTLONGITUDE)))
directionRequest.transportType = .walking
let direction = MKDirections(request: directionRequest)
direction.calculate(completionHandler: { (response, error) in
if error != nil {
print("Error while build route")
} else {
let route = response?.routes.last
let distance = route?.distance

关于swift - 从当前位置到注释的距离。 (火力基地),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com