Android Mapview -Setting Zoom Automatically Until All Itemizedoverlay's Are Visible

How to make multiple icon overlays only appear after a certain zoom level on Google Maps MapView?

I really like using the OverlayManager library for Android. It adds features to the Google Maps code, and makes a few things a lot easier. Find it here including some demo code that uses it

Option #1: If you use this, then you can use the OverlayManager's gesture listener interface for your MapActivity, to receive a callback for each zoom (in/out) event.

public class Map extends MapActivity implements OnOverlayGestureListener 
{
private boolean mShowOverlays = true;
private MapView mMapView; // assign this in onCreate()

private void setOverlayVisibility() {
boolean showOverlays = mMapView.getZoomLevel() >= 18;
if (showOverlays != mShowOverlays) {
mShowOverlays = showOverlays;
for (Overlay overlay : mMapView.getOverlays()) {
if (overlay instanceof ItemOverlay) {
((ItemOverlay)overlay).setVisible(showOverlays);
}
}
}
}

// this is the onOverlayGestureListener callback:
public boolean onZoom(ZoomEvent ze, ManagedOverlay mo) {
setOverlayVisibility();
return true;
}
}

You will have to also add your Map instance as a gesture listener with ManagedOverlay.setOnOverlayGestureListener(). See the sample code for that.

Finally, in your ItemOverlay class, you can override the draw() method, and selectively draw based on whether the overlay has been marked as visible or not. You need to add a custom visible property:

public class ItemOverlay extends ItemizedOverlay {

private boolean mVisible = true;

public void setVisible(boolean value) {
mVisible = value;
}
public boolean isVisible() {
return mVisible ;
}

@Override
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
if (mVisible) {
super.draw(canvas, mapView, shadow);
}
}
}

Option #2: Now, using the Overlay Manager library just for this one purpose might be overkill. So, another, probably simpler alternative is to create a zoom listener in the way described in this stack overflow answer. The code Kurru provides would go in your Map class. You would replace this in the answer's code:

checkMapIcons();

with the method I showed above:

setOverlayVisibility();

So, now you have two ways to "watch" the zoom level, and overriding ItemOverlay.draw() allows you to make the markers disappear whenever you like (zoom level < 18 in this example).

OSMDroid simple example required

This one worked for me:

setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);   

as did:

setTileSource(TileSourceFactory.MAPNIK);

I didn't need to have anything in the the XML

It's coming back to be now, I had to add one of these:

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>

to the manifest.xml.

I can't remember which one was necessary but if you put all 3 in, it should work.

Well here's my entire source, which I've just run on the emulator:

package com.nbt.osmdroidtest;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import android.app.Activity;
import android.os.Bundle;

public class OsmDroidTest extends Activity {
/** Called when the activity is first created. */
private MapController mapController;
private MapView mapView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(15);
GeoPoint point2 = new GeoPoint(51496994, -134733);
mapController.setCenter(point2);
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

Give it a minute or so to load, as initially it might be quite slow in building up a cache.
Those coordinates should put you over central London. If you still have problems see if there is anything illuminating in the logcat.

And the main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<org.osmdroid.views.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"

/>
</LinearLayout>


Related Topics



Leave a reply



Submit