1

I am attempting to display myLocation in a GMSMapView. Here is the code I have so far:

class ViewController: UIViewController {

    // MARK: Properties
    @IBOutlet weak var mapView: GMSMapView!

    let locationManager = CLLocationManager()
    let stdZoom: Float = 12


    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.requestWhenInUseAuthorization()
        mapView.myLocationEnabled = true

        if let myLocation = mapView.myLocation {
            print("my location enabled");
            let update = GMSCameraUpdate.setTarget(myLocation.coordinate, zoom: stdZoom)
            mapView.moveCamera(update)
        } else {
            print("my location could not be enabled")
        }
    }

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

I followed the advice of the question here to make the CLLocationManager a class variable, and I set NSLocationWhenInUseUsageDescription to a String in my Info.plist file in Xcode, but the prompt to allow location services is never shown in the simulator and I am getting the following console output:

2015-11-14 19:17:14.752 ios-demo[34759:569776] Simulator user has requested new graphics quality: 100
my location could not be enabled
2015-11-14 19:17:15.838 ios-demo[34759:569776] Google Maps SDK for iOS version: 1.10.21020.0

Can someone point out what I am missing?

Community
  • 1
  • 1
scottysseus
  • 1,922
  • 3
  • 25
  • 50

1 Answers1

1

I followed the tutorial here, specifically the part titled My Location. I ignored the part about func locationManager and instead put the call for viewMap.myLocationEnabled = true in viewDidLoad. The tutorial's overriding of observeValueForKeyPath seems to be outdated, which required some tweaking but here is my full code:

class ViewController: UIViewController, CLLocationManagerDelegate {

    // MARK: Properties
    @IBOutlet weak var mapView: GMSMapView!
    let locationManager = CLLocationManager()
    let stdZoom: Float = 12
    var didFindMyLocation = false

    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.delegate = self
        self.locationManager.requestWhenInUseAuthorization()
        self.mapView.myLocationEnabled = true
        self.mapView.settings.myLocationButton = true

        self.mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)

    }

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

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if !didFindMyLocation {
            let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
            self.mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
            self.mapView.settings.myLocationButton = true

             didFindMyLocation = true
        }
    }


}
scottysseus
  • 1,922
  • 3
  • 25
  • 50