Search code examples
iosiphoneswiftcocoa-touchgoogle-maps-sdk-ios

How to show user location on Google Maps iOS SDK


I want to present the blue dot on the map that represents the user's location.

I've tried to do it like that:

override func viewWillAppear(animated: Bool) {
    self.mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}

deinit {
    self.mapView.removeObserver(self, forKeyPath: "myLocation")
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath == "myLocation" {
        self.mapView.animateToLocation((object?.myLocation!!.coordinate)!)
        self.mapView.animateToZoom(17)
    }
}

but observeValueForKeyPath(keyPath: ofObject: change: context:) doesn't called and I can't understand why.

Thank you very much!


Solution

  • You can do this with following line of code. This will bring up the google's default blue dot on users current location.

    self.mapView.myLocationEnabled = true