Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func updateMap() {
- //posizione dell'utente
- var currentLocation: CLLocation!
- if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
- CLLocationManager.authorizationStatus() == .authorizedAlways){
- currentLocation = locationManager.location
- }
- let location = CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
- let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008))
- self.vistaMappa.setRegion(region, animated: true)
- self.navigationController!.navigationBar.topItem!.title = "Mappa scuole"
- let apps = DownloadManager.shared.localArra
- for app in apps {
- let location = CLLocationCoordinate2D(latitude: CLLocationDegrees(Float(app.latScuola)), longitude : CLLocationDegrees(Float(app.lngScuola)))
- scuoleLocations.append(Pin(pinTitle: app.nomeScuola, pinSubTitle: app.indirizzoScuola, location: location))
- }
- //print(scuoleLocations)
- self.vistaMappa.addAnnotations(scuoleLocations)
- }
- func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
- if annotation is MKUserLocation {
- return nil
- }
- let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
- annotationView.image = UIImage(named: "marker")
- annotationView.canShowCallout = true
- let btn = UIButton(type: .detailDisclosure)
- annotationView.rightCalloutAccessoryView = btn
- return annotationView
- }
- func mapView(_ mapView: MKMapView,
- annotationView view: MKAnnotationView,
- calloutAccessoryControlTapped control: UIControl)
- {
- performSegue(withIdentifier: "toDettaglio", sender: self)
- }
- //implemento il protocollo con locationManager
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- //verifico se posso recuperare le coordinate (e in caso le recupero)
- if let lat = locations.last?.coordinate.latitude, let long = locations.last?.coordinate.longitude {
- print("\(lat),\(long)")
- } else {
- print("Nessuna coordinata")
- }
- }
- //implemento monitoraggio errore
- func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
- print(error)
- }
- //estrapolo dalle coordinate il nome della città o luogo in cui l'utente si trova
- func lookUpCurrentLocation(completionHandler: @escaping (CLPlacemark?) -> Void ) {
- // recupero direttamente da locationManager le ultime coordinate memorizzate
- if let lastLocation = self.locationManager.location {
- //creo un'istanza di CLGeocoder per il reversing delle coordinate
- let geocoder = CLGeocoder()
- // Look up the location and pass it to the completion handler
- geocoder.reverseGeocodeLocation(lastLocation, completionHandler: { (placemarks, error) in
- if error == nil {
- let firstLocation = placemarks?[0]
- completionHandler(firstLocation)
- }
- else {
- // An error occurred during geocoding.
- completionHandler(nil)
- }
- })
- }
- else {
- // No location was available.
- completionHandler(nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement