Beiträge getaggt mit scroll

Limit Scrolling of the map

The code still creates a Exception when zooming Out directly at a border!

Hey everyone, Project is almost finished and slowly i have more time again to write some stuff.

Ok since i was asked to write something about scrolling in osmdroid i had a (quick) look inside mapview and what is happening when the map gets scrolled.

The bad news is: I didn’t see any check for scrolling borders. So i guess osmdroid does not support the limitation of scrolling

The good news is: This shouldn’t be to complicated to realize by ourself.

First we need to set up our Variables in the head of our class:


/* fields for map scrolling limis */
 private int scrollLimitNorthE6;
 private int scrollLimitEastE6;
 private int scrollLimitSouthE6;
 private int scrollLimitWestE6;
 private int lastVaildX;
 private int lastValidY;
<pre>

Next we initialize them (for example in onCreate):

</pre>
scrollLimitEastE6 = 19863300;
 scrollLimitNorthE6 = 51471100;
 scrollLimitWestE6 = 9310900;
 scrollLimitSouthE6 = 44789600;
<pre>

and last but not least we build in the code to watch the limits:


</pre>
mapView.setMapListener(new MapListener() {

 @Override
 public boolean onZoom(ZoomEvent arg0) {
 return false;
 }

 @Override
 public boolean onScroll(ScrollEvent arg0) {

/* Log.i("CartoApp", mapView.getMapCenter().getLatitudeE6() + " / " + mapView.getMapCenter().getLongitudeE6());
 Log.i("CartoApp", "Lat North: " + mapView.getBoundingBox().getLatNorthE6());
 Log.i("CartoApp", "Lon East: " + mapView.getBoundingBox().getLonEastE6());
 Log.i("CartoApp", "Lat South: " + mapView.getBoundingBox().getLatSouthE6());
 Log.i("CartoApp", "Lon West: " + mapView.getBoundingBox().getLonWestE6()); */
 if (mapView.getBoundingBox().getLatNorthE6() >= scrollLimitNorthE6 ||
 mapView.getBoundingBox().getLonEastE6() >= scrollLimitEastE6 ||
 mapView.getBoundingBox().getLatSouthE6() <= scrollLimitSouthE6 ||
 mapView.getBoundingBox().getLonWestE6() <= scrollLimitWestE6) {
 mapView.scrollTo(lastVaildX, lastValidY);
 } else {
 lastVaildX = arg0.getX();
 lastValidY = arg0.getY();
 }

 return false;
 }
 });
<pre>

To explain it in few words: With every scroll of the map we check if some border is at or over our scrolling limits. if this is the case we „undo“ the last step and get back to the last position we were before we got outside the limits. If we are inside all our limits we just update the lastKnownPosition so that we can back there again when we reach a border.
Hope this helps.
greetings
Daniel

Advertisements

, , , , , , ,

2 Kommentare