【问题标题】:Google Maps API not showing my locationGoogle Maps API 未显示我的位置
【发布时间】:2015-11-15 00:24:56
【问题描述】:

我试图在GMSMapView 中显示myLocation。这是我到目前为止的代码:

class ViewController: UIViewController {

    // MARK: Properties
    @IBOutlet weak var mapView: GMSMapView!

    let locationManager = CLLocationManager()
    let stdZoom: Float = 12


    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.requestWhenInUseAuthorization()
        mapView.myLocationEnabled = true

        if let myLocation = mapView.myLocation {
            print("my location enabled");
            let update = GMSCameraUpdate.setTarget(myLocation.coordinate, zoom: stdZoom)
            mapView.moveCamera(update)
        } else {
            print("my location could not be enabled")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我按照问题here 的建议将CLLocationManager 设置为类变量,并在Xcode 的Info.plist 文件中将NSLocationWhenInUseUsageDescription 设置为字符串,但是从来没有提示允许定位服务显示在模拟器中,我得到以下控制台输出:

2015-11-14 19:17:14.752 ios-demo[34759:569776] Simulator user has requested new graphics quality: 100
my location could not be enabled
2015-11-14 19:17:15.838 ios-demo[34759:569776] Google Maps SDK for iOS version: 1.10.21020.0

有人能指出我缺少什么吗?

【问题讨论】:

    标签: ios swift google-maps xcode7 cllocationmanager


    【解决方案1】:

    我按照教程here,特别是标题为我的位置的部分。我忽略了关于func locationManager 的部分,而是将viewMap.myLocationEnabled = true 的调用放在viewDidLoad 中。本教程对observeValueForKeyPath 的覆盖似乎已经过时,需要进行一些调整,但这是我的完整代码:

    class ViewController: UIViewController, CLLocationManagerDelegate {
    
        // MARK: Properties
        @IBOutlet weak var mapView: GMSMapView!
        let locationManager = CLLocationManager()
        let stdZoom: Float = 12
        var didFindMyLocation = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.locationManager.delegate = self
            self.locationManager.requestWhenInUseAuthorization()
            self.mapView.myLocationEnabled = true
            self.mapView.settings.myLocationButton = true
    
            self.mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
            if !didFindMyLocation {
                let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
                self.mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
                self.mapView.settings.myLocationButton = true
    
                 didFindMyLocation = true
            }
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 2012-02-26
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 2013-02-15
      • 2014-07-29
      相关资源
      最近更新 更多