#ios #dictionary #swift3 #currentlocation
#iOS #словарь #swift3 #текущее местоположение
Вопрос:
Я пытаюсь получить свое текущее местоположение, но мое приложение этого не сделает: (Не могли бы вы помочь мне определить, что не так? Приложение работает без каких-либо ошибок.
импорт UIKit импорт MapKit импорт CoreLocation
класс MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @IBOutlet вар MapView:MKMapView!
let locationManager = CLLocationManager()
var restaurant:Restaurant!
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
// Convert address to coordinate and annotate it on map
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(restaurant.location, completionHandler: { placemarks, error in
if error != nil {
print(error)
return
}
if let placemarks = placemarks {
// Get the first placemark
let placemark = placemarks[0]
// Add annotation
let annotation = MKPointAnnotation()
annotation.title = self.restaurant.name
annotation.subtitle = self.restaurant.type
if let location = placemark.location {
annotation.coordinate = location.coordinate
// Display the annotation
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)
}
}
})
// Map customization
mapView.showsCompass = true
mapView.showsScale = true
mapView.showsTraffic = true
// Set the MKMapViewDelegate
mapView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - MKMapViewDelegate methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didFailWithError: Error)
{
print("Errors:")
}
Комментарии:
1. в чем проблема?
2. Миша, я не вижу своего местоположения на карте, оно просто не работает, какие-либо предложения?
3. вы не опубликовали код для разрешений, поэтому я предполагаю, что вы запрашиваете их, и добавлены ключи plist?
4. Теперь он работает, спасибо, кстати, вы знаете, как я могу создать / включить навигацию между моим текущим местоположением и моими контактами с адресами?