[英]How to calculate the distance between my current location and locations populated in a JSON File
I am new to Swift, but I am trying to develop my first application. 我是Swift的新手,但我正在尝试开发我的第一个应用程序。 So far I want to calculate the distance between my current location and the nearest shopping malls around me. 到目前为止,我想计算当前位置和我周围最近的购物中心之间的距离。 The locations and the names of the shopping malls are populated in a JSON File. 购物中心的位置和名称填充在JSON文件中。 I have created a UISLider between 0 and 50km and when the user select for example 5km, the app has to display all of the shopping malls which are in radius of 5km from the user's location. 我创建了一个介于0到50公里之间的UISLider,当用户选择例如5公里时,该应用程序必须显示距用户所在位置半径5公里的所有购物中心。 My JSON file is: 我的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"
}
}
}
and Code so far is : 和代码到目前为止:
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)
I have also created a Table View Controller and all of the displayed Shopping Malls will be displayed in the Table View Cells. 我还创建了一个Table View Controller,所有显示的Shopping Malls都将显示在Table View Cells中。 The main problem is that I do not know how to get the names and the coordinates of the shopping malls from the JSON file and I do not know how to transfer populate them in the Table View Cells. 主要问题是我不知道如何从JSON文件中获取购物中心的名称和坐标,我不知道如何在Table View Cells中填充它们。 I was wondering maybe I can use prepare for segue, but I am now sure. 我想知道也许我可以为segue做准备,但我现在肯定。 I would be glad if someone can help me solve these issues. 如果有人能帮我解决这些问题,我会很高兴的。
you can treat this situation as if your location is the centre of circle and try get the distance between your coordinate and other points coordinates using this method 您可以将此情况视为您的位置是圆心,并尝试使用此方法获取坐标与其他点坐标之间的距离
struct Point {
var x:Float;
var y:Float;
}
func getDistanceBetweenPoints(pointOne:Point,pointTwo:Point) ->Float{
let deltaX = (pointOne.x-pointTwo.x)*(pointOne.x-pointTwo.x);
let deltay = (pointOne.y-pointTwo.y)*(pointOne.y-pointTwo.y);
let distanceSquared = deltaX+deltay;
return sqrt(distanceSquared);
}
Hope this helps you 希望这对你有所帮助
1. Get People
objects from JSON: 1.从JSON获取People
对象:
Use ObjectMapper 使用ObjectMapper
class Person: Mappable {
var name: String?
var mall: String?
var lat: Float?
var lon: Float?
}
2. Get array: 2.获取数组:
var list: Array<Person> = Mapper<Person>().mapArray(string: json)
3. Get distances: 3.获得距离:
let meters:CLLocationDistance = mallLocation.distanceFromLocation(mineLocation)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.