【问题标题】:current location update on Google Maps SDK for Xcode not workingGoogle Maps SDK for Xcode 上的当前位置更新不起作用
【发布时间】:2016-10-29 10:18:56
【问题描述】:

我一直在尝试在我的应用程序中实现谷歌地图。我想做的第一步就是简单地得到: 1.获取应用程序请求使用当前位置的权限 2.找到用户当前的位置,并有一个蓝点标记来标记它。 3. 更新地图并以用户当前位置为中心。

我尝试了各种方法来使其正常工作,但总是以错误或无法正常工作而告终。最初,当我模拟下面的代码时,它确实出现了关于请求当前位置许可和一个蓝点的对话框,但在下面的模拟中,这似乎已经消失了,当我在模拟器中更改我的当前位置时(通过编辑方案并更改default location) ,新的当前位置没有更新。我感觉代码在 viewDidLoad 之外无法正常工作。我哪里错了?

请我就如何纠正这些问题并让上述几点发挥作用获得一些建议(以及模拟器始终请求当前位置的许可)。目前代码运行没有错误,但只向我显示了英国的地图。 (我已经将 NSLocationWhenInUse 和 NSLocationAlwaysUsage.... 添加到 info.plist 中。并且我已经禁用了所有断点。我也尝试过重置模拟器)

import UIKit
import GoogleMaps

class FirstViewController: UIViewController,CLLocationManagerDelegate {

    @IBOutlet weak var googlemaps: GMSMapView!
    //this is the connection from the storyboard to the view controller


    override func viewDidLoad() {
        super.viewDidLoad()
        let camera = GMSCameraPosition.camera(withLatitude: 51.508742, longitude: -0.087891, zoom: 8.0)
        let viewMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        view = viewMap

        viewMap.isMyLocationEnabled = true
        let locationManager = CLLocationManager()

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()


    }

    override func loadView() {
        super.viewDidLoad()
        func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
            // verification of granted permission while app in use
            let locationManager = CLLocationManager()
            let mapView = GMSMapView()


            if status == .authorizedWhenInUse {


                locationManager.startUpdatingLocation()
                // if permission granted- starting updating location
                mapView.isMyLocationEnabled = true
                mapView.settings.myLocationButton = true
                //this is suppose to be button that is added when tapped centers to users location
            }





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



                    mapView.camera = GMSCameraPosition.camera(withTarget: location.coordinate, zoom: 20)

                    self.view = mapView


                    locationManager.stopUpdatingLocation()
                }

            }
        }
    }
}

提前致谢!我花了几天时间试图解决这个问题,但没有成功:(

【问题讨论】:

    标签: ios iphone xcode google-maps currentlocation


    【解决方案1】:

    我能够通过更新 Cocoapods 并确保 GoogleMaps 在我的 Podfile 中来解决这个问题。然后在我的 viewController 中,我有以下完美执行的代码。

        import UIKit
    
        import GoogleMaps
    
    
        class FirstViewController: UIViewController, CLLocationManagerDelegate {
    
    
    @IBOutlet weak var mapView: GMSMapView!
    var locationManager = CLLocationManager()
    var vwGMap = GMSMapView()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 22.300000, longitude: 70.783300, zoom: 10.0)
        vwGMap = GMSMapView.map(withFrame: self.view.frame, camera: camera)
        vwGMap.camera = camera
    
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        locationManager.distanceFilter = 500
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    
        self.view = vwGMap
        }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    
            if (status == CLAuthorizationStatus.authorizedWhenInUse)
    
            {
                vwGMap.isMyLocationEnabled = true
            }
        }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let newLocation = locations.last
            vwGMap.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0)
            vwGMap.settings.myLocationButton = true
            self.view = self.vwGMap
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)
        marker.map = self.vwGMap
        }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多