I'm using Mapbox to build a rider/driver app. All fine, except when I try to reverse geocode my current location. This give me wrong result with wrong location.
Here is the code:
// VALocation.swift
import Foundation
import MapboxSearch
final class VALocation {
fileprivate var result: SearchResult!
public convenience init(with searchResult: SearchResult) {
self.init()
self.result = searchResult
}
func searchResult() -> SearchResult {
return self.result
}
}
import MapboxSearch
// ViewController vars
let searchEngine = SearchEngine()
// Current selected pickUp & dropOff
var currentPickUp: VALocation?
var currentDropOff: VALocation?
// viewDidLoad
self.searchEngine.defaultSearchOptions = SearchOptions(countries: ["CU"], languages: ["es"], limit: 10)
// Function called when user tap get current location. Here I autocomplete a textField with reverse geocoding values & set self.pickUp
func getCurrentUserLocation() {
guard let lastKnowLocation = self.mapManager.getLastKnowLocation() else {
return
}
// lastKnowLocation = <+23.05375215,-82.44208426> +/- 3.54m (speed 0.00 mps / course 328.01) @ 21/2/23 10:24:09 p.m. hora estándar de Cuba
print("lastKnowLocation: \(lastKnowLocation.coordinate)")
let options = ReverseGeocodingOptions(point: lastKnowLocation.coordinate, countries: ["CU"], languages: ["es"])
self.searchEngine.reverseGeocoding(options: options) { result in
switch result {
case .success(let results):
guard let result = results.first else {
return
}
Async.main {
let vaLocation = VALocation(with: result)
self.currentPickUp = vaLocation
self.tfPickUp.text = vaLocation.searchResult().address?.formattedAddress(style: .short) ?? vaLocation.searchResult().name
print("Reversed: \(vaLocation.searchResult().coordinate)")
// print: CLLocationCoordinate2D(latitude: 23.024011611938477, longitude: -82.47828674316406) <- Wrong lat/lon (would be the same)
}
case .failure(let error):
print(error)
}
}
}
Later, when I draw route, this begin from a bad origin coordinate. That coordinate is obtained from the ReverseGeocode, and is wrong. Some pict to clarify the thinks:
Here, the red circle is where the route must begin, and the blue circle is the bad coordinate I get from reverse geocode the original (my current location) coordinate.
Just for reference, when I forward geocode & reverse geocode with MapKit, all of this works like a charm, but I loss good functionalities from MapBox itself.
I have no idea what I'm doing wrong or what I need to configure to get the desired result. Any help will be appreciated.