Android: Skobbler, How To Limit Map View In An Area?
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'
Solution 1:
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:
publicclassMapActivityextendsActivityimplementsSKMapSurfaceListener, ... {
...
// the box inside which map operations are allowed privateSKBoundingBoxbox=newSKBoundingBox(47.087426, 8.257230, 46.874277, 8.637632);
// last region that was visible on the screen and was inside the box privateSKCoordinateRegionlastValidRegion=null;
...
@OverridepublicvoidonSurfaceCreated(SKMapViewHolder mapHolder) {
...
// position the map somewhere inside your box
mapView.centerMapOnPosition(newSKCoordinate(8.304354, 47.050253));
}
...
// checks if a given region is inside the bounding box privatebooleanisInBoundingBox(SKCoordinateRegion newRegion) {
SKBoundingBoxnewBoundingBox= mapView.getBoundingBoxForRegion(newRegion);
if (newBoundingBox.getTopLeftLatitude() > box.getTopLeftLatitude() || newBoundingBox.getBottomRightLatitude() < box.getBottomRightLatitude() ||
newBoundingBox.getTopLeftLongitude() < box.getTopLeftLongitude() || newBoundingBox.getBottomRightLongitude() > box.getBottomRightLongitude()) {
returnfalse;
}
returntrue;
}
@OverridepublicvoidonMapRegionChanged(SKCoordinateRegion mapRegion) {
booleaninBoundingBox= isInBoundingBox(mapRegion);
if (inBoundingBox) {
// if mapRegion is valid save it if (lastValidRegion == null) {
lastValidRegion = newSKCoordinateRegion(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.
Post a Comment for "Android: Skobbler, How To Limit Map View In An Area?"