MKAnnotations 未显示在地图上 iOS 9 / Swift 2.0
Posted
技术标签:
【中文标题】MKAnnotations 未显示在地图上 iOS 9 / Swift 2.0【英文标题】:MKAnnotations Not Showing Up On Map iOS 9 / Swift 2.0 【发布时间】:2015-11-05 07:22:32 【问题描述】:尝试使用显示其名称和地址的注释在地图上显示 100 个 EV 充电站,但以下代码应显示的 100 个注释中只有一个显示在地图上。
代码构建良好并且没有任何错误标志,但我感觉地图注释代码有问题。在运行时在调试器中收到一条很长的错误消息:
2015-11-05 01:07:47.758 JSON[65210:8214517] 此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。 堆:( 0 核心基础 0x000000010dd67f45 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010fdc2deb objc_exception_throw + 48 2 核心基础 0x000000010dd67e7d +[NSException raise:format:] + 205 3 基础 0x000000010e35c289 _AssertAutolayoutOnMainThreadOnly + 79 4 基础 0x000000010e1bccce -[NSISEngine withBehaviors:performModifications:] + 31 5 UIKit 0x000000010e95ed4a-[UIView(层次结构)_postMovedFromSuperview:] + 575 6 UIKit 0x000000010e96c7e7-[UIView(内部)_addSubview:positioned: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】:此应用程序正在从后台线程修改自动布局引擎,...
...
self.annotations[x].coordinate = locations[x] //this line causes the error log
...
您应该在后台加载数据,然后在完成后更新主线程中的视图。
【讨论】:
以上是关于MKAnnotations 未显示在地图上 iOS 9 / Swift 2.0的主要内容,如果未能解决你的问题,请参考以下文章
MKAnnotations 已成功制作,但有时无法在 MKMapView 上呈现