Search code examples
iosswiftgoogle-mapsgoogle-maps-sdk-ios

Cant get current location (GPS) on google maps ios sdk


My goal is to move the camera a user's current location, but for somehow it keeps showing the general map, I have tried many things but seems like it wont move to the user's current location

Screenshot

enter image description here

Current code

import UIKit
import GoogleMaps


class ParcelViewController: UIViewController, GMSMapViewDelegate {


    @IBOutlet var mapView: GMSMapView!

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        mapView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


// Mark: -CLLocationManagerDelegate
extension ParcelViewController: CLLocationManagerDelegate {

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .AuthorizedWhenInUse {
            locationManager.startUpdatingLocation()

            mapView.myLocationEnabled = true
            mapView.settings.myLocationButton = true
        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {
            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

            locationManager.stopUpdatingLocation()

        }
    }
}

Solution

  • Did you add NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription (in your case it's this one) keys in your .plist file. Because if the alert view asking for authorization is not showing this might be the issue.