0

I have a GMSMapView and want to show current location on GMSMapView. I have added bellow line in info.plist source code -

<key>NSLocationAlwaysUsageDescription</key>
<string>Result</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Result</string>

My simulated location is San Francisco, CA, USA from xcode. Simulated location

When I load GMSMapView I get location alert. Location Alert

But current location always shown San Francisco and all map related Apps on my iPhone's current location change which is always San Francisco though now my phone location should show Dhaka, Bangladesh. What is the solution ???
I want my current location from my mobile GPS.

Community
  • 1
  • 1
  • please check this https://stackoverflow.com/questions/25449469/swift-show-current-location-and-update-location-in-a-mkmapview – Jigar Dec 11 '17 at 06:37
  • You are testing it on simulator or on actual device ? If simulator seems like you added location as San Fransisco hence location is pointing at San Fransisco test it on real device it should be fine – Sandeep Bhandari Dec 11 '17 at 06:37
  • 1
    try adding a gpx file @Ahnaf – Ashish Bahl Dec 11 '17 at 06:41

2 Answers2

1
Step 1: Import the this into your controller class 

    import CoreLocation

Step 2: Add this delegate to your class file

    class Your_controller: CLLocationManagerDelegate

Step 3: Declare this above for viewed load

    var locationManager = CLLocationManager()

Step 4: Add this code to your `viewdidload` method

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()


    if CLLocationManager.locationServicesEnabled() {
      switch (CLLocationManager.authorizationStatus()) {
        case .notDetermined, .restricted, .denied:
          print("No access")
        case .authorizedAlways, .authorizedWhenInUse:
          print("Access")
      }
    } else {
      print("Location services are not enabled")
    }

Step 5: Add this code below viewdidload method

    func locationManager(_ manager: CLLocationManager,
      didUpdateLocations locations: [CLLocation]) {

      let locationArray = locations as NSArray
      let locationObj = locationArray.lastObject as !CLLocation
      let coord = locationObj.coordinate

      lattitude = coord.latitude
      longitude = coord.longitude
      print(lattitude)
      print(longitude)
    }

Step 6: add to permission your plist file

Key - "Privacy - Location When In Use Usage Description" 

Value - "This app needs access to your location"

Step 7: run your Application on Hardware Device
Ravi Padsala
  • 126
  • 2
  • 10
  • thank you, just a minor issue let locationObj = locationArray.lastObject as !CLLocation should be let locationObj = locationArray.lastObject as! CLLocation – Hoang HUA Apr 04 '18 at 15:56
  • Thanks Ravi, +1. it's really work great:) just want to add some line in location method to add marker which show current location. let center = CLLocationCoordinate2D(latitude: locationObj.coordinate.latitude, longitude: locationObj.coordinate.longitude) let marker = GMSMarker() marker.position = center marker.title = "current location" marker.map = mapView let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: 15) self.mapView.animate(to: camera) locationManager.stopUpdatingLocation() – Chandni Jul 19 '18 at 07:15
0

in your viewDidLoad() add the following for location manager configuration:

override func viewDidLoad() {
    super.viewDidLoad()

    if (CLLocationManager.locationServicesEnabled())
    {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
}

Further implement delegates for GMSMapView:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    let location = locations.last as CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)        
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.map.setRegion(region, animated: true)
}
Amrit Sidhu
  • 1,870
  • 1
  • 18
  • 32