Advertisement
axxe16

metodi mappa

Nov 10th, 2018
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.84 KB | None | 0 0
  1.   func updateMap() {
  2.        
  3.         //posizione dell'utente
  4.         var currentLocation: CLLocation!
  5.        
  6.         if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
  7.             CLLocationManager.authorizationStatus() ==  .authorizedAlways){
  8.            
  9.             currentLocation = locationManager.location
  10.            
  11.         }
  12.        
  13.         let location = CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
  14.        
  15.         let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008))
  16.        
  17.         self.vistaMappa.setRegion(region, animated: true)
  18.        
  19.         self.navigationController!.navigationBar.topItem!.title = "Mappa scuole"
  20.        
  21.        
  22.         let apps = DownloadManager.shared.localArra
  23.        
  24.         for app in apps {
  25.            
  26.             let location = CLLocationCoordinate2D(latitude: CLLocationDegrees(Float(app.latScuola)), longitude : CLLocationDegrees(Float(app.lngScuola)))
  27.            
  28.             scuoleLocations.append(Pin(pinTitle: app.nomeScuola, pinSubTitle: app.indirizzoScuola, location: location))
  29.         }
  30.         //print(scuoleLocations)
  31.         self.vistaMappa.addAnnotations(scuoleLocations)
  32.     }
  33.    
  34.     func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  35.        
  36.         if annotation is MKUserLocation {
  37.             return nil
  38.         }
  39.         let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
  40.         annotationView.image = UIImage(named: "marker")
  41.         annotationView.canShowCallout = true
  42.        
  43.         let btn = UIButton(type: .detailDisclosure)
  44.         annotationView.rightCalloutAccessoryView = btn
  45.        
  46.         return annotationView
  47.     }
  48.  
  49.    
  50.     func mapView(_ mapView: MKMapView,
  51.                  annotationView view: MKAnnotationView,
  52.                  calloutAccessoryControlTapped control: UIControl)
  53.     {
  54.         performSegue(withIdentifier: "toDettaglio", sender: self)
  55.     }
  56.  
  57.    
  58.    
  59.    
  60.     //implemento il protocollo con locationManager
  61.     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  62.        
  63.         //verifico se posso recuperare le coordinate (e in caso le recupero)
  64.         if let lat = locations.last?.coordinate.latitude, let long = locations.last?.coordinate.longitude {
  65.             print("\(lat),\(long)")
  66.         } else {
  67.             print("Nessuna coordinata")
  68.         }
  69.     }
  70.    
  71.     //implemento monitoraggio errore
  72.     func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
  73.         print(error)
  74.     }
  75.    
  76.     //estrapolo dalle coordinate il nome della città o luogo in cui l'utente si trova
  77.     func lookUpCurrentLocation(completionHandler: @escaping (CLPlacemark?) -> Void ) {
  78.        
  79.         // recupero direttamente da locationManager le ultime coordinate memorizzate
  80.         if let lastLocation = self.locationManager.location {
  81.            
  82.             //creo un'istanza di CLGeocoder per il reversing delle coordinate
  83.             let geocoder = CLGeocoder()
  84.            
  85.             // Look up the location and pass it to the completion handler
  86.             geocoder.reverseGeocodeLocation(lastLocation, completionHandler: { (placemarks, error) in
  87.                 if error == nil {
  88.                     let firstLocation = placemarks?[0]
  89.                     completionHandler(firstLocation)
  90.                 }
  91.                 else {
  92.                     // An error occurred during geocoding.
  93.                     completionHandler(nil)
  94.                 }
  95.             })
  96.         }
  97.         else {
  98.             // No location was available.
  99.             completionHandler(nil)
  100.         }
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement