lionheart
lionheart

Reputation: 437

change radius by zoom level

I wish to calculate radius in meters or km according to the zoom level.

I've found this formula - which gives me the roughly calculated meters per pixel: https://groups.google.com/forum/#!topic/google-maps-js-api-v3/hDRO4oHVSeM

Also, these links assisted me in understanding what I was trying to accomplish exactly: google map API zoom range How to get center of map for v2 android maps?

and this is the implementation:

double calculateScale(float zoomLevel, LatLng centerPos){
    double meters_per_pixel = 156543.03392 * Math.cos(centerPos.latitude * Math.PI / 180) / Math.pow(2, zoomLevel);

    return meters_per_pixel;
}

and this is how I listen to the zoom level changing:

        _map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
        @Override
        public void onCameraIdle() {
            Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Current Zoom : " + _map.getCameraPosition().zoom);
            Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Center Lat : " + _map.getCameraPosition().target.latitude +
                    ", Center Long : " + _map.getCameraPosition().target.longitude);
        }}

Now, whenever the user changes zoom level, I wish to determine what is the maximum radius that can be displayed within the map view...

Upvotes: 0

Views: 1857

Answers (1)

Mikhail Spitsin
Mikhail Spitsin

Reputation: 2608

You can use next snippet:

_map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
    @Override
    public void onCameraIdle() {
        Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Current Zoom : " + _map.getCameraPosition().zoom);
        Log.d(Consts.TAGS.FRAG_MAIN_MAP,"Center Lat : " + _map.getCameraPosition().target.latitude +
                ", Center Long : " + _map.getCameraPosition().target.longitude);

        float zoom = _map.getCameraPosition().zoom;
        LatLng position = _map.getCameraPosition().target;
        double maxRadius = calculateScale(zoom, position) * mapViewDiagonal() / 2;
    }
}

private mapViewDiagonal() {
    return Math.sqrt(_map.getWidth() * _map.getWidth() + _map.getHeight() * _map.getHeight());
}

Upvotes: 2

Related Questions