current location update on Google Maps SDK for Xcode not working

痴心易碎 提交于 2019-12-12 03:46:18

问题


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 :(


回答1:


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


来源:https://stackoverflow.com/questions/40318101/current-location-update-on-google-maps-sdk-for-xcode-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!