Change Camera Zoom based on Radius Google Maps iOS SDK

前端 未结 6 825
既然无缘
既然无缘 2021-02-01 09:53

I am working on an app that displays certain markers based on a radius around your current location. The radius is between 100 - 5000 meters. I change the radius with an U

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 10:21

    Here's a simpler solution for getting the bounds of a GMSCircle. It doesn't rely on MapKit and avoids the two calls that change the camera position (moveCamera and animateToLocation)

    import GoogleMaps
    
    extension GMSCircle {
        func bounds () -> GMSCoordinateBounds {
            func locationMinMax(_ positive : Bool) -> CLLocationCoordinate2D {
                let sign: Double = positive ? 1 : -1
                let dx = sign * self.radius  / 6378000 * (180 / .pi)
                let lat = position.latitude + dx
                let lon = position.longitude + dx / cos(position.latitude * .pi / 180)
                return CLLocationCoordinate2D(latitude: lat, longitude: lon)
            }
    
            return GMSCoordinateBounds(coordinate: locationMinMax(true),
                                   coordinate: locationMinMax(false))
        }
    }
    

    After adding this file to your project, all you have to do is:

    let update = GMSCameraUpdate.fit(myCircle.bounds())
    myMap.animate(with: update)
    

    where myCircle and myMap are replaced by the actual circle and map.

提交回复
热议问题