-1

I am working on a project based on Google Maps API. I want to fetch the user's current location and address using Google API. I have used location manager to get address of user by using the user's current latitude and longitude. It gives me the address when I set the location of my simulator to San Francisco. Here's my Code:

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
// Here you can check whether you have allowed the permission or not.

if CLLocationManager.locationServicesEnabled()
{
    switch(CLLocationManager.authorizationStatus())
    {
    case .authorizedAlways, .authorizedWhenInUse:
        print("Authorize.")
        let latitude: CLLocationDegrees = (coordinate.latitude)
        let longitude: CLLocationDegrees = (coordinate.longitude)
        print(coordinate.latitude,coordinate.longitude)

        let location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!
        CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
            if error != nil {

                let alertController = UIAlertController(title: "Current Location", message: "\(error)", preferredStyle: .alert)
                let saveAction = UIAlertAction(title: "Ok", style: .default) { [unowned self] action in
                    self.dismiss(animated: true, completion: nil)

                    return

                }

                alertController.addAction(saveAction)

                self.present(alertController, animated: true, completion: nil)
                return

            }else if let country = placemarks?.first?.country,
                let city = placemarks?.first?.locality,

                let area = placemarks?.first?.subLocality,
                let street =  placemarks?.first?.thoroughfare,
                let number =  placemarks?.first?.subThoroughfare{
                print(country)
                print(city)
                print(area)
                print(street)
                print(number)

                let locationString = "\(street ?? "")-" + "\(number ?? "") ," + "\(area ?? "")"
                print(locationString)
                 UserDefaults.standard.set(locationString, forKey: "Address")
                let alertController = UIAlertController(title: "Current Location", message: "\(locationString)", preferredStyle: .alert)
                let saveAction = UIAlertAction(title: "Ok", style: .default) { [unowned self] action in
                    self.dismiss(animated: true, completion: nil)

                }


                alertController.addAction(saveAction)

                self.present(alertController, animated: true, completion: nil)
                }



        })
        break

    case .notDetermined:
        print("Not determined.")
        break

    case .restricted:
        print("Restricted.")
        break

    case .denied:
        print("Denied.")
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Hasnain ahmad
  • 301
  • 1
  • 10
  • 33
  • Try this ... https://stackoverflow.com/questions/16647996/get-location-name-from-latitude-longitude-in-ios/51797299#51797299 – Naresh Feb 21 '19 at 12:56
  • 1
    Possible duplicate of [How to geocode address by google maps iOS API?](https://stackoverflow.com/questions/40971633/how-to-geocode-address-by-google-maps-ios-api) – Shahzaib Qureshi Feb 21 '19 at 12:58
  • 1
    Firstly thanks for quick response, thank you very much. secondly my codes works on San Francisco lat long. but i want to get address on country like Pakistan. – Hasnain ahmad Feb 21 '19 at 13:02
  • Some countries just have better data than others. You could try using the Google Maps geocoder instead of Apple's `CLGeocoder` and you might get better results if Google data is more accurate in your country. – EmilioPelaez Feb 21 '19 at 15:30
  • Issue resolved thanks alot :) – Hasnain ahmad Feb 22 '19 at 06:08

1 Answers1

1

If you are running on simulators, you need to edit your scheme which is probably why you are always getting a San Francisco address, go to Product -> Scheme -> Edit Scheme, then in the window that opens select the "Options" tab, in the first options you'll see that "Default location" is set to San Francisco, just change that to "None" and try again

Samuel Chavez
  • 768
  • 9
  • 12
  • 1
    i have followed your steps but same result. – Hasnain ahmad Feb 22 '19 at 06:09
  • did you clean your build after setting a different location? maybe you need to delete an reinstall the app in your device, but also delete the derived data from your project before so it's a clean installation – Samuel Chavez Feb 22 '19 at 16:47