Sto cercando di trovare la latitudine e la longitudine della posizione dell'utente in modo da poter centrare la mappa sull'utente in viewdidload.Come devo trovare i dati di posizione corretti utilizzando userLocation in Mapbox per iOS SDK? (Swift)
Ho implementato quello che sembra essere il codice giusto ma i valori di userLat (latitudine) e userLon (longitudine) sono lontani.
N.B. Qualcun altro ha avuto lo stesso problema come me, ma la sua risposta non è mai stato risolto: stampa Mapbox iOS8 Swift mapView.showUsersLocation
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate {
// let locationManager = CLLocationManager()
@IBOutlet weak var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Initalise map's center coordinate as vancouver
mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: 49.283382,
longitude: -123.117394),
zoomLevel: 15, animated: false)
view.addSubview(mapView)
// Set the delegate property of our map view to self after instantiating it.
mapView.delegate = self
// User location
mapView.showsUserLocation = true
let userLoc = mapView.userLocation!
userLoc.title = "Hello"
userLoc.subtitle = "I am here!"
let userLat = userLoc.coordinate.latitude
let userLon = userLoc.coordinate.longitude
print(userLat, userLon)
/*
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}*/
}
func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
}
risultante:
3.40282346638529e+38 3.40282346638529e+38
La cosa strana è che l'annotazione funziona bene, e quando clicco mia posizione Ottengo il titolo e il sottotitolo.