我是Swift的新手,但我正在尝试开发我的第一个应用程序。到目前为止,我想计算我目前的位置和我周围最近的购物商场之间的距离。商场的位置和名称都填入JSON文件中。我创建了一个介于0到50公里之间的UISLider,当用户选择例如5公里时,应用程序必须显示距离用户位置5公里范围内的所有商场。我的JSON文件是:如何计算我的当前位置和填充在JSON文件中的位置之间的距离
{
"People": {
"Person0": {
"A1": "New York",
"B1": "ShoppingMall2",
"C1": "43.0757",
"D1": "23.6172"
},
"Person1": {
"A1": "London",
"B1": "ShoppingMall2",
"C1": "44.0757",
"D1": "24.6172"
},
"Person2": {
"A1": "Paris",
"B1": "ShoppingMall3",
"C1": "45.0757",
"D1": "25.6172"
},
"Person3": {
"A1": "Bern",
"B1": "ShoppingMall4",
"C1": "41.0757",
"D1": "21.6172"
},
"Person4": {
"A1": "Sofia",
"B1": "ShoppingMall4",
"C1": "46.0757",
"D1": "26.6172"
}
}
}
和到目前为止的代码是:
import UIKit
import MapKit
import CoreLocation
import SwiftyJSON
class FirstViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var LabelTest: UILabel!
@IBOutlet weak var Slider: UISlider!
@IBOutlet weak var LabelValueSlider: UILabel!
var MySliderCurrentValue = Double()
var manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func SliderChange(sender: UISlider) {
let MySliderCurrentValue: String = String(Int(sender.value))
LabelValueSlider.text = MySliderCurrentValue
}
@IBAction func LocateMe(sender: AnyObject) {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
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)
我还创建了一个表视图控制器和显示的所有商场将显示在表格视图单元。主要问题是我不知道如何从JSON文件中获取购物中心的名称和坐标,我不知道如何将它们填充到表格视图单元格中。我想知道,也许我可以使用准备for segue,但我现在肯定。如果有人能帮我解决这些问题,我会很高兴。
的可能的复制[如何来用JSON信息的数组文件和计算距离?](http://stackoverflow.com/questions/39043783/how-to-populate-an-array-w ith-information-from-json-file-and-calculation-distance) – Moritz