M
M
MarsFM2017-03-28 18:26:21
iOS
MarsFM, 2017-03-28 18:26:21

How to find your coordinates in google maps?

Hello!
Plz tell me how to find your coordinates in google maps?
I know that there is a CLLocationManager and its didUpdateLocations method, but I need it through google maps

override func viewDidLoad() {
        super.viewDidLoad()

//        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        geoCoder = CLGeocoder()
        
        mapView = GMSMapView(frame: self.mapContainer.frame)
        
        mapView.isMyLocationEnabled = true
        mapView.settings.myLocationButton = true
        
        lantitude = mapView.myLocation?.coordinate.latitude
        longtitude = mapView.myLocation?.coordinate.longitude
    
        let currentPosition = CLLocationCoordinate2D(latitude: lantitude, longitude: longtitude)
        mapView.camera = GMSCameraPosition(target: currentPosition, zoom: 15, bearing: 0, viewingAngle: 0)
    
        print("\(lantitude)")
        
        self.view.addSubview(self.mapView)
    }

lantitude and longtitude if printed out via print writes nil

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MarsFM, 2017-03-29
@sportredwhite

Found the answer) you also need to use the "import GooglePlaces"
code example:

placesClient = GMSPlacesClient()
        
        placesClient.currentPlace { (placeLikelihoodList, error) in
            if let error = error {
                print("Pick Place error: \(error.localizedDescription)")
                return
            }
            
            
            if let placeLicklihoodList = placeLikelihoodList {
                let place = placeLicklihoodList.likelihoods.first?.place
                if let place = place {
                    if self.longtitude == nil && self.lantitude == nil {
                        self.lantitude = place.coordinate.latitude
                        self.longtitude = place.coordinate.longitude
                        self.address = "\(place.formattedAddress!)"
                    } else {
                        self.geoCoder.geocodeAddressString(self.address, completionHandler: { (placemark, error) in
                            if error == nil {
                                print(error?.localizedDescription ?? "Error")
                            }
                            
                            print(self.address)
                            
                            let location = placemark?[0].location?.coordinate
                            self.lantitude = location?.latitude
                            self.longtitude = location?.longitude
                            
                            print(self.lantitude)
                            print(self.longtitude)
                            
                        })
                    }
                }
            }
    
            
            self.mapView = GMSMapView(frame: self.mapContainer.frame)
            let currentPosition = CLLocationCoordinate2D(latitude: self.lantitude, longitude: self.longtitude)
            self.mapView.camera = GMSCameraPosition(target: currentPosition, zoom: 15, bearing: 0, viewingAngle: 0)
            self.view.addSubview(self.mapView)
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question