2015-11-05 51 views
0

试图在地图上显示100个EV充电站,使用显示其名称和地址的注释,但仅应显示在下面的代码中的100个注释之一显示在地图。MKAnnotations无法显示在地图上iOS 9/Swift 2.0

代码生成良好,并没有任何错误标志,但我有一种感觉,我做了地图注释代码的错误。在运行期间获得的调试器很长的错误消息,该消息开始:

2015年11月5日01:07:47.758 JSON [65210:8214517]本申请是从后台线程修改自动布局引擎,其可导致引擎腐败和奇怪的崩溃。这将在未来的版本中引发异常。 堆栈:( 0的CoreFoundation 0x000000010dd67f45 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010fdc2deb objc_exception_throw + 48 2的CoreFoundation 0x000000010dd67e7d + [NSException提高:格式:] + 205 3基础0x000000010e35c289 _AssertAutolayoutOnMainThreadOnly + 79 4基金会0x000000010e1bccce - [NSISEngine withBehaviors:performModifications:] + 31 5的UIKit 0x000000010e95ed4a - [UIView的(层次)_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:变化:上下文:] + 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. 
} 


} 

回答

0

此应用程序从后台线程修改自动布局引擎,...

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

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