简体   繁体   中英

Can't find Google Maps mapview.myLocation (iOS SDK)

How can I show the user's location on the map with Google Maps iOS SDK?

I try to find .myLocation property in self.myMapView(MKMapView) but I can't find it.

Can anyone explain me step by step how to show my user's location on the map?

Create properties for GMSMapView and CLLocationManager like

@IBOutlet weak var mapView: GMSMapView!

var locationManager = CLLocationManager()

var didFindMyLocation = false

and in the viewdidload method add observer for map view for location update like below

override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

        let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(48.857165, longitude: 2.354613, zoom: 2.0)
        mapView.camera = camera
        mapView.delegate = self

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

    }

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

            didFindMyLocation = true
        }
    }

// MARK: - CLLocationManagerDelegate

extension MapViewController: CLLocationManagerDelegate {
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == CLAuthorizationStatus.AuthorizedWhenInUse {
            mapView.myLocationEnabled = true
        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {
            mapView.myLocationEnabled = true
            mapView.settings.myLocationButton = true

            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

            locationManager.stopUpdatingLocation()
        }

    }
}

didFindMyLocation is the bool property, and also don't forget to add the 'NSLocationWhenInUseUsageDescription' key to Info.plist file

在此处输入图片说明

This code will set marker to desired coordinated. Current location coordinates you can get via CoreLocation framework(CLLocationManagerDelegate)

self.mapView.camera = GMSCameraPosition.cameraWithLatitude(latitude, longitude: longitude, zoom: 6)

let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(latitude, longitude)
marker.title =   "Marker title"
marker.map = self.mapView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM