The following content is reprinted from the article offline Map Access Guide of Tencent Location Service.

Author: Tencent Location Service

Link: www.cnblogs.com/TencentLBS/…

Source: Blogland

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

In order to smoothly display the map in no network, weak network or special external network environment, Tencent map SDK from version 4.3.5, provides a set of offline map scheme. Support offline map switch, city list access, offline city data download, download status callback, offline cache management and other capabilities.

Offline map download address: iOS map SDK, Android Map SDK

This article is for Android map SDK offline map use tutorial, iOS offline map tutorial please refer to: lbs.qq.com/mobile/iOSM…

To enable the offline map capability, perform the following steps:

  1. Turn on the offline map switch

  2. Get a list of cities for the offline map

  3. Gets an offline controller for a city

  4. Determine whether the city has data updates

  5. Perform offline data download

Turn on the offline map switch

TencentMapOptions allows you to configure the offline map switch. When multiple map instances are deployed, the status of the offline map switch remains the same.

TencentMapOptions options = new TencentMapOptions();
options.setOfflineMapEnable(true);
MapView mapView = new MapView(context, options);
TencentMap map = mapView.getMap();
Copy the code

Get a list of cities for the offline map

With the offline map switch turned on, the offline map component can be obtained through the getMapComponent interface of TencentMap

OfflineMapComponent offlineComp = map.getMapComponent(OfflineMapComponent.class);
Copy the code

The offline map component provides four interfaces:

Public interface OfflineMapComponent extends TencentMapComponent.Com ponent {/ * * * open * * @ whether offline mapsreturn*/ Boolean isOfflineMapEnable(); Void syncLatestData(OfflineMapSyncedListener listener); ** @param Item Specifies the offline Item * @param listener status listener * @returnOffline map data, when the incoming Item is invalid (null or not included in offline service) * or offline map mode is not enabled * or offline map initialization is not complete, Returns null * / OfflineItemController getOfflineItemController (OfflineItem item, OfflineStatusChangedListener listener); /** * get the list of offline data ** @return*/ List<OfflineItem> getOfflineItemList(); /** * Get the list of offline data asynchronously, Void getOfflineItemList(callback <List<OfflineItem>> callback); void getOfflineItemList(callback <List<OfflineItem>> callback); }Copy the code

Refresh to get the latest data through the syncLatestData interface

offlineComp.syncLatestData(new OfflineMapSyncedListener() {
  @Override
  public void onSynced(boolean result) {
    Log.i("Initial synchronization result:" + result);
    if(result) {// handle offline data}}});Copy the code

The offline city list data can be obtained asynchronously through getOfflineItemList interface, or the offline list can be obtained synchronously by waiting for syncLatestData results

/ / asynchronous get offline city List offlineComp getOfflineItemList (new Callback < List < OfflineItem > > () {@ Override public void callback(List<OfflineMapCity> result) {if(result ! = null) { mOfflineCityList = result; // Select a city for the next step}}}); // get synchronously when syncLatestData results intrue, the list is not empty mOfflineCityList = offlineComp. GetOfflineItemList ();Copy the code

In the list of offlineItems returned, relevant data for each offline city will be provided. Offlineitems are divided into three types:

  1. OfflineNation

  2. OfflineProvince

  3. OfflineCity

data type instructions
name String Download item display name
pinyin String The only pinyin marked
size long Data size
upgrade boolean Whether it needs to be updated
percentage int Percentage of data [0-100]

Gets an offline controller for a city

Retrieves the offline controller for a city from the offline map component, which returns Null in three cases:

  1. Offline map switch is not enabled
  2. Introduction to illegal cities (only support national summary, municipalities, prefecture-level city, do not support downloading a province)
  3. Map initialization is not complete
OfflineItem beijing = findCity("Beijing");
OfflineItemController cityController = offlineComp.getOfflineItemController(beijing, statusChangedListener);
Copy the code

Monitor interface for state changes

Public interface OfflineStatusChangedListener {/ * * * * * @ offline callback param * @ param item city status status * / void onStatusChanged(OfflineItem item, OfflineStatus status); }Copy the code

The offline city controller provides four interfaces:

Public interface OfflineItemController {/** * check for invalid ** @return true*/ Boolean checkInvalidate(); */ Boolean checkInvalidate(); /** * Enable offline ** @return*/ Boolean open(); /** * close offline ** @return*/ Boolean close(); /** * startDownload */ void startDownload(); /** * stopDownload */ void stopDownload(); /** * Remove the cache, does not affect the currently used offline city * @return true*/ Boolean removeCache(); }Copy the code

Determine whether the city has data updates

After obtaining the city controller, the controller can check whether the city needs data update. When the offline data of the city is not loaded, the data version is upgraded, or the cache is cleared, the controller will return true

if(cityController ! = null) { boolean needDownload = cityController.checkInvalidate();if(needDownload) {/ download/execution cityController startDownload (); }else{// Skip the update and open the offline cityController.open(); }}Copy the code

Perform offline data download

City whether there is any update if there is no judgment, executed directly download, its internal will update data check, the result will be returned in the OfflineMapStatusChangedListener callback

Public enum DOWNLOADING {/** * offline ERROR */ ERROR, /** * READY to download */ READY, /** * START */ START, /** * DOWNLOADING */ DOWNLOADING, / CANCEL download * * * * / CANCEL, / * * * * / COMPLETED it has finished downloading, OPEN / * * * * / offline OPEN, / * * * CLOSE offline * / CLOSE}Copy the code

To stop the downloading

You can stop a city downloading from the city controller

 if(cityController ! = null) { cityController.stopDownload(); }Copy the code

Clear the cache

The cache for the current city can be cleared through the city controller

Citycontroller.close (); / / clean up the city's cache Boolean ret. = cityController removeCache ();Copy the code