Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find zoom level based on circle draw on map

I am drawing the circle on map with specifying the radius and it'll draw the circle successfully. But when I change the size of circle using seekbar I need to feet the circle in screen and zoom the map of that level, I have not idea about this, need your guideline thank you.

like image 595
Pratik Avatar asked Jul 03 '12 11:07

Pratik


2 Answers

We can also get the zoom level for map from the drawn circle

Circle circle = googleMap.addCircle(circleOptions);

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                        circleOptions.getCenter(), getZoomLevel(circle)

// Methode for zoomlevel

public int getZoomLevel(Circle circle) {
    int zoomLevel = 11;
    if (circle != null) {
        double radius = circle.getRadius() + circle.getRadius() / 2;
        double scale = radius / 500;
        zoomLevel = (int) (16 - Math.log(scale) / Math.log(2));
    }
    return zoomLevel;
}
like image 165
Anand Tiwari Avatar answered Sep 29 '22 13:09

Anand Tiwari


build.gradle:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.4+'
}

public static LatLngBounds getLatLngBoundsFromCircle(Circle circle){
    if(circle != null){
        return new LatLngBounds.Builder()
                .include(SphericalUtil.computeOffset(circle.getCenter(), circle.getRadius() * Math.sqrt(2), 45))
                .include(SphericalUtil.computeOffset(circle.getCenter(), circle.getRadius() * Math.sqrt(2), 225))
                .build();
    }
    return null;
}

map.animateCamera( CameraUpdateFactory
    .newLatLngBounds(MapUtils.getLatLngBoundsFromCircle(mapCircle),20) );
like image 20
Shimon Doodkin Avatar answered Sep 29 '22 12:09

Shimon Doodkin