0

I try to print the userLocation to a textLabel, but what I don't want is the exact longitude and latitude, I just want for example the street name of where the user's current location is or even just the tip code, do you know what I mean?

I use following code to get the users location:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    mapView.showsUserLocation = true
if CLLocationManager.locationServicesEnabled() == true {

        if CLLocationManager.authorizationStatus() == .restricted || CLLocationManager.authorizationStatus() == .denied ||  CLLocationManager.authorizationStatus() == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
        }
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    } else {
        print("PLease turn on location services or GPS")
    }
 }

 //MARK:- CLLocationManager Delegates
func locationManager(_ manager: CLLocationManager,       
 didUpdateLocations locations: [CLLocation]) {
    self.locationManager.stopUpdatingLocation()
    let region = MKCoordinateRegion(center:  
 CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, 
 longitude: locations[0].coordinate.longitude), span: 
   MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
      //self.posizione.text = "latitude: " +  
      //String(location.coordinate.latitude) + ", longitude: " + 
     //String(location.coordinate.longitude)
    self.mapView.setRegion(region, animated: true)
}

To print the location on the label I tried:

self.posizione.text = "latitude: " +  
String(location.coordinate.latitude) + ", longitude: " + 
String(location.coordinate.longitude)

but sadly the Label keeps staying blank...

ProgrammerPer
  • 1,125
  • 1
  • 11
  • 26
J. Doe
  • 285
  • 4
  • 13

1 Answers1

2

So if I understand you correctly you don't need the longitude and latitude but rather the city name and the street address where the user is currently at.

To get those, do the following:

Make your class conform to CLLocationManagerDelegate and MKMapViewDelegate. Add the required permisson in the Info.plist file (Location when in use usage description).

Just below your class declaration insert:

    let locationManager = CLLocationManager()
    var scanLocation: String?

In your viewDidLoad insert:

        //setting up location manager
    locationManager.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }

    //setting up the map view
    mapView.delegate = self
    mapView.mapType = .standard
    mapView.isZoomEnabled = true
    mapView.isScrollEnabled = true

And then call the didUpdateLocations method as follows:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let lastLocation = locations.last {
        let geoCoder = CLGeocoder()

        //this is where you deal with the mapView to display current location
        let center = CLLocationCoordinate2D(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        mapView.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()
        annotation.coordinate = manager.location!.coordinate
        annotation.title = "I know where you are"
        annotation.subtitle = "your current location"
        mapView.addAnnotation(annotation)

        //This is where you get the current street and city name
        geoCoder.reverseGeocodeLocation(lastLocation) { (placeMarks, error) in
            if error == nil {
                if let firstLocation = placeMarks?[0] {
                    self.locationManager.stopUpdatingLocation()

                    if let cityName = firstLocation.locality,
                        let street = firstLocation.thoroughfare {

                    self.scanLocation = "\(street), \(cityName)"
                    print("This is the current city name", cityName)
                    print("this is the current street address", street)

                    self.posizione.text = self.scanLocation!
                    }
                }
            }
        }
    }
}
lajosdeme
  • 2,189
  • 1
  • 11
  • 20
  • Thanks for your answer, your code seems to work perfectly fine, it prints a street name to the label, but it doesn't give me any position shown on the map, am I missing something there? – J. Doe Feb 11 '19 at 14:25
  • @J.Doe I'm sorry it wasn't clear (at least for me) from your question that you also want to display the location on MKMapView, but looking at your code a second time I see you got the mapView implemented. It's easy to do that, my above answer only requires a few additional lines of code. This SO answer will sure help you out: https://stackoverflow.com/questions/25449469/show-current-location-and-update-location-in-mkmapview-in-swift Try this and if you don't know how to do it I will edit my answer to include this too. – lajosdeme Feb 11 '19 at 15:27
  • Nah no worries hahaha I also didn't clearly asked for it, I just came on the idea of asking as it really seems like you have a clue of what you are talking about. But to come to the problem, I tried it, but get *Value of type '[CLLocation]' has no member 'coordinate'* as an Error... I also added it to the locationManagerFunction - I don't know what's the error there so it would be really nice if you would include your solution in your answer :) – J. Doe Feb 11 '19 at 16:12
  • this is awesome, even with a mark haha - thank you so so so much, you're already the man of the week – J. Doe Feb 11 '19 at 17:24
  • @J.Doe I'm glad it helped! :) – lajosdeme Feb 11 '19 at 19:18