0
我需要查找我當前位置周圍最近的城市。你會建議我如何在我的項目中將座標填充到數組中,以及如何計算我的位置和距離我最近的位置之間的距離。我必須顯示(KM)和城市名稱的距離,以便用戶可以選擇最適合他的城市。我還需要從json文件中調用我的代碼中的座標和名稱 我的JSON文件是:如何將經度緯度從JSON文件填充到數組中
{
"City A": {
"Position": {
"Longitude": 9.96233,
"Latitude": 49.80404
}
},
"City B": {
"Position": {
"Longitude": 6.11499,
"Latitude": 50.76891
}
},
"City C": {
"Position": {
"Longitude": 6.80592,
"Latitude": 51.53548
}
和我的功能至今:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userlocation:CLLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.5, 0.5)
let region = MKCoordinateRegion(center: location, span: span)
Mapview.setRegion(region, animated: true)
let locations = ["42.6977,23.3219","43.6977,24.3219"]
let distanceMeters = userlocation.distanceFromLocation(CLLocation(latitude: 42.6977,longitude: 23.3219))
let distanceKilometers = distanceMeters/1000.00
let roundedDistanceKilometers = String(Double(round(100 * distanceKilometers)/100)) + " km"
// var distanceMeter = NSString(format: "\(distanceKilometers)km")
感謝您的示例,但您如何將用戶當前位置與其他位置進行比較? –
我更新了答案。 – vadian
你會檢查爲什麼我在這裏得到一個錯誤: 讓nearestCity = cities.sort({userLocation.distanceFromLocation($ 0.location)