如何计算当前位置与JSON文件中填充的位置之间的距离

时间:2016-08-18 19:13:50

标签: ios json location

我是Swift的新手,但我正在尝试开发我的第一个应用程序。到目前为止,我想计算当前位置和我周围最近的购物中心之间的距离。购物中心的位置和名称填充在JSON文件中。我创建了一个介于0到50公里之间的UISLider,当用户选择例如5公里时,该应用程序必须显示距用户所在位置半径5公里的所有购物中心。我的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)

我还创建了一个Table View Controller,所有显示的Shopping Malls都将显示在Table View Cells中。主要问题是我不知道如何从JSON文件中获取购物中心的名称和坐标,我不知道如何在Table View Cells中填充它们。我想知道也许我可以为segue做准备,但我现在肯定。如果有人能帮我解决这些问题,我会很高兴。

2 个答案:

答案 0 :(得分:0)

您可以将此情况视为您的位置是圆心,并尝试使用此方法获取坐标与其他点坐标之间的距离

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);
 }

example and formula

希望这有助于你

答案 1 :(得分:0)

<强> 1。从JSON获取People个对象:

使用ObjectMapper

class Person: Mappable {
    var name: String?
    var mall: String?
    var lat: Float?
    var lon: Float?
}

<强> 2。获取数组:

var list: Array<Person> = Mapper<Person>().mapArray(string: json)

第3。获得距离:

let meters:CLLocationDistance = mallLocation.distanceFromLocation(mineLocation)