Android Baidu Map SDK v3_3_0 (iii)---offline map

Source: Internet
Author: User

Forwarding please indicate the source: http://blog.csdn.net/tanzuai/article/details/43793455

Features to be implemented in this blog post

First on:

Let's show you how to achieve

1. Function Introduction

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.

Second, the interface download method is as follows: Moffline.start (Cityid); Still more reliable, I believe this is everyone's favorite.


     2, 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 the 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 by 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) launches offline map to download 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 Mkolsearchrecord

Basically contains some cityname, Cityid, size, etc.

3. Example Introduction

3.1 initializing layout controls

        /**  * Initialize layout control  */private void Initofflinelayout () {Cidview = (TextView) Findviewbyid (R.id.cityid);// City Idcitynameview = (EditText) Findviewbyid (r.id.city);//city Name Stateview = (TextView) Findviewbyid (r.id.state);// Download Status Hotcitylist = (ListView) Findviewbyid (r.id.hotcitylist);//Popular Cities Allcitylist = (ListView) Findviewbyid ( r.id.allcitylist)///National City Allcity = (linearlayout) Findviewbyid (r.id.citylist_layout);//National Urban Total layout localcity = ( LinearLayout) Findviewbyid (r.id.localmap_layout);//download City Layout Localmaplistview = (ListView) Findviewbyid ( r.id.localmaplist);//Downloaded City}

3.2 Initializing offline maps

     /** * Initialize offline map */private void Initofflinemap () {moffline = new Mkofflinemap ();   Moffline.init (this);//Get the lively city records1 = Moffline.gethotcitylist (); if (records1! = null) {for (Mkolsearchrecord r:records1) {hotcities.add (R.cityname + "(" + R.cityid + ")" + "--" + thi    S.formatdatasize (R.size));} } Hadapter = (listadapter) New arrayadapter<string> (this, Android.  R.layout.simple_list_item_1, hotcities);  Hotcitylist.setadapter (Hadapter);  Get all cities that support offline maps allcities = new arraylist<string> ();  RECORDS2 = Moffline.getofflinecitylist (); if (records1! = null) {for (Mkolsearchrecord r:records2) {allcities.add (R.cityname + "(" + R.cityid + ")" + "--" + thi  S.formatdatasize (R.size));} } Alladapter = (listadapter) New arrayadapter<string> (this, Android.  R.layout.simple_list_item_1, allcities);  Allcitylist.setadapter (Alladapter);  Allcity.setvisibility (View.gone);  Localcity.setvisibility (view.visible); Get offline map information that has been down localmaplist = MoffliNe.getallupdateinfo ();  if (localmaplist = = null) {localmaplist = new arraylist<mkolupdateelement> ();  } ListView Localmaplistview = (ListView) Findviewbyid (R.id.localmaplist);  Ladapter = new Localmapadapter (); Localmaplistview.setadapter (Ladapter);}
3.3. Initialize the Listener

    @Override public   void ongetofflinemapstate (int type, int.) {    switch (type) {case    Mkofflinemap.type_ Download_update: {  mkolupdateelement UPDATE = Moffline.getupdateinfo (state);  Handle the download progress update prompt  if (update! = null) {Stateview.settext (String.Format ("%s:%d%%", Update.cityname,update.ratio)); Updateview ();  }    } break;    Case Mkofflinemap.type_new_offline://There is a new offline map installed LOG.D ("Offlinedemo", String.Format ("Add Offlinemap num:%d", state)); break;    Case Mkofflinemap.type_ver_update://version update prompt//mkolupdateelement e = Moffline.getupdateinfo (state); break;    }   }

3.4 Configuring the Adapter

    /** * Offline Map Management list Adapter */public class Localmapadapter extends Baseadapter {@Override public int getcount () {return lo  Calmaplist.size ();  } @Override public Object getItem (int index) {return localmaplist.get (index);  } @Override public long getitemid (int index) {return index; } @Override public View getView (int index, view view, ViewGroup arg2) {mkolupdateelement e = (mkolupdateelement) getItem (index); view = View.inflate (offlineactivity.this,r.layout.offline_localmap_list, null); Initviewitem (view, E);  return view; } void Initviewitem (view view, Final Mkolupdateelement e) {button display = (Button) View.findviewbyid (R.id.display); Button remove = (Button) View.findviewbyid (R.id.remove); TextView title = (TextView) View.findviewbyid (r.id.title); TextView update = (TextView) View.findviewbyid (r.id.update); TextView ratio = (TextView) View.findviewbyid (r.id.ratio) ratio.settext (e.ratio + "%"); Title.settext (e.cityname); if ( E.update) {Update.settext ("updatable");} else {Update.settext ("mostNew ");} if (e.ratio! =) {display.setenabled (false);} else {display.setenabled (true);} Remove.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {moffline.remove (E.cityid); Updateview ();}}); Display.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.putextra ("x", E.geopt.longitude); Intent.putextra ("Y", e.geopt.latitude); Intent.setclass ( Offlineactivity.this, Mainactivity.class); startactivity (intent);}});}}
