0
我試圖從GoogleMaps API提取數據後在MKMapView上顯示註解。 HTTP請求成功,我可以在Xcode底部的控制檯中看到結果。但是,當試圖在地圖上顯示註釋時,它不會返回任何內容。只有當前位置(藍點)。這裏是代碼:從GoogleMaps API提取數據後,註解不顯示在MKMapView上
func plotPositions(_ data: [Any]) {
//Remove any existing custom annotations but not the user location blue dot
for annotation: MKAnnotation in mapView.annotations {
if (annotation is MapPoint) {
mapView.removeAnnotation(annotation)
}
}
//Loop through the array of places returned from the Google API
for i in 0..<data.count {
//Retrieve the NSDictionary object in each index of the array
let place: [AnyHashable: Any]? = (data[i] as? [AnyHashable: Any])
//There is a specific NSDictionary object that gives us the location info
let geo: [AnyHashable: Any]? = (place?["geometry"] as? [AnyHashable: Any])
// Get the lat and long for the location
let loc: [AnyHashable: Any]? = (geo?["location"] as? [AnyHashable: Any])
//Get the name and address info for adding to a pin
let name: String? = (place?["name"] as? String)
let vicinity: String? = (place?["vicinity"] as? String)
// Create a special variable to hold this coordinate info
var placeCoord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
// Set the lat and long
if let latitude = loc?["lat"] as? CDouble, let longitude = loc?["lat"] as? CDouble {
placeCoord.latitude = latitude
placeCoord.longitude = longitude
}
//Create a new annotation
let placeObject = MapPoint(name: name!, address: vicinity!, coordinate: placeCoord)
mapView.addAnnotation(placeObject)
}
}
任何想法,爲什麼它不顯示?我錯過了什麼嗎?
謝謝!