简体   繁体   English

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

[英]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);
 }

example and formula 例子和公式

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.

相关问题 如何计算我当前位置和MapView中其他Pins之间的距离 - How to calculate the distance between my current location and other Pins in MapView 计算位置与位置列表之间的距离 - calculate distance between a location and a list of locations 当一个位置更改用户位置而第二个位置更改用户位置时,如何计算位置之间的距离? - How to calculate distance between locations when one is changing user location and second one should be constant? 如何在iOS中通过Localsearch(搜索栏)计算当前位置与所选位置/目的地之间的距离 - How to calculate distance between current location and selected location/destination from Localsearch (search bar) in iOS 在iOS中的固定点和我当前位置之间行走时计算距离 - Calculate distance while walking between a fixed point and my current location in iOS 如何使用谷歌计算两个位置之间的距离 api in swift 4 - How to calculate distance between two locations using google api in swift 4 如何计算用户当前位置和多个地理坐标之间的最短距离 - How to calculate shortest distance between user current location and multiple geo coordinate 计算我的位置和 Swift 上的 MapKit 引脚之间的距离 - Calculate distance between my location and a MapKit pin on Swift 当我的接送位置相同时,如何计算驾驶室所覆盖的点之间的总距离? - How to calculate total distance between points covered by a Cab when my pickup and drop location are same? 如何根据远程文件中的位置检查我的当前位置 - iOS 应用 - How to check my current location against locations in a remote file - iOS app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM