gpt4 book ai didi

ios - 为什么我的注释没有出现在我的 map View 中?

转载 作者:行者123 更新时间:2023-11-29 01:29:56 25 4
gpt4 key购买 nike

我创建了一组“IssueLocation”,这是一个符合 MKAnnotation 协议(protocol)的类。为什么我的注释(基于 API JSON 数据构建)没有出现在我的 map View 中?

这是它背后的代码:

类代码:

class IssueLocation: NSObject, MKAnnotation {

var locationName: String
var campusName: String
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D

init(locationName: String, campusName: String, latitude: Double, longitude: Double, coordinate: CLLocationCoordinate2D) {
self.locationName = locationName
self.campusName = campusName
self.latitude = latitude
self.longitude = longitude
self.coordinate = coordinate

super.init()

}

var subtitle: String? {
if (latitude == 44.22438242146097) {
return "West Campus"
} else {
return "Main Campus"
}
}

var title: String? {
return locationName
}

func mapItem() -> MKMapItem {

let addressDictionary = [String(kABPersonAddressStreetKey): locationName]

let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)

let mapItem = MKMapItem(placemark: placemark)
mapItem.name = locationName

return mapItem
}
}

创建一组 IssueLocations:

 func populateMapObjects() {

if populatingMapObjects {
return
}

populatingMapObjects = true

self.loadingIndicator.startAnimating()

var index = 0


Alamofire.request(GWNetworking.Router.MapObjects).responseJSON() { response in
if let JSON = response.result.value {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)) {

/* Making an array of all the node IDs from the JSON file */

if (JSON .isKindOfClass(NSArray)) {


for _ in JSON as! [Dictionary<String,AnyObject>] {

if let issueLocation: IssueLocation = IssueLocation(locationName: "Center of the universe", campusName: "Queen's University", latitude: 44.22661586877309, longitude: -76.49380087852478, coordinate: CLLocationCoordinate2D(latitude: 44.22661586877309, longitude: -76.49380087852478)) {


if let locationName = JSON[index]["name"] as? String {
issueLocation.locationName = locationName
}

if let latitude = JSON[index]["coordinates"]!![1] as? Double {
issueLocation.latitude = latitude
}

if let longitude = JSON[index]["coordinates"]!![0] as? Double {
issueLocation.longitude = longitude
}

issueLocation.coordinate = CLLocationCoordinate2D(latitude: issueLocation.latitude, longitude: issueLocation.longitude)

index = index+1

self.mapObjects.append(issueLocation)
}
}
}

dispatch_async(dispatch_get_main_queue()) {

self.loadingIndicator.stopAnimating()
self.populatingMapObjects = false
print(self.mapObjects.count)
}
}
}
}
}

最后,这是我尝试将注释添加到 map View 的地方:

 func loadObjectsIntoMapView() {

for mapObject in mapObjects {

let temporaryMapAnnotation = IssueLocation(locationName: mapObject.locationName, campusName: "Main Campus", latitude: mapObject.latitude, longitude: mapObject.longitude, coordinate: mapObject.coordinate)

if (temporaryMapAnnotation.longitude < -76.50921821594238) {
temporaryMapAnnotation.campusName = "West Campus"
}

self.mapView.addAnnotation(temporaryMapAnnotation)
}

}

最佳答案

将 loadObjectsIntoMapView() 移动到此 block 中:

dispatch_async(dispatch_get_main_queue()) {

self.loadingIndicator.stopAnimating()
self.populatingMapObjects = false
print(self.mapObjects.count)
}

在 viewDidLoad 中调用 loadObjectsIntoMapView() 会立即执行并且数据还没有从服务器上下来,所以 mapObjects 中没有 mapObject,因此没有注释。

关于ios - 为什么我的注释没有出现在我的 map View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33558064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com