gpt4 book ai didi

swift - 从 GoogleMaps API 获取数据后,注释不会显示在 MKMapView 上

转载 作者:行者123 更新时间:2023-11-30 12:31:06 26 4
gpt4 key购买 nike

我尝试在从 GoogleMaps API 获取数据后在 MKMapView 上显示注释。 HTTP请求成功,我可以在Xcode底部的控制台中看到结果。但是,当尝试在 map 上显示注释时,它不会返回任何内容。仅 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)
}
}

知道为什么它不显示吗?我错过了什么吗?

谢谢!

最佳答案

let placeObject = CustomPointAnnotation()
placeObject.coordinate = placeCoord
placeObject.title = name
placeObject.subtitle = vicinity

尝试使用 CustomPointAnnotation 对象作为注释

关于swift - 从 GoogleMaps API 获取数据后,注释不会显示在 MKMapView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43553664/

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