@sportredwhite

Как узнать свои координаты в google maps?

Всем привет!
Подскажите плз как узнать свои координаты в google maps?
Знаю что есть CLLocationManager и его метод didUpdateLocations, но мне нужно именно через 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 и longtitude если высести через print пишет nil
  • Вопрос задан
  • 751 просмотр
Решения вопроса 1
@sportredwhite Автор вопроса
Нашёл ответ) надо использовать ещё и "import GooglePlaces"

пример кода:
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)
        }
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы