“This is the 25th day of my participation in the August Gwen Challenge.
In the last article, we realized the real-time positioning display function based on our research and learning, which can show our location on the map and automatically update it in real time. So let’s implement offline maps.
Offline maps are often used in daily business development. Then we need to load and download the map of the area we are in when there is an Internet connection. When there is no carrier network or WIFI, we can switch to the offline map without feeling, which will not greatly affect the use of users.
Welcome friends to search “Andy Hui” on wechat to pay attention to a wave!
Write some thoughts of the programmer, hope it will help you.
Baidu Map development – Realize offline map function 05 (QQ.com)
Realize offline map function
About offline map, the content of this Baidu tutorial is not very detailed, specific or need you to download the demo they provide, to study, otherwise it is difficult to achieve offline map function.
There are two main ways to realize offline map:
First, store the offline map file downloaded in advance in a specific location of the APP software.
This method is not officially explained at present, according to the online said that the previous version has special instructions, can import offline map, the latest version has abandoned this interface. But after my research, it turns out it can be used wisely.
Baidu map download file is.dat format end, according to the baidu map official APP research, found that it will download offline map cache to Android/data/ APP package name /files/ VMP, we can look at their Android phone cache Baidu map offline map address.
In fact, I was wondering, since its official cache address is here, does that mean that in the SDK, it retrieves the offline map from this address first for display? If so, can you put the map file I downloaded in advance here?
It turns out that this is actually the case. Putting offline maps that need to be cached into this directory can load and display maps when the phone has no Internet connection.
If active downloading is difficult for your business needs, you can use this method to remotely update offline maps to customers for on-site use.
The specific address is shown in the following figure: the red box at the top is the address, and the red circle at the bottom is the downloaded offline map of Tianjin. In the case of no network, the SDK will load the offline map for display by default.
2. Use MKofflineMap to actively download maps.
Let’s first go according to the official website to the tutorial, to see whether we will encounter problems.
If you are new to the offline map tutorial, you will be confused. The official list just gives you a few specific key points, some details of the operation is not explained.
- Initialize the offline map
MKOfflineMap mOffline = new MKOfflineMap(); // Moffline.init (listener) is triggered when the offline map state changes.Copy the code
- Start the download
ArrayList<MKOLSearchRecord> Records = moffline.searchCity (cityName); if (records ! = null && records.size() == 1) { cityId = records.get(0).cityID; // cityID moffline.start (cityID);Copy the code
- Pause to download
// Pause moffline. pause(cityid);Copy the code
- To stop the downloading
// Stop downloading moffline. pause(cityid);Copy the code
- Delete the download
Remove (cityid); // Download moffline.remove (cityid);Copy the code
- Download updates
// Download moffline.update (cityid);Copy the code
As for the update download, information will be called back and forth through onGetOfflineMapState during the update process, and you can view the update progress, new offline map installation, version update prompt and other functions.
The above content is the tutorial given by the official, it is true that the map can be downloaded by following it, but there is no hint, it is not friendly, and the developers do not know where the map is downloaded, they need to study according to the DEMO given by themselves.
The following is a combination of official tutorials and official API, DEMO, etc., to find out. At present, only a simple implementation of the map download, download progress value display. You can customize this later to suit your business needs.
Initialize the offline map
2. Download the corresponding map by city ID
3. Display map download progress in callback function (0-100)
So let’s actually do it
Click the event to download and pause the map
In the listener event mKOfflineMapListener, use the callback function to get the progress of downloading the map in real time.
After running, click start download to realize the offline operation of Tianjin map. The download progress will be obtained in the callback function and printed in real time and displayed on the software interface.
When the progress is complete, you will see the offline map file of Tianjin that you have downloaded under the address folder in the red box.
Hahaha, doesn’t it feel nice?
Above I am just a simple introduction to download offline map, which around offline map there are a lot of gameplay, specific according to their own business combined, can be achieved.
Well, that’s all for today’s sharing. Thank you for reading.
A word of warning to everyone, see the DEMO, it is much better than the official documentation tutorial.
Small remarks
Life is short, I don’t want to go after what I can’t see, I just want to catch what I can see.
Original is not easy, give a attention.
I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.