0

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...

Community
  • 1
  • 1
lionheart
  • 437
  • 11
  • 33

1 Answers1

2

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());
}
Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
  • This answer showed me how to calculate the max radius in a way that suits my use case (I've used the map container layout instead of _map). – lionheart Sep 22 '16 at 07:53
  • Sorry, I use too much details. Main point was to get half of diagonal of view and it will be maxRadius. :) Glad you got this point. – Michael Spitsin Sep 22 '16 at 07:57