Abstract: when you use a map, you want to display the map without connecting to the Internet. In this case, you need to use the offline map. How can I introduce offline maps in Baidu map SDK?
Bytes -------------------------------------------------------------------------------------------------------------------
Related reading:
1. [Baidu map-android SDK]: Write android from scratchProgram
Http://www.cnblogs.com/milkmap/archive/2011/11/25/2263475.html
2. ["Starting Point"-Baidu map mobile SDK] how to create a map
Http://www.cnblogs.com/milkmap/archive/2012/05/11/2496526.html
Bytes -------------------------------------------------------------------------------------------------------------------
1. Download the offline map package
Download offline map package to the official website, web site: http://shouji.baidu.com/map/map.html? From = 3052
First, select the mobile phone brand and mobile phone model:
After you select your phone, the system prompts you. Select the old version map to download.
Currently, the SDK can only use raster graphs, so we must download the old version of maps. In the future, the SDK will support vector graphs, and the offline map of Beijing will be reduced from MB to 14 Mb.
It is expected that the new offline map will be available in May ~~~
For example, I chose to download the offline map of Beijing:
After decompression, there are two files. One is baidumap and the other is installation instructions.
Do not care about the installation instructions. Open the baidumap and you can see the mapdata folder. Copy this folder.
Copy the mapdata folder to the root directory of the baidumapsdk folder on the mobile phone SD card.
2. Compile an offline MapCode
Find the main java file and declare the variable in the activity.
Mkofflinemap moffline =Null;//Offline map Variables
Then write the offline map initialization code in the oncreate function:
/** Offline map initialization * */ Moffline = New Mkofflinemap (); moffline. INIT (mbmapman, New Mkofflinemaplistener (){ Public Void Ongetofflinemapstate ( Int Type, Int State ){ Switch (Type ){ Case Mkofflinemap. type_download_update: {mkolupdateelement update = Moffline. getupdateinfo (State ); // Mtext. settext (string. Format ("% s: % d %", update. cityname, update. ratio )); } Break ; Case Mkofflinemap. type_new_offline: log. D ( "Offlinedemo", String. Format ("add offlinemap num: % d" , State )); Break ; Case Mkofflinemap. type_ver_update: log. D ( "Offlinedemo", String. Format ("New offlinemap Ver" )); Break ;}}}); /** Offline map import offline package * */ Int Num =Moffline. Scan (); // If (num! = 0) mtext. settext (string. Format ("% d offline packages installed", num ));
3. Connect to the real machine and check the running result.
See, this is not connected to the network, and the effect of the map is displayed.
4. All java filesSource code
Package Com. Baidu. searchpoi; Import Android. App. activity; Import Android. OS. Bundle; /** Introduction Class * */ Import Java. util. arraylist; Import Java. util. List; Import Android. content. context; Import Android. Graphics. Canvas; Import Android. Graphics. paint; Import Android. Graphics. Point; Import Android. Graphics. drawable. drawable; Import Android. Location. location; Import Android. OS. Bundle; Import Android. util. log; Import Android. View. view; Import Android. widget. Toast; Import Com. Baidu. mapapi. bmapmanager; Import Com. Baidu. mapapi. geopoint; Import Com. Baidu. mapapi. itemizedoverlay; Import Com. Baidu. mapapi. locationlistener; Import Com. Baidu. mapapi. mkaddrinfo; Import Com. Baidu. mapapi. mkdrivingrouteresult; Import Com. Baidu. mapapi. mkgenerallistener; Import Com. Baidu. mapapi. mklocationmanager; Import Com. Baidu. mapapi. mkolupdateelement; Import Com. Baidu. mapapi. mkofflinemap; Import Com. Baidu. mapapi. mkofflinemaplistener; Import Com. Baidu. mapapi. mkplannode; Import Com. Baidu. mapapi. mkpoiresult; Import Com. Baidu. mapapi. mksearch; Import Com. Baidu. mapapi. mksearchlistener; Import Com. Baidu. mapapi. mktransitrouteresult; Import Com. Baidu. mapapi. mkwalkingrouteresult; Import Com. Baidu. mapapi. mapactivity; Import Com. Baidu. mapapi. mapcontroller; Import Com. Baidu. mapapi. mapview; Import Com. Baidu. mapapi. mylocationoverlay; Import Com. Baidu. mapapi. overlay; Import Com. Baidu. mapapi. overlayitem; Import Com. Baidu. mapapi. poioverlay; Import Com. Baidu. mapapi. routeoverlay; Import Com. Baidu. mapapi. transitoverlay; /** Activity * */ Public Class Searchpoiactivity Extends Mapactivity { // Declarative Variables Bmapmanager mbmapman =Null ; Mapview mmapview = Null ; Mkofflinemap moffline = Null ; // Offline map Variables @ Override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main); mbmapman =New Bmapmanager (getapplication (); mbmapman. INIT ( "4aa2949e616e069c37cdc3152e8c401a05a0035d ", Null ); Super . Initmapactivity (mbmapman); mmapview = (Mapview) findviewbyid (R. Id. bmapsview); mmapview. setbuiltinzoomcontrols ( True ); // Set to enable the built-in zoom control Mapcontroller mmapcontroller = Mmapview. getcontroller ();// Get control of mmapview, which can be used to control and drive translation and Scaling Geopoint point = New Geopoint (( Int ) (39.93 * 1e6 ),( Int ) (116.42*1e6 )); // Construct a geopoint with the given longitude and latitude, in the unit of "micro" (degree * 1e6) Mmapcontroller. setcenter (point ); // Set map center point Mmapcontroller. setzoom (13 ); // Set map zoom level // Mmapview. setsatellite (true ); // Satellite Map // Mmapview. settraffic (true ); // Traffic Flow /** Offline map initialization * */ Moffline = New Mkofflinemap (); moffline. INIT (mbmapman, New Mkofflinemaplistener (){ Public Void Ongetofflinemapstate ( Int Type, Int State ){ Switch (Type ){ Case Mkofflinemap. type_download_update: {mkolupdateelement update = Moffline. getupdateinfo (State ); // Mtext. settext (string. Format ("% s: % d %", update. cityname, update. ratio )); } Break ; Case Mkofflinemap. type_new_offline: log. D ( "Offlinedemo", String. Format ("add offlinemap num: % d" , State )); Break ; Case Mkofflinemap. type_ver_update: log. D ( "Offlinedemo", String. Format ("New offlinemap Ver")); Break ;}}}); /** Offline map import offline package * */ Int Num = Moffline. Scan (); // If (num! = 0) mtext. settext (string. Format ("% d offline packages installed", num )); } @ Override Protected Boolean Isroutedisplayed (){ Return False ;} @ Override Protected Void Ondestroy (){ If (Mbmapman! = Null ) {Mbmapman. Destroy (); mbmapman = Null ;} Super . Ondestroy () ;}@ override Protected Void Onpause (){ If (Mbmapman! = Null ) {Mbmapman. Stop ();} Super . Onpause () ;}@ override Protected Void Onresume (){ If (Mbmapman! = Null ) {Mbmapman. Start ();} Super . Onresume ();} }