I am using google sdk maps of iOS (Swift).
Does anyone know how to "Show current location on Google maps when I open ViewController"?
In fact, this is similar to the Google Maps application. When you open Google Maps, a blue dot will show your current location. You do not need to click "myLocationButton" first.
So this is the code:
import UIKit import CoreLocation import GoogleMaps class GoogleMapsViewer: UIViewController { @IBOutlet weak var mapView: GMSMapView! let locationManager = CLLocationManager() let didFindMyLocation = false override func viewDidLoad() { super.viewDidLoad() let camera = GMSCameraPosition.cameraWithLatitude(23.931735,longitude: 121.082711, zoom: 7) let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera) mapView.myLocationEnabled = true self.view = mapView // GOOGLE MAPS SDK: BORDER let mapInsets = UIEdgeInsets(top: 80.0, left: 0.0, bottom: 45.0, right: 0.0) mapView.padding = mapInsets locationManager.distanceFilter = 100 locationManager.delegate = self locationManager.requestWhenInUseAuthorization() // GOOGLE MAPS SDK: COMPASS mapView.settings.compassButton = true // GOOGLE MAPS SDK: USER LOCATION mapView.myLocationEnabled = true mapView.settings.myLocationButton = true } } // MARK: - CLLocationManagerDelegate extension GoogleMapsViewer: 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: 20, bearing: 0, viewingAngle: 0) locationManager.stopUpdatingLocation() } } }
Anyone help? Thank you very much!
source share