Android Baidu Map SDK v3.0.0 (d) offline map function Introduction

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lmj623565791/article/details/37758097

It is important to think that map applications support offline maps. I wait to move the 2G cock wire, the flow is not easy. and use and cherish.

For tutorials on offline maps, the official development Guide offers two options:

First. Manual import, the first download from the official website offline package decompression, the VMP directory into the SD card root directory in the BAIDUMAPSDK directory.

Well, I mean, I can't accept it, I'm ignoring it.

Second, the interface download method such as the following: Moffline.start (Cityid), but also more reliable, is not specifically introduced.

Today. We mainly introduce a different approach, and then integrate it into our map that has integrated the positioning direction sensor, assuming 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 multiple waiting for the download, has canceled the download and so on.

Most of course, after the download, only need to locate the traffic (even not) can be very good use of our map pull ~ by the way: originally wanted to engage in a thread pool, support multiple download at the same time, this piece may be very much do not notice there will be some problems, but the Baidu map to open the start ( Citycode) No matter how I try (I can't even try multiple offline map instances). You can only download one at a time. 1, Baidu map offline related API Introduction A, class Mkofflinemap to provide map download, offline map list access, downloaded map query and so on java.util.arraylist< Mkolupdateelement> Getallupdateinfo ()   Back to Cities 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)   Return the specified city ID offline map update information java.util.arraylist< Mkolsearchrecord> searchcity (java.lang.String cityname)   Search the city by city name offline map record
Boolean init ( Mkofflinemaplistener listener)   Initial
Boolean pause (int cityid)   Pause download offline map for specified city ID
Boolean remove (int Cityid)   Delete offline map with specified City ID
Boolean start (int Cityid)   launch offline map to download specified city ID void destroy ()   Destroy offline map Management module, call when not in use &NBSP

interface mkofflinemaplistener offline map event notification interface.

void ongetofflinemapstate (int type, int state)
class Mkolupdateelement and   class Mkolsearchrecord basically includes some cityname, Cityid, size, etc.
APIs are also very specific in Baidu's help documentation, which are 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. Pause, 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;}}

Included, city name: Used for display on the ListView. City ID: Used for query download, progress: Update the display when the ListView is downloaded, flag: When the user opens or cancels the download.


3, the use of offline maps when the actvity start. 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 number update prompt break;}});}
Set up the download listener interface for offline maps. For now we are only concerned with TYPE mkofflinemap.type_download_update, when the incoming state is Cityid, then we pass mofflinemap.getupdateinfo (state); To get the download data for the city, 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 the list of all cities already downloaded 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 matter what the 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);}}

Enter activity first through mofflinemap.gethotcitylist (); Get a list of popular cities. Just hot inside incredibly no XI ' an, I manually added a XI ' an. (Here I am for convenience.) Interested in the list of cities supported by the country); next Mofflinemap.getallupdateinfo (); Get the data for the city you have downloaded, then cross-control to set up 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 the Click event for the ListView item and join the download queue the first time you click. 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 status, 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. It is mainly the data setting display of the fundamental bean in GetView. Finally, we add a menu item to our main activity, and this 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, now use so many ~ ~ Assume that the later different needs. will also continue to update ~

source code click to downloadNote: Developers key needs to be replaced by their own application, not clear the application please see the first blog.



Baidu Map Related Blog Video version number has been online: the use of Baidu map in Android look forward to your support.


Bo Master part of the video has been online, assuming you do not like boring text, please poke (first record, look forward to your support):

1. Android self-defined control scratch card in real-action e-commerce activities

2. Android Self-defining controls to build Android streaming layouts and popular tags

3. android Robot "Little Mu" implementation

4, high imitation QQ5.0 sideways slip

5, High imitation 5.2.1 main interface and message alert





Baidu Map Related Blog Video version number has been online: the use of Baidu map in Android look forward to your support.



Bo Master part of the video has been launched. If you don't like the boring text, please poke (first record, look forward to your support):

1. Android self-defined control scratch card in real-action e-commerce activities

2. Android Self-defining controls to build Android streaming layouts and popular tags

3. android Robot "Little Mu" implementation

4, high imitation QQ5.0 sideways slip

5, High imitation 5.2.1 main interface and message alert


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Android Baidu Map SDK v3.0.0 (d) offline map function Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.