Skip to content Skip to sidebar Skip to footer

How Do I Restrict Google Maps API To A Specific Area When Its Latitude And Longitude Bound Is Given?

I only want to display a google map of a specific area on the mobile screen. The map should not move beyond that area when there is user interaction such as zoom or moving the map.

Solution 1:

I have found the solution this way. Do this on onMapReady() function.

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //get latlong for corners for a specified area of map

    LatLng one = new LatLng(42.0140555,-88.2131937);
    LatLng two = new LatLng(40.993729,-87.6622417); 

    LatLngBounds.Builder builder = new LatLngBounds.Builder();

    //add them to builder
    builder.include(one);
    builder.include(two);

    LatLngBounds bounds = builder.build();

    //get width and height to current display screen
    int width = getResources().getDisplayMetrics().widthPixels;
    int height = getResources().getDisplayMetrics().heightPixels;

    // 20% padding
    int padding = (int) (width * 0.20);

    //set latlong bounds
    mMap.setLatLngBoundsForCameraTarget(bounds);

    //move camera to fill the bound to screen
    mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding));

    //set zoom to level to current so that you won't be able to zoom out viz. move outside bounds
    mMap.setMinZoomPreference(mMap.getCameraPosition().zoom);
}

Post a Comment for "How Do I Restrict Google Maps API To A Specific Area When Its Latitude And Longitude Bound Is Given?"