Anton Anton - 29 days ago 21
iOS Question

Google Maps SDK iOS - Calculate radius according zoom Level

I need calculate radius to show markers on the map according camera zoom level. Right now I've got southWestCorner and my location that is center of my MapView. I need zoom out and calculate new radius when zoom changed.

Does anyone know how to get it from data I have?

My code is here:

func mapView(mapView: GMSMapView!, idleAtCameraPosition position: GMSCameraPosition!) {

println("latitude: \(position.target.latitude) longtitude: \(position.target.longitude)")

var visibleRegion = mapView.projection.visibleRegion()
var cameraZoom = mapView.camera.zoom
var bounds = GMSCoordinateBounds(region: visibleRegion)
var southWestCorner = bounds.southWest

}

Answer

Ok, I have found good answer for my question. Maybe it could be helpful to anyone else. according this article

To get radius should use next example (all functions translated to swift):

// calculate radius
    func getCenterCoordinate() -> CLLocationCoordinate2D {
        var centerPoint = self.mapView.center
        var centerCoordinate = self.mapView.projection.coordinateForPoint(centerPoint)
        return centerCoordinate
    }

    func getTopCenterCoordinate() -> CLLocationCoordinate2D {
        // to get coordinate from CGPoint of your map
        var topCenterCoor = self.mapView.convertPoint(CGPointMake(self.mapView.frame.size.width / 2.0, 0), fromView: self.mapView)
        var point = self.mapView.projection.coordinateForPoint(topCenterCoor)
        return point
    }

    func getRadius() -> CLLocationDistance {

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

        var radius = CLLocationDistance(centerLocation.distanceFromLocation(topCenterLocation))

        return round(radius)
    }