android - OSMdroid Loading Multiple Offline MBTIles Via ListView -


i using osmdroid offline mbtiles. need able add users select multiple mbtiles listview. listview windows easy. however, need getting different mbtiles loaded based on mbtiles selected listview. mbtiles loaded 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 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 24082456, 120558472         geopoint geopoint = new geopoint(24082456, 120558472);         this.mapcontroller.setcenter(geopoint);      } } 

i create global string variable hold mapname listview class can set via public method setmapname().

by way, there method read mbtiles center automatically instead of hard coding this?

    // scroll 24082456, 120558472     geopoint geopoint = new geopoint(24082456, 120558472);     this.mapcontroller.setcenter(geopoint); 

which method should use everytime user switches osmdroid map listview load selected offline mbtiles map list view? above on create method load mbtiles map once when loaded first time? here 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[] = { "mapname", "selected" };         int[] = { 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 integrate switching between mytwolistitemsactivity , offlinemapdemoactivity?

small world, did looking for. required patch osmdroid.

assuming patch applied, need make own extension of maptileproviderbase (see maptileproviderbasic reference). you'll want create constructor takes in file array , pass filearchiveprovider. here, once user has selected files want use offline maps, pass list instance of custom tile provider, call mapview.settileprovider.

also note /sdcard/osmdroid works fine purpose, if device on kitkat , files in /storage/extsdcard (such samsung devices), you'll need need this

i'll submit pull request of adapters made make process simpler.

edit: direct question, "how integrate switching between mytwolistitemsactivity , offlinemapdemoactivity?", more of android specific question on how transfer data 1 activity another. intents , shared preferences candidates application. may want consider using kind of popup, menu, action bar, dialog, etc flip map sources. construction of mapview expensive , preventing reinitialization idea.

edit: here's code used.

caveat 1) need know tile source name ahead of time each archive. zip files map source name, openstreetmaps. sqlite has column it, etc. if tiles don't show up, cause.

create tile source. simplicity's sake, choose name files map file source names. adjust meet needs.

public class filebasedtilesource extends xytilesource {      public filebasedtilesource(string aname, resourceproxy.string aresourceid, int azoomminlevel, int azoommaxlevel, int atilesizepixels, string aimagefilenameending, string[] abaseurl) {         super(aname, aresourceid, azoomminlevel, azoommaxlevel, atilesizepixels, aimagefilenameending, abaseurl);     }      public static itilesource getsource(string name) {         if (name.contains(".")) {             name = name.substring(0, name.indexof("."));         }         return new filebasedtilesource(name,             resourceproxy.string.mapbox, 0, 18, 256, ".png", new string[]{             "http://localhost"});     } } 

then create with

itilesource src=filebasedtilesource.getsource("mytilesource.gemf"); 

next you'll need following class, i'll create pull request for. tile provider.

public class offlinetileprovider extends maptileproviderarray implements imaptileprovidercallback {      /**      * creates {@link maptileproviderbasic}.      * throws source[] null or empty      */     public offlinetileprovider(final iregisterreceiver pregisterreceiver, file[] source     )         throws exception {         super(filebasedtilesource.getsource(source[0].getname()), pregisterreceiver);         iarchivefile[] f = new iarchivefile[source.length];         (int i=0; < source.length; i++)             f[i]=archivefilefactory.getarchivefile(source[i]);          mtileproviderlist.add(new maptilefilearchiveprovider(pregisterreceiver, gettilesource(), f));      } } 

then create instance follows:

offlinetileprovider provider = new offlinetileprovider(new simpleregisterreceiver(mcontext), myuserselectedfiles[])); mmapview.settileprovider(provider); mmapview.setusedataconnection(false); mmapview.invalidate(); 

that should create instance , update map view.

additional notes: io exceptions more possible


Comments