solomon solo
solomon solo

Reputation: 1

how to get user's location using google api

import UIKit
import GoogleMaps
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .AuthorizedWhenInUse{
            locationManager.startUpdatingLocation()

            mapView.myLocationEnabled = 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)


            locationManager.stopUpdatingLocation()
        }
    }


}

This is the code I have written, but I am not able get the user's location. I have added NSLocationWhenInUseUsageDescriptation in info.plist . Can somebody help me on this? I have been trying this from a long time.

I am getting error in these lines.

=================================
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true

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

==================================

Can somebody help me to fix this?

1 Value of type 'MKMapView' has no member 'myLocationEnabled' 2 Value of type 'MKMapView' has no member ‘settings' 3 Cannot assign value of type 'GMSCameraPosition!' to type 'MKMapCamera'

this are the 3 error am getting

my exact error is on @IBOutlet weak var mapView: MKMapView! is i change this @IBOutlet weak var mapView: GMSMapView! i get thread 1 error @IBOutlet weak var mapView: MKMapView! in this only map show but doesn't pin my location

and am not add comment for the answer

Upvotes: 0

Views: 912

Answers (2)

derdida
derdida

Reputation: 14904

I´ve done it that way and it works like a charm:

let locationManager = CLLocationManager()
var mapView:GMSMapView!

override func viewDidLoad() {
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in

        if(error != nil) {

            print("Error:" + error!.localizedDescription )
            return

        }

        if(placemarks!.count > 0) {

            if let pm = placemarks?.first {
                self.displayLocationInfo(pm)
            }

        } else {
            print("Error with data")
        }

    });

}

func displayLocationInfo(placemark: CLPlacemark) {
    self.locationManager.stopUpdatingLocation()

}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error with Server");
}

Upvotes: 0

LorenzOliveto
LorenzOliveto

Reputation: 7936

You are declaring an instance of MKMapView (the mapView provided by the MapKit Framework). You should use an instance of GMSMapView, the map of the Google Maps Framework, in order to use these functions.

Upvotes: 0

Related Questions