current location update on Google Maps SDK for Xcode not working

Question

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


Show source
| xcode   | ios   | iphone   | google-maps   | currentlocation   2016-10-29 12:10 0 Answers

Answers ( 0 )

◀ Go back