Suheyb
Suheyb

Reputation: 31

Google Maps Android Set Visible Bounds

Firstly I am sorry about my English. I try to set exact visibile bounds in google maps android api. For example map will only show America's region and user can't scroll map to Canada. When you look to link you can understand what I try to do.I think I can do it with CameraChangeListener and getting visible regions but I couldn't implement algorithm. Can you help me ? http://sehirharitasi.ibb.gov.tr/

Upvotes: 0

Views: 1916

Answers (1)

N Dorigatti
N Dorigatti

Reputation: 3540

It is not that easy and good as your link but it's something that can work:

//When you setup the map:
 map.setOnCameraChangeListener(new OnCameraChangeListener() {
     @Override
     public void onCameraChange(CameraPosition cameraPosition) {
         checkBounds();
     }
 });

And the CheckBounds function can either check if the visible area is inside the allowed one:

public void checkBounds() {
  //non ricostruire allowedbounds ogni volta, sono sempre gli stessi, fatti il build nell'onCreate
  LatLngBounds actualVisibleBounds = map.getProjection().getVisibleRegion().latLngBounds;
  if (allowedBounds.contains(actualVisibleBounds)){
    return
  }else{
    map.animateCamera(CameraUpdateFactory.newLatLngBounds(allowedBounds));
  }
}

Or you can use the center of the viewport and check if is inside the allowed area (to allow bounding zooms)

public void checkBounds() {
                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                builder.include(northeast);
                builder.include(southwest);
                final LatLngBounds allowedBounds = builder.build();

                LatLngBounds centro = map.getProjection().getVisibleRegion().latLngBounds;

                if (allowedBounds.contains(centro.getCenter()))
                        return;
                else {
                        map.animateCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, zoomLevel));
                }                
        }

Upvotes: 1

Related Questions