How to "Show my current location on google maps, when I open the ViewController?" in Swift? How to "Show my current location on google maps, when I open the ViewController?" in Swift? ios ios

How to "Show my current location on google maps, when I open the ViewController?" in Swift?


For Swift 3.x solution, please check this Answer

First all of you have to enter a key in Info.plist fileNSLocationWhenInUseUsageDescription

enter image description here

After adding this key just make a CLLocationManager variable and do the following

@IBOutlet weak var mapView: GMSMapView!var locationManager = CLLocationManager()class YourControllerClass: UIViewController,CLLocationManagerDelegate {    //Your map initiation code     let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)    self.view = mapView    self.mapView?.myLocationEnabled = true    //Location Manager code to fetch current location    self.locationManager.delegate = self    self.locationManager.startUpdatingLocation()}//Location Manager delegatesfunc locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {    let location = locations.last    let camera = GMSCameraPosition.cameraWithLatitude((location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)    self.mapView?.animateToCameraPosition(camera)    //Finally stop updating location otherwise it will come again and again in this delegate    self.locationManager.stopUpdatingLocation()}

When you run the code you will get a pop up of Allow and Don't Allow for location. Just click on Allow and you will see your current location.

Make sure to do this on a device rather than simulator. If you are using simulator, you have to choose some custom location and then only you will be able to see the blue dot.

enter image description here


Use this code,

You miss the addObserver method and some content,

viewDidLoad:

mapView.settings.compassButton = YES;mapView.settings.myLocationButton = YES;mapView.addObserver(self, forKeyPath: "myLocation", options: .New, context: nil)dispatch_async(dispatch_get_main_queue(), ^{    mapView.myLocationEnabled = YES;  });

Observer Method:

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {    if change[NSKeyValueChangeOldKey] == nil {        let location = change[NSKeyValueChangeNewKey] as CLLocation        gmsMap.camera = GMSCameraPosition.cameraWithTarget(location.coordinate, zoom: 16)    }}

hope its helpful


  • first add the following to your info.plist

    1. NSLocationWhenInUseUsageDescription
    2. LSApplicationQueriesSchemes (of type array and add two items to this array item 0 : googlechromes ,item 1 : comgooglemaps
  • second go to https://developers.google.com/maps/documentation/ios-sdk/start and follow the steps till step 5

  • last thing to do after you set up every thing is to go to your ViewController and paste the following

    import UIKitimport GoogleMapsclass ViewController: UIViewController,CLLocationManagerDelegate {    //Outlets    @IBOutlet var MapView: GMSMapView!    //Variables    var locationManager = CLLocationManager()    override func viewDidLoad() {        super.viewDidLoad()        initializeTheLocationManager()        self.MapView.isMyLocationEnabled = true    }    func initializeTheLocationManager() {        locationManager.delegate = self        locationManager.requestWhenInUseAuthorization()        locationManager.startUpdatingLocation()    }    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {        var location = locationManager.location?.coordinate        cameraMoveToLocation(toLocation: location)    }    func cameraMoveToLocation(toLocation: CLLocationCoordinate2D?) {        if toLocation != nil {            MapView.camera = GMSCameraPosition.camera(withTarget: toLocation!, zoom: 15)        }    }}

( don't forget to add a view in the storyboard and connect it to the MapViw)

now you can build and run to see your current location on the google map just like when you open the Google Map App

enjoy coding :)