Вызов функции при изменении положения камеры - PullRequest
0 голосов
/ 27 января 2020

Я пытаюсь показать кнопку UIB, когда расстояние между положением пользователя и положением камеры превышает 1 км. Как я могу достичь этой функции?

Я пробовал func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) и func mapView(_ mapView: GMSMapView, willMove gesture: Bool), но без желаемых результатов.

Спасибо за любую дополнительную помощь!

Ответы [ 2 ]

1 голос
/ 27 января 2020

Можете попробовать

var current: CLLocation?

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

  self.current = locations.last!

}

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

    guard let currentLocation = current else { return } 

    let moved = CLLocation(latitude: position.target.latitude, longitude: position.target.longitude)

    if currentLocation.distance(from: moved) > 1000 {

        self.yourButton.isHidden = false
    } 
    else { //hide it }
}
1 голос
/ 27 января 2020

Я использую это так:

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

    let location: CLLocation = locations.last!
    print("Location: \(location)")

    let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
                                          longitude: location.coordinate.longitude,
                                          zoom: self.zoomLevel)

    if let mapView = self.mapView {

        if mapView.isHidden {
            mapView.isHidden = false
            mapView.camera = camera

        } else {
            mapView.animate(to: camera)
        }
    }
    self.calculateDistance(destiLat: Lat Coordinate, destiLongL: Long Coordinate)
}

    //This function calculates the distance between origin and destination

private func calculateDistance(destiLat: CLLocationDegrees, destiLongL: CLLocationDegrees) {

    DispatchQueue.main.async {
        let origin = CLLocation(latitude: self.locationManager.location?.coordinate.latitude ?? 0, longitude: self.locationManager.location?.coordinate.longitude ?? 0)
        let destination = CLLocation(latitude: destiLat, longitude: destiLongL)
        let distanceInMeters = origin.distance(from: destination)

        print(distanceInMeters)

        if distanceInMeters.isLessThanOrEqualTo(60) {

           //You are in range of 60 meters
           //Change the distance and show your button here
        }
    }
}

Я не знаю, лучший ли это способ, но он работает для меня.

...