2015-11-06 51 views
0

我已經創建了一組「IssueLocation」's,這符合MKAnnotation協議的一類。爲什麼我的註釋(從API JSON數據構建而來)沒有出現在我的地圖視圖中?爲什麼我的標註沒有出現在我的地圖視圖中?

這裏是它背後的所有代碼:

類代碼:

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) 
       } 
      } 
     } 
    } 
} 

最後,這是我嘗試添加註釋到地圖視圖:

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) 
    } 

} 
+0

你打電話loadObjectsIntoMapView()?當你派遣到主隊列時,你會想在你得到JSON之後這樣做嗎? – beyowulf

+0

我在viewDidLoad中 –

+0

嘿調用populateMapObjects後立即調用它!有效!操作可能發生在錯誤的順序!註釋現在出現在地圖上。 –

回答

1

移動loadObjectsIntoMapView()到該塊:

dispatch_async(dispatch_get_main_queue()) { 

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

在調用viewDidLoad中loadObjectsIntoMapView()將立即執行,且數據還沒有從服務器下來,因此還必須在MapObjects的無MapObject的,因此沒有註釋。

+0

非常感謝。它現在有效 –

相關問題