2015-11-05 52 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 
... 

你應該在後臺加載數據,然後當你完成更新主線程的視圖。