3.5 Click events for each feature below

/** * Switch to City list * * @param view */public void Clickcitylistbutton (view view) {allcity = (linearlayout) Findviewbyid (r.id.ci Tylist_layout); localcity = (linearlayout) Findviewbyid (r.id.localmap_layout); allcity.setvisibility (View.GONE); Localcity.setvisibility (view.visible);} /** * Start Download * * @param view */public void Start (view view) {int Cityid = Integer.parseint (Cidview.gettext (). toString ()); MO Ffline.start (Cityid); Clicklocalmaplistbutton (null); Toast.maketext (This, "Start downloading offline maps. Cityid: "+ Cityid, Toast.length_short). Show (); Updateview ();} /** * Switch to download management list * * @param view */public void Clicklocalmaplistbutton (view view) {allcity = (linearlayout) Findviewbyid (R . id.citylist_layout); localcity = (linearlayout) Findviewbyid (r.id.localmap_layout); Allcity.setvisibility ( view.visible); localcity.setvisibility (View.gone);} /** * Pause Download * * @param view */public void Stop (view view) {int Cityid = Integer.parseint (Cidview.gettext (). toString ()); mOf Fline.pause (Cityid); Toast.maketext (This, "Pause downloading of offline maps. Cityid: "+Cityid, Toast.length_short). Show (); Updateview ();} /** * Delete Offline map * * @param view */public void Remove (view view) {int Cityid = Integer.parseint (Cidview.gettext (). toString ()) ; Moffline.remove (Cityid); Toast.maketext (This, "delete the offline map. Cityid: "+ Cityid, Toast.length_short). Show (); Updateview ();} /** * Search Offline Demand City * * @param view */public void search (view view) {arraylist<mkolsearchrecord> records = Moffline.search City (Citynameview.gettext (). toString ()); if (records = = NULL | | records.size ()! = 1) return;cidview.settext ( String.valueof (Records.get (0). Cityid));} /** * Import offline Map installation package from SD card * * @param view */public void Importfromsdcard (view view) {int num = Moffline.importofflinedata (); String msg = ""; if (num = = 0) {msg = "The offline package is not imported, it may be the offline package placement is incorrect, or the offline package has been imported";} else {msg = String.Format ("Successfully imported%d offline packages, can be viewed in download management", num);} Toast.maketext (This, MSG, Toast.length_short). Show (); Updateview ();}

3.6 In order to save power to the mobile phone, the following we will start, the close of the

@Overrideprotected void OnPause () {int Cityid = Integer.parseint (Cidview.gettext (). toString ()); Mkolupdateelement temp = Moffline.getupdateinfo (Cityid); if (temp! = NULL && Temp.status = = mkolupdateelement.downloading) {moffline.pause (Cityid);} Super.onpause ();} @Overrideprotected void Onresume () {Super.onresume ();} @Overrideprotected void OnDestroy () {/** * exit, destroy the offline map module */moffline.destroy (); Super.ondestroy ();}

3.7 There is the previous show Map page to be modified.(You can refer to the Android Baidu map SDK v3_3_0 (a)---basic map)

   Intent Intent = Getintent (); if (Intent.hasextra ("x") && Intent.hasextra ("y")) {//When using the intent parameter, set the center point to the specified point Bundle b = Intent.getextras (); LATLNG p = new Latlng (b.getdouble ("Y"), B.getdouble ("X")); Mmapview = new Mapview (this,new baidumapoptions (). Mapstatus (New Mapstatus.builder (). Target (P). Build ())); } else {Mmapview = new Mapview (This, new Baidumapoptions ());}         Setcontentview (Mmapview);

Because the offline download page changes the coordinate location (you can view the above3.4 Configuring the adapter's code)

All right! In fact, it is quite simple! After reading this blog I believe you already know how to achieve the offline map! There is a better way to achieve, you can go to the Comments column to publish their own comments!

Below is the source code:

Source Code




Android Baidu Map SDK v3_3_0 (iii)---offline map

Related Article

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.