使用JSON在Mkmapview上使用Xcode Swift 2.0多个注释

时间:2015-12-04 06:59:57

标签: xcode swift annotations location mkmapview

最近我尝试在一个mapview中放置多个注释,我使用JSON来填充我的位置纬度和经度。我在如何链接JSON文件并能够在我的mapview上显示多个注释时遇到了一些问题?

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let map = MKMapView(frame: view.bounds)
    map.autoresizingMask = .FlexibleWidth | .FlexibleHeight
    view.addSubview(map)

    var annotations = NSMutableArray()

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        let pointData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("location", ofType: "geojson")!)
        let points = NSJSONSerialization.JSONObjectWithData(pointData!,
            options: nil,
            error: nil) as NSDictionary
        for point in points["features"] as NSArray {
            let coordinate = (point["geometry"] as NSDictionary)["coordinates"] as NSArray
            let lon = coordinate[0] as CLLocationDegrees
            let lat = coordinate[1] as CLLocationDegrees
            let a = MKPointAnnotation()
            a.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
            annotations.addObject(a)
        }
        dispatch_async(dispatch_get_main_queue()) {
            map.addAnnotations(annotations)
            map.showAnnotations(map.annotations, animated: false)
        }
    }

}

}

0 个答案:

没有答案