我正在尝试从GoogleMaps API获取数据后在MKMapView上显示注释。 HTTP请求成功,我可以在Xcode底部的控制台中看到结果。但是,在尝试在地图上显示注释时,它不会返回任何内容。只有mu当前位置(蓝点)。这是代码:
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)
}
}
知道它为什么不显示?我错过了什么吗?
谢谢!
答案 0 :(得分:0)
let placeObject = CustomPointAnnotation()
placeObject.coordinate = placeCoord
placeObject.title = name
placeObject.subtitle = vicinity
尝试使用CustomPointAnnotation
对象作为注释