Google地图SDK for Xcode上的当前位置更新无效 [英] current location update on Google Maps SDK for Xcode not working

查看:159
本文介绍了Google地图SDK for Xcode上的当前位置更新无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的应用程序中实施Google地图。我想要做的第一步是简单地得到:
1.获取应用程序以获得使用当前位置的权限
2.找到用户当前位置并使用蓝色圆点标记标记它。
3.更新用户当前位置并围绕地图中心。



我尝试了各种方法来获取此工作,但始终以错误或结束没有工作。最初当我模拟下面的代码时,它确实想出了请求当前位置权限和蓝点的对话框,但在下面的模拟中,这似乎已经消失,并且在模拟器中更改当前位置时(通过编辑方案并更改默认位置),新的当前位置不会更新。我有一种感觉,代码不能超越viewDidLoad。我在哪里会出错?



请问我可以就如何解决这些问题提出一些建议,并让上述几点工作(以及模拟器总是要求许可当前位置)。目前代码运行没有错误,但只显示了英国的地图。 (我已经添加NSLocationWhenInUse和NSLocationAlwaysUsage ....到info.plist中,并且我已经禁用了所有的断点,我也尝试了重置模拟器)

  import UIKit 
import GoogleMaps
$ b $ class FirstViewController:UIViewController,CLLocationManagerDelegate {

@IBOutlet weak var googlemaps:GMSMapView!
//这是从故事板到视图控制器的连接


覆盖func viewDidLoad(){
super.viewDidLoad()
让相机= 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()



$ b重写func loadView(){
super.viewDidLoad()
func locationManager(manager:CLLocationManager,didChangeAuthorizationStatus status:CLAuthorizationStatus){
//在使用应用程序时验证授予的权限
let locationManager = CLLocationManager()
让mapView = GMSMapView()


if status == .authorizedWhenInUse {


locationManager.startUpdatingLocation()
//如果授予权限 - 开始更新位置
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
//假设这是在点击中心到用户位置时添加的按钮
}





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



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

self.view = mapView


locationManager.stopUpdatingLocation()
}

}
}
}
}
pre>

预先感谢!我花了好几天的时间试图解决这个问题,但没有成功:($ / b>

解决方案

我通过更新Cocoapods和确保GoogleMaps位于我的Podfile中,然后在我的viewController中,我有以下代码完美执行。

  import UIKit 

导入GoogleMaps


类FirstViewController:UIViewController,CLLocationManagerDelegate {


@IBOutlet弱var mapView:GMSMapView!
var locationManager = CLLocationManager()
var vwGMap = GMSMapView()


重写func viewDidLoad(){
super.viewDidLoad()
let camera: GMSCameraPosition = GMSCameraPosition.camera(带有:22.300000,经度:70.783300,缩放:10.0)
vwGMap = GMSMapView.map(withFrame:self.view.frame,camera:camera)
vwGMap.camera = camera

locationManager.delegate = self
locationManager.desiredAccuracy = k CLLocationAccuracyKilometer
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!.coordin ate.longitude)
marker.map = self.vwGMap
}
}


I have been trying to implement google maps in my application. The first step what I want to do is to simply get is: 1. Get the app to ask permission to use current location 2. Find the users current location and have a blue dot marker to mark it. 3. Update and centre the map around the users current location.

I have tried various methods to get this working but always end up with an error or doesn't work. Initially when I simulated the code below, it did come up with the dialog about requesting current location permission and a blue dot but in the following simulations this seems to have disappeared and when changing my current location in simulator (by going to edit schemes and changing default location) , the new current location did not update. I have a feeling that the code isn't working beyond viewDidLoad. where am I going wrong?

Please can I get some advice on how to rectify these problems and get the above points working (as well as the simulator to always ask for permission for current location). Currently the code runs with no errors but only shows me a map of the UK. (I have already added NSLocationWhenInUse and NSLocationAlwaysUsage.... into info.plist. And I have disabled all breakpoints. I have also tried resetting simulator)

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()
                }

            }
        }
    }
}

Thanks in advance! I have spent days trying to work this out with no success :(

解决方案

I was able to solve this problem by updating Cocoapods and ensuring that GoogleMaps is in my Podfile. Then in my viewController I had the following code which executed perfectly.

    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
    }
    }

这篇关于Google地图SDK for Xcode上的当前位置更新无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