【问题标题】:Google Maps iOS myLocation谷歌地图 iOS myLocation
【发布时间】:2017-01-18 17:10:43
【问题描述】:

我在 Google 地图的 myLocation 属性上苦苦挣扎,我总是得到 nil,不知道为什么。在我的 ViewDidLoad 中,我设置了以下内容

map.myLocationEnabled = true

在用户想要获取他/她的位置时调用的函数中,我运行这个:

print(map.myLocation)

我第一次知道它可能没有位置,但我不应该在一段时间后获得位置吗?

【问题讨论】:

    标签: ios swift google-maps gps


    【解决方案1】:

    我也有同样的想法,但您实际上需要首先通过 Apple 的 CLLocationManager api 获取用户的位置。 import CoreLocation 并让你的 VC 遵守 CLLocationManagerDelegate 并使用 didUpdateLocations 方法获取用户的当前位置,然后将其反映到 GMaps。

    import UIKit
    import GoogleMaps
    import CoreLocation
    
    class MapVC: UIViewController
    {
        @IBOutlet weak var googleMap: GMSMapView!
    
        var locationManager: CLLocationManager = CLLocationManager()
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
        }
    
        override func didReceiveMemoryWarning()
        {
            super.didReceiveMemoryWarning()
        }
    }
    
    extension MapVC: CLLocationManagerDelegate
    {
        func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
        {
            switch status
            {
            case .AuthorizedAlways:
                print("Location AuthorizedAlways")
                googleMap.myLocationEnabled = true
                locationManager.startUpdatingLocation()
    
            case .AuthorizedWhenInUse:
                print("Location AuthorizedWhenInUse")
                googleMap.myLocationEnabled = true
                locationManager.startUpdatingLocation()
    
            case .Denied:
                print("Location Denied")
                googleMap.myLocationEnabled = false
                locationManager.stopUpdatingLocation()
    
            case .NotDetermined:
                print("Location NotDetermined")
                googleMap.myLocationEnabled = false
                locationManager.stopUpdatingLocation()
    
            case .Restricted:
                print("Location Restricted")
                googleMap.myLocationEnabled = false
                locationManager.stopUpdatingLocation()
            }
        }
    
        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
        {
            if locations.count > 0
            {
                googleMap.camera = GMSCameraPosition.cameraWithTarget((locations.last?.coordinate)!, zoom: 10.0)
                googleMap.settings.myLocationButton = true
            }
        }
    }
    

    【讨论】:

    • 哦,所以实际上是 CLLocationsmanager 保持地图的 myLocation 更新?通过每秒更新一次?是否有可能限制更新位置的频率?
    • Yap.. 调用时,didUpdateLocations 方法会触发对 Apple 服务器的请求,不仅请求您当前的位置,还请求有关它的不同详细信息,例如 postalCodeadministrativeArea 等。现在除非否则,在locationManager.startUpdatingLocation() 执行一次的那一刻,它将继续向Apple 的服务器发出请求。以什么速度?我也不知道。您的情况我建议创建一个计时器并将其目标设置为 locationManager 然后执行 startUpdatingLocaton 作为其选择器然后停止在 didUpdateLocations 内更新
    • 像这样:let timer = NSTimer();用于失效目的的全局变量。 timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self.locationManager, selector: #selector(self.startUpdatingLocation), userInfo: nil, repeats: true);这将每 60 秒更新一次用户位置然后在设置 GMaps 相机内容后立即在您的 didUpdateLocations 方法中添加 locationManager.stopUpdatingLocation()。由于我们是非常细心的开发人员并且不希望计时器闲置,请在您的 viewWillDisappearviewDidDisappear 中添加“timer.invalidate()”
    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2011-06-06
    • 2011-12-26
    相关资源
    最近更新 更多