Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }
like image 442
Anton Avatar asked Aug 11 '15 13:08

Anton


2 Answers

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)
    }
like image 190
Anton Avatar answered Sep 25 '22 19:09

Anton


Updated Antons anwser to Swift 3 and as an extension of GMSMapView

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

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

    func getRadius() -> CLLocationDistance {
        let centerCoordinate = getCenterCoordinate()
        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))
        return round(radius)
    }
}

Calling self.map.getRadius() would return the radius

like image 29
Allreadyhome Avatar answered Sep 26 '22 19:09

Allreadyhome