MKAnnotations没有出现在地图iOS 9 / Swift 2.0上

时间:2015-11-05 07:22:32

标签: ios json mkmapview swift2 mkannotation

尝试使用显示其姓名和地址的注释在地图上显示100个EV充电器电台,但地图上只显示应由下方代码显示的100个注释中的一个。

代码构建正常并且没有任何错误标志,但我感觉我在做地图注释代码时出错了。在运行时期间,在调试器中获取一条长错误消息,该消息开始于:

  

2015-11-05 01:07:47.758 JSON [65210:8214517]此应用程序正在从后台线程修改autolayout引擎,这可能导致引擎损坏和奇怪的崩溃。这将在将来的版本中导致异常。    堆:(       0 CoreFoundation 0x000000010dd67f45 __exceptionPreprocess + 165       1 libobjc.A.dylib 0x000000010fdc2deb objc_exception_throw + 48       2 CoreFoundation 0x000000010dd67e7d + [NSException raise:format:] + 205       3基金会0x000000010e35c289 _AssertAutolayoutOnMainThreadOnly + 79       4基金会0x000000010e1bccce - [NSISEngine withBehaviors:performModifications:] + 31       5 UIKit 0x000000010e95ed4a - [UIView(Hierarchy)_postMovedFromSuperview:] + 575       6 UIKit 0x000000010e96c7e7 - [UIView(内部)_addSubview:定位:relativeTo:] + 1967       7 MapKit 0x000000010e6f2c90 - [MKAnnotationContainerView addSubview:] + 128       8 MapKit 0x000000010e6f283e - [MKAnnotationContainerView addAnnotationView:allowAnimation:] + 466       9 MapKit 0x000000010e5ff976 - [MKMapView addAnnotationRepresentation:allowAnimation:] + 487       10 MapKit 0x000000010e66ed64 - [MKAnnotationManager _addRepresentationForAnnotation:] + 721       11 MapKit 0x000000010e66e3be - [MKAnnotationManager updateVisibleAnnotations] + 1551       12 MapKit 0x000000010e66f314 - [MKAnnotationManager observeValueForKeyPath:ofObject:change:context:] + 859       13基金会0x000000010e17b610 NSKeyValueNotifyObserver + 347       14基金会

//
//  ViewController.swift
//  JSON
//
//  Created by Matt Velker on 11/4/15.
//  Copyright © 2015 slingshot. All rights reserved.
//

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController {

@IBOutlet weak var map: MKMapView!
var annotations = [MKPointAnnotation](count: 100, repeatedValue: MKPointAnnotation())

override func viewDidLoad() {
    super.viewDidLoad()

    var mapLat:CLLocationDegrees = CLLocationDegrees(40.596061)
    var mapLong:CLLocationDegrees = CLLocationDegrees(-98.819799)
    var mapLongDelta:CLLocationDegrees = CLLocationDegrees(50)
    var mapLatDelta:CLLocationDegrees = CLLocationDegrees(50)
    var mapCenterLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(mapLat, mapLong)
    var mapSpan:MKCoordinateSpan = MKCoordinateSpanMake(mapLatDelta, mapLongDelta)
    var mapRegion:MKCoordinateRegion = MKCoordinateRegionMake(mapCenterLocation, mapSpan)
    map.setRegion(mapRegion, animated: true)

    let url = NSURL(string: "https://developer.nrel.gov/api/alt-fuel-stations/v1.json?fuel_type=ELEC&state=CA&limit=100&api_key=vo1v1jn4eZC83ni2pII19vYmzLmk7UQzID4VZsZT&format=JSON")!
    let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
        if let urlContent = data {
            do {
                let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
                //print(jsonResult)
                var lat: [CLLocationDegrees] = []
                var long: [CLLocationDegrees] = []
                var location: CLLocationCoordinate2D
                var locations: [CLLocationCoordinate2D] = []
                if let jsonArrayOfDictionaries = jsonResult["fuel_stations"] {
                    for var x=0; x < jsonArrayOfDictionaries!.count; x++ {
                        lat.append(jsonArrayOfDictionaries![x]["latitude"] as! CLLocationDegrees)
                        long.append(jsonArrayOfDictionaries![x]["longitude"] as! CLLocationDegrees)
                        location = CLLocationCoordinate2DMake(lat[x], long[x])
                        locations.append(location)
                        self.annotations[x].coordinate = locations[x]
                        self.annotations[x].title = jsonArrayOfDictionaries![x]["station_name"] as? String
                        self.annotations[x].subtitle = jsonArrayOfDictionaries![x]["street_address"] as? String
                    }
                }
            } catch {
                print("JSON serialization failed")
            }

        }
    }
    task.resume()
    map.addAnnotations(annotations)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

1 个答案:

答案 0 :(得分:0)

  

此应用程序正在从后台线程修改autolayout引擎,...

...
self.annotations[x].coordinate = locations[x] //this line causes the error log
...

您应该在后台加载数据,然后在完成后更新主线程中的视图。