2

Im trying to get a users current location to show with google maps API. I have been struggling with this issue for weeks and the code runs but doesn't update to current location when I change the location in the simulator.

My code is below and after looking how other people are doing it they all have mapView.myLocationEnabled = true whereas for me this produces an error saying 'myLocationEnabled has been renamed to 'isMylocationEnabled'. I've seen some up to date posts (from 2 months ago) and everyone is using myLocationEnabled without an error it seems, why am I getting this? could this be the reason why my current location is not updating?

import UIKit
import GoogleMaps

class FirstViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var mapView: GMSMapView!


    let locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        mapView.settings.myLocationButton = true
        mapView.settings.compassButton = true
    }
    override func viewDidAppear(_ animated: Bool) {
        locationManager.requestWhenInUseAuthorization()
    }
    func locationManager(manager: CLLocationManager , didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            locationManager.startUpdatingLocation()


            mapView.isMyLocationEnabled = true

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

            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

            let marker = GMSMarker()
            marker.title = "Current Location"
            marker.map = mapView
            locationManager.stopUpdatingLocation()
        }
    }
}
Rajat
  • 10,977
  • 3
  • 38
  • 55
Assel Kash
  • 111
  • 3
  • 17
  • Did u created a project in the google developer console? And by the way u should either update ur old post http://stackoverflow.com/questions/40318101/current-location-update-on-google-maps-sdk-for-xcode-not-working or delete it instead of creating a new one with the same issue. This https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial could be helpful as well. – LoVo Nov 24 '16 at 18:21
  • Hey, yes I have. I have my API key in my Delegate and the GoogleMaps does show, just not shows the current location. Ok, sorry about that, I'll delete the old post. That was actually the tutorial I was following initially but there is no sign of him having this same issue. I also tried this tutorial https://www.appcoda.com/google-maps-api-tutorial/ and others I found online. By the way the 'isMyLocationEnabled' is a BOOL- could that be the problem? Could it be a problem with a Frameworks? since I downloaded GoogleMaps SDK manually so maybe missed something out? – Assel Kash Nov 24 '16 at 20:01

1 Answers1

2

SO I FINALLY SOLVED THIS! Basically I updated my existing Podfile with GoogleMaps and GooglePlaces and then updated pod to ensure all the frameworks are working. Then I used this code to get my current location to work

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

}

Assel Kash
  • 111
  • 3
  • 17