每当我将地址搜索到谷歌地图并对其进行地理编码时,我的地图在第二次运行搜索之前不会添加标记。谁能告诉我为什么会这样? 基本上我搜索一个地址,将其名称运行到第三方地理编码器中,并改变我在地图上创建的全局标记。这样我在地图上一次只有一个标记。出于某种原因,它从来没有在我第一次搜索地址时放置标记,我必须搜索另一个地址。奇怪的是,它不必是相同的地址,如果我输入另一个地址,它只会改变标记的标题,放在我原本想要去的地方。
我的代码:
extension EventCreatorVC: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
// Do something with the selected place.
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
Nominatim.getLocation(fromAddress: place.name, completion: {(error, location) -> Void in
let latitude = (location!.latitude as NSString).doubleValue
let longitude = (location!.longitude as NSString).doubleValue
self.locationOfMarker = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
})
markerPlace(locationOfMarker: locationOfMarker, name: place.name)
}
func markerPlace(locationOfMarker: CLLocationCoordinate2D, name: String) {
marker.position = locationOfMarker
marker.title = name
}
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
答案 0 :(得分:1)
现在你的问题就像你的竞争部分需要一些时间才能完成。因此,我将此更改延迟到您的代码
Nominatim.getLocation(fromAddress: place.name, completion: {(error, location) -> Void in
let latitude = (location!.latitude as NSString).doubleValue
let longitude = (location!.longitude as NSString).doubleValue
self.locationOfMarker = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let delayTime = DispatchTime.now() + Double(Int64(0.5 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: delayTime)
{
self.markerPlace(locationOfMarker: self.locationOfMarker, name: "")
self.mapView.animate(toLocation: self.locationOfMarker)
}
})
根据我的改变方法确定它将解决您的问题。
感谢您的支持