Reputation: 314
I want to show only a specific area using Google Maps Activity in Android Studio, and it will tell if the user is out of bounds/range.
example for Specific Area, map link is from How to highlight an area on Google Maps Without Javascript?
How can i do this?
Upvotes: 0
Views: 1844
Reputation: 3540
what you are trying to achieve is called "locked extent". In Android this cannot be done automatically or directly, but you have to do some "tricks".
You have two options: Lock by whole extent (an area MUST Be completely inside the visible extent) Lock by map center (the center of the map MUST be inside a specific extent).
In order to do this, you have to use the "onCameraChangeListener" of the googleMap object: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnCameraChangeListener
map.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
checkBounds();
}
});
where check bounds is either a check for an area or a part of it:
public void checkBounds() {
// allowedbounds must be generated only once, in onCreate, because it will be a fixed area
LatLng actualCenter = map.getProjection().getVisibleRegion().getCenter();
if (allowedBounds.contains(actualCenter)){
return;
}else{
//If bounds are not respected, go back to allowed bounds map.animateCamera(CameraUpdateFactory.newLatLngBounds(allowedBounds)); } }
if you want to check if full bound is respected, you have to write your own method for the check and change the if condition. An hint for that method can be found here: https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/geometry/Bounds.java
Upvotes: 1