1. Introduction to offline Map
The Baidu map SDK starts with version 2.0.0 and uses an offline map of the vector version. Please select it when downloading it. Download the offline map.
2. Initialization
Create a MapView before using offline map. For details about how to create a MapView, see "Hello World.
The core code for initializing an offline map is as follows:
Source code copy printing
- MKOfflineMap mOffline = null; // declare the variable
- MapController mMapController = mMapView. getController ();
-
-
- // Write it in the onCreate Function
- MOffline = new MKOfflineMap ();
- // Change the implementation method of offline.
- MOffline. init (mMapController, new MKOfflineMapListener (){
- @ Override
- 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;
- }
- }
- }
- ); 3. Import offline packages
The SDK supports importing offline packages. decompress the downloaded offline packages from the official channel, and paste the vmp folder into the BaiduMapSdk folder under the root directory of the SD card. Download the offline map.
Source code copy printing
- Int num = mOffline. scan ();
- // If (num! = 0)
- // MText. setText (String. format (% d offline packages have been installed, num ));
If you have successfully imported the data using the above method, congratulations, you don't have to read it any more. Otherwise, please continue reading,
If your Android phone has both internal storage and mounted SD card, you may encounter the same problems as me. Previously, I placed the downloaded offline map package on the SD card of my mobile phone. The directory structure is/mnt/sdcard/BaiduMapSdk/vmp/. At this time, it cannot be imported, because the Baidu map SDK cannot be found at all. Later, I copied the BaiduMapSdk folder to the root directory of the mobile phone's internal storage, and then re-executed it immediately.
Later, I downloaded the Baidu map Android mobile app (apk ), it is found that the offline map package directory of the application is BaiduMap (I don't know why the cache directory of the offline map is BaiduMapSdk when using the SDK, but here it is BaiduMap. Is it more convenient to unify it ?), When importing an offline map package, you can select the storage location (mobile phone internal storage or SD card). When using the Baidu map sdk, there is no interface to specify the location of the offline map package, this causes the above problems when using the offline map package.