2016-08-18 67 views
1

我是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,但我现在肯定。如果有人能帮我解决这些问题,我会很高兴。

+0

的可能的复制[如何来用JSON信息的数组文件和计算距离?](http://stackoverflow.com/questions/39043783/how-to-populate-an-array-w ith-information-from-json-file-and-calculation-distance) – Moritz

回答

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

希望这有助于你

+0

我想知道更多关于如何填充坐标 –

+0

你的意思是将它们解析为对象还是将位置添加到地图上的注释 –

+0

我必须采取名称和坐标,并将它们填充到数组中,该数组将填充表格单元格到表格视图 –

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) 
+0

谢谢,但是我怎样才能填充数组白色信息。在这种情况下(名称,商场拉特,lon)我必须将该信息填充到数组中 –

+0

@IvanSosev我用代码更新了答案以获取数组 –

相关问题