【问题标题】:Can't find Google Maps mapview.myLocation (iOS SDK)找不到 Google 地图 mapview.myLocation(iOS SDK)
【发布时间】:2015-11-26 10:46:08
【问题描述】:

如何使用 Google Maps iOS SDK 在地图上显示用户的位置?

我尝试在self.myMapView(MKMapView) 中查找.myLocation 属性,但找不到。

谁能一步一步地解释我如何在地图上显示我的用户的位置?

【问题讨论】:

  • MKMapView 是 Apple Maps 地图视图 - Google Maps 地图视图是 GMSMapView

标签: ios google-maps cocoa-touch google-maps-sdk-ios


【解决方案1】:

为 GMSMapView 和 CLLocationManager 等创建属性

@IBOutlet 弱 var mapView: GMSMapView!

var locationManager = CLLocationManager()

var didFindMyLocation = false

并在 viewdidload 方法中为地图视图添加观察者以进行位置更新,如下所示

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 是 bool 属性,不要忘记在 Info.plist 文件中添加 'NSLocationWhenInUseUsageDescription' 键

【讨论】:

    【解决方案2】:

    此代码会将标记设置为所需的坐标。 您可以通过 CoreLocation 框架(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
    

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多