Reputation: 437
Whenever I search an address into google maps and geocode it, my map doesn't add a marker until I run the search a second time. Can anyone tell me why that is? Basically I search an address, run through the name of it into a 3rd party geocoder, and mutate a global marker I created on the map. This is so I only have one marker at a time on the map. For some reason though, it never places the marker the first time I search for an address, I have to search for another address. The weird thing is, it doesn't have to be the same address, if I enter another address it will just change the title of the marker, putting where I originally wanted it to go.
My code:
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
}
}
Upvotes: 0
Views: 225
Reputation: 2394
Now your problem is like your compeletion part take some time to get finished. So I make it delay with this change to your code as
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)
}
})
Change method as per mine sure it will solve your issue.
Thank you for your support
Upvotes: 1