Google Map radius to zoom level

耗尽温柔 提交于 2021-01-28 12:50:21

问题


I have a map with a custom UIView on top of it. I'm calculating the radius of the circle of according to the zoom level.

func getCenterCoordinate() -> CLLocationCoordinate2D {
    let centerPoint = self.mapView.center
    let centerCoordinate = self.mapView.projection.coordinate(for: centerPoint)
    return centerCoordinate
}

func getTopCenterCoordinate() -> CLLocationCoordinate2D {
    // to get coordinate from CGPoint of your map
    let topCenterCoor = self.mapView.convert(CGPoint(x:self.circleView.frame.size.width / 2.0, y:0), from: self.circleView)
    let point = self.mapView.projection.coordinate(for: topCenterCoor)
    return point
}

func getRadius() -> CLLocationDistance {

    let centerCoordinate = getCenterCoordinate()
    // init center location from center coordinate
    let centerLocation = CLLocation(latitude: centerCoordinate.latitude, longitude: centerCoordinate.longitude)
    let topCenterCoordinate = self.getTopCenterCoordinate()
    let topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude)

    let radius = CLLocationDistance(centerLocation.distance(from: topCenterLocation)) / 1000
    print(radius)
    return round(radius)
}

Now, I want to zoom the map according to a given radius? How could I do that ?


回答1:


You can create an extension in you Class:-

extension GMSCircle {

    func bounds() -> GMSCoordinateBounds {

        func locationMinMax(positive : Bool) -> CLLocationCoordinate2D {

            let sign:Double = positive ? 1 : -1
            let dx  = sign * self.radius  / 6378000 * (180/Double.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(positive: true), 
                                   coordinate: locationMinMax(positive: false))
    }

}

Local Variable

var cirlce: GMSCircle!
var circleCenter = CLLocationCoordinate2D()

Now you can use this bound to your circle zoom.

@IBAction func circleZoom(_ sender: Any) {

    // Now Map Zoom with Circle size.
    let update = GMSCameraUpdate.fit(cirlce.bounds())
    self.googleMaps.animate(with: update)
}

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {

    let customInfoWindow = Bundle.main.loadNibNamed("Subview", 
                                                    owner: self, 
                                                    options: nil)?[0] as! CustomInfoWindow

    customInfoWindow.title.text = marker.title!
    customInfoWindow.button.tag = 0
    customInfoWindow.button.addTarget(self, 
                                      action: #selector(YourViewController.buttonpush(sender:)), 
                                      for: .touchUpInside)

    return customInfoWindow
}

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

    let location = locations.last
    let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, 
                                          longitude: (location?.coordinate.longitude)!, 
                                          zoom: zoom)

    self.googleMaps?.animate(to: camera)
    self.locationManager.stopUpdatingLocation() 
}

Definitely, it Will Help you. I am using this with Slider on Map. Sample Screen Shots.First Zoom In and Second Zoom Out.

Here you can find more Details Regarding Circle Creation and Map Zoom in Swift and Objective C




回答2:


Pick any coordinate on the perimeter of the circle, and set the map's zoom or camera's zoom to the bounds of that coordinate, adding some padding ofcourse. Now for this logic, if your map is user enabled to be zoomed, you again have to recalculate everytime your map's camera is moveid or recentered.

You can use this method to do so : mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))

Here bounds is a GMSCoordinateBounds object.

Hope this helps



来源:https://stackoverflow.com/questions/45733492/google-map-radius-to-zoom-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!