Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/37758097
Always feel that map application support offline Map is very important Ah, I wait for mobile 2G cock Wire, flow is not easy, and use and cherish.
For tutorials on offline maps, the official development Guide offers two options:
First, manual import, first download the offline package from the official website decompression, the VMP folder into the SD card root directory in the Baidumapsdk folder. Well, I mean, I can't accept it, I'm ignoring it.
Second, the interface download method is as follows: Moffline.start (Cityid), but also more reliable, is not detailed introduction.
Today, we mainly introduce the second approach in detail, and then integrate it into our map that has integrated the orientation sensor, if you don't understand:android Baidu map SDK v3.0.0 (iii) Add overlay marker and Infowindow use
: For convenience, I added a menu button ~ can see the download location can be saved, support several waiting for the download, has canceled the download and so on. The main course is, after downloading, only need to locate the traffic (even not) can be very good use of our map pull ~ by the way: originally wanted to get a thread pool, support multiple simultaneous download, this block may be a lot of attention will have some problems, but Baidu map open out of the start (Citycode) No matter how I try (not to try multiple offline map instances), you can only download one at a time. 1, Baidu map offline related API Introduction
A, class MkofflinemapProvide map download, offline map list access, downloaded map query etc java.util.arraylist<mkolupdateelement> getallupdateinfo () return to each city offline map update information
Java.util.arraylist<mkolsearchrecord> gethotcitylist () Back to top cities list
Java.util.arraylist<mkolsearchrecord> getofflinecitylist () Back to support offline map City list
Mkolupdateelement getupdateinfo (int cityid) returns the specified city ID offline map update information java.util.arraylist<mkolsearchrecord> searchCity (java.lang.String cityname) search the city offline map records according to the city name
Boolean init (Mkofflinemaplistener listener) Initial initialization
Boolean pause (int cityid) pauses the download of the offline map for the specified city ID
Boolean remove (int Cityid) deletes the offline map for the specified city ID
Boolean start (int Cityid) starts downloading the offline map for the specified city ID void destroy () destroys the offline map management module, which is called when not in use
Interface MkofflinemaplistenerOffline map event notification interface. void ongetofflinemapstate (int type, int state)
class Mkolupdateelement and class MkolsearchrecordBasically contains some cityname, Cityid, size, etc.
The API is also detailed in the Baidu Help documentation, which is used in the code below.
2. Entity bean for offline map city information
Package Com.zhy.zhy_baidu_ditu_demo00;public class offlinemapcitybean{private String cityname;private int cityCode;/* * * Download Progress */private int progress;private Flag flag = flag.no_status;/** * Download status: Stateless, paused, downloading * @author Zhy * */public enum Flag{no_status,pause,downloading}public Flag Getflag () {return flag;} public void Setflag (flag flag) {This.flag = flag;} Public Offlinemapcitybean () {}public Offlinemapcitybean (String cityname, int citycode, int progress) {This.cityname = Cityname;this.citycode = Citycode;this.progress = Progress;} Public String Getcityname () {return cityname;} public void Setcityname (String cityname) {this.cityname = CityName;} public int Getcitycode () {return citycode;} public void Setcitycode (int citycode) {this.citycode = Citycode;} public int getprogress () {return progress;} public void setprogress (int progress) {this.progress = progress;}}
Contains, city name: For display on the ListView, City ID: For query Download, progress: Update the display when the ListView is downloaded, flag: flag When the user opens or cancels the download.
3, the use of offline maps at actvity startup, first initialize the offline map
/** * Initialize offline map */private void Initofflinemap () {mofflinemap = new Mkofflinemap ();//Set Listener Mofflinemap.init (new Mkofflinemaplistener () {@Overridepublic void ongetofflinemapstate (int type, int.) {switch (type) {case mkofflinemap.type_download_update://offline Map Download Update event type mkolupdateelement update = Mofflinemap.getupdateinfo (state); LOG.E (TAG, Update.cityname + "," + Update.ratio); for (Offlinemapcitybean Bean:mdatas) {if (bean.getcitycode () = = State) { Bean.setprogress (Update.ratio); Bean.setflag (flag.downloading); break;}} Madapter.notifydatasetchanged (); LOG.E (Tag, "type_download_update"); Break;case mkofflinemap.type_new_offline://has a new offline map installation LOG.E (tag, "Type_new_ OFFLINE "); break;case mkofflinemap.type_ver_update://version update prompt break;}});}
To set up the download listener interface for offline maps, we are only concerned with TYPE mkofflinemap.type_download_update at this time, the incoming state is Cityid, then we pass Mofflinemap.getupdateinfo ( State), you can get the download data for the city, then update the data set of our ListView and finally refresh the interface.
Initialize data:
private void InitData () {//Get all popular cities arraylist<mkolsearchrecord> offlinecitylist = Mofflinemap.gethotcitylist () ;//Manually added XI ' an Mkolsearchrecord xian = new Mkolsearchrecord (); Xian.cityid = 233;xian.cityname = "Xian City"; Offlinecitylist.add ( Xian);//Get all the downloaded cities list arraylist<mkolupdateelement> allupdateinfo = Mofflinemap.getallupdateinfo ();// Set the status for all data for (Mkolsearchrecord record:offlinecitylist) {Offlinemapcitybean Citybean = new Offlinemapcitybean (); Citybean.setcityname (Record.cityname); Citybean.setcitycode (Record.cityid); if (allupdateinfo! = null)//No download record, Returns NULL, why does not return an empty list ~~{for (Mkolupdateelement ele:allupdateinfo) {if (Ele.cityid = = Record.cityid) {citybean.setprogress ( Ele.ratio);}}} Mdatas.add (Citybean);}}
Go to activity first through mofflinemap.gethotcitylist (); Get a list of popular cities, but there is no one in the hot city, I manually added a XI ' an. (Here I am for convenience, interested can list the city supported by the country); next Mofflinemap.getallupdateinfo (); Get the data for the city you have downloaded; then cross-compare to set the data source data.
Initialize ListView
private void Initlistview () {Mlistview = (ListView) Findviewbyid (R.ID.ID_OFFLINE_MAP_LV); madapter = new Myofflinecitybeanadapter (); Mlistview.setadapter (Madapter); Mlistview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long ID) { int Cityid = Mdatas.get (position). Getcitycode (); if (Mcitycodes.contains (Cityid)) {Removetaskfromqueue (position, Cityid);} Else{addtodownloadqueue (position, Cityid);}});}
Set a click event for the ListView item, join the download queue the first time you click, and cancel the download the second time you click. The adapter for the ListView
/** * Adapter * * @author Zhy * */class Myofflinecitybeanadapter extends baseadapter{@Overridepublic the list of popular city maps is Enabled (int position) {if (Mdatas.get (position). getprogress () = =) {return false;} return super.isenabled (position);} @Overridepublic int GetCount () {return mdatas.size ();} @Overridepublic Object getItem (int position) {return mdatas.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Offlinemapcitybean Bean = Mdatas.get ( position); Viewholder holder = null;if (Convertview = = null) {holder = new Viewholder (); Convertview = Minflater.inflate (r.layout.offl Inemap_item,parent, false); holder.cityname = (TextView) Convertview.findviewbyid (r.id.id_cityname); holder.progress = (TextView) Convertview.findviewbyid (r.id.id_progress); Convertview.settag (holder);} Else{holder = (Viewholder) Convertview.gettag ();} Holder.cityName.setText (Bean.getcityname ()); int progress = Bean.getprogress (); String progressmsg = "";//Based on progress, set display if (progress = = 0) {progressmsg = "not downloaded";} else if (progress = =) {Bean.setflag (flag.no_status);p rogressmsg = "Downloaded";} else{progressmsg = progress + "%";} Depending on the current state, the settings display switch (Bean.getflag ()) {case pause:progressmsg + = "" Wait for download "; break;case downloading:progressmsg + =" "Downloading" "; break;default:break;} Holder.progress.setText (progressmsg); return convertview;} Private class Viewholder{textview CityName; TextView Progress;}}
The code of the adapter is relatively simple, mainly is the data setting display of the fundamental bean in GetView. Finally, add a menu item to our main activity and the activity is completed:
@Overridepublic boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case R.ID.ID_MENU_MAP_ Offline:intent Intent = new Intent (mainactivity.this,offlinemapactivity.class); startactivity (Intent); break;
Well, about the Baidu map, the current use so much ~ ~ If the later different needs, will continue to update ~
source Click to download