2

I'm new not only on Skobbler but also for Map. At this time, i'm trying to create a mapp app that show only an area.

And, what i want are:

  • User can't move out of that area.
  • Don't load any thing (title, .. )outside that area.
  • User can view and zoom in only.

I tried with bound like below but it not work:

SKBoundingBox boundingBox = new SKBoundingBox(47.087426, 8.257230, 46.874277, 8.637632);
mapView.fitBoundingBox(boundingBox, 0, 0);

Could you give some hint, please?

Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32
  • 1
    The same question, but for the android platform: http://stackoverflow.com/questions/32031325/limit-a-map-to-certain-bounds – Ando Aug 31 '15 at 06:45
  • In case someone is looking for an iOS answer to this, there you go http://stackoverflow.com/a/33792213/1658268 – SudoPlz Nov 18 '15 at 23:05

1 Answers1

2

Currently the SDK does not support limiting map operations to a bounding box.

As a workaround, whenever the map region that is visible on the screen changes (as a result of panning, zooming or rotation) and it goes outside the defined bounding box the map will switch back to the last visible region that was inside that bounding box:

public class MapActivity extends Activity implements SKMapSurfaceListener, ... { 

... 
// the box inside which map operations are allowed 
private SKBoundingBox box = new SKBoundingBox(47.087426, 8.257230, 46.874277, 8.637632); 

// last region that was visible on the screen and was inside the box 
private SKCoordinateRegion lastValidRegion = null; 

... 

@Override 
    public void onSurfaceCreated(SKMapViewHolder mapHolder) { 
... 
// position the map somewhere inside your box 
mapView.centerMapOnPosition(new SKCoordinate(8.304354, 47.050253)); 
} 

... 

// checks if a given region is inside the bounding box 
private boolean isInBoundingBox(SKCoordinateRegion newRegion) { 
        SKBoundingBox newBoundingBox = mapView.getBoundingBoxForRegion(newRegion); 
        if (newBoundingBox.getTopLeftLatitude() > box.getTopLeftLatitude() || newBoundingBox.getBottomRightLatitude() < box.getBottomRightLatitude() || 
                newBoundingBox.getTopLeftLongitude() < box.getTopLeftLongitude() || newBoundingBox.getBottomRightLongitude() > box.getBottomRightLongitude()) { 
            return false; 
        } 
        return true; 
    } 

@Override 
    public void onMapRegionChanged(SKCoordinateRegion mapRegion) { 
        boolean inBoundingBox = isInBoundingBox(mapRegion); 
        if (inBoundingBox) { 
// if mapRegion is valid save it 
            if (lastValidRegion == null) { 
                lastValidRegion = new SKCoordinateRegion(mapRegion.getCenter(), mapRegion.getZoomLevel()); 
            } else { 
                lastValidRegion.setCenter(mapRegion.getCenter()); 
                lastValidRegion.setZoomLevel(mapRegion.getZoomLevel()); 
            } 
        } else { 
// if mapRegion is invalid reposition the map inside the bounding box 
            if (lastValidRegion != null) { 
                mapView.changeMapVisibleRegion(lastValidRegion, false); 
            } 
        } 
    } 

} 

Also, if needed, limits can also be applied to zoom levels using the SKMapSettings.setZoomLimits(...) method.

SylviA
  • 1,577
  • 9
  • 13