I am using OSMdroid with Offline MBTiles. I need to be able add users to select multiple MBtiles from a ListView
. ListView
windows is easy. However, I need help getting different MBtiles loaded based on MBtiles selected from ListView
. My MBTiles are loaded from SDCard.
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RelativeLayout;
public class OfflineMapDemoActivity extends Activity {
private String MapName;
public String getMapName(){
return MapName;
}
public void setMapName(String s){
MapName = s;
}
// layout
private RelativeLayout mapLayout;
// MapView
private MapView mapView;
private MapController mapController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// init Layout
setContentView(R.layout.main);
this.mapLayout = (RelativeLayout) findViewById(R.id.mapLayout);
// init Offline Map
MapName="World.sqlitedb";
this.mapView = new OfflineMapView(this, MapName);
this.mapController = mapView.getController();
// set Zoom Countrol
this.mapView.setBuiltInZoomControls(true);
// set Touch Control
this.mapView.setMultiTouchControls(true);
// zoom to 15
this.mapController.setZoom(15);
//add mapview
this.mapLayout.addView(this.mapView, new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.FILL_PARENT));
// scroll to 24082456, 120558472
GeoPoint geoPoint = new GeoPoint(24082456, 120558472);
this.mapController.setCenter(geoPoint);
}
}
I create a global string variable to hold the MapName
where the Listview
class can set via public method setMapName()
.
By the way, is there a method to read the MBTiles center automatically instead of hard coding like this?
// scroll to 24082456, 120558472
GeoPoint geoPoint = new GeoPoint(24082456, 120558472);
this.mapController.setCenter(geoPoint);
Which method should I use everytime user switches to OSMdroid
map from ListView
to load the selected the offline MBtiles map from list view? Above on Create method only do load the MBTiles map once when loaded for the first time? Here is the list view code.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class MyTwoListItemsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "mapname", "selected" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Map1", "Map1"));
list.add(putData("Map2", "Map2"));
list.add(putData("Map3", "Map3"));
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("mapname", mapname);
item.put("selected", selected);
return item;
}
}
How do I integrate switching between MyTwoListItemsActivity
and OfflineMapDemoActivity
?