问题
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.
For example: These are latitude and longitude bound:
LatLng one = new LatLng(42.0140555,-88.2131937);
LatLng two = new LatLng(40.993729,-87.6622417);
回答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);
}
来源:https://stackoverflow.com/questions/59576306/how-do-i-restrict-google-maps-api-to-a-specific-area-when-its-latitude-and-longi