Android引路蜂地图开发示例:离线地图示例

jerry 地图开发 2015年11月26日 收藏

在手机上,离线地图包一般放在SD卡上,然后通过文件读取。引路蜂地图开发包支持同时读取多个地图包,这是通过MapTiledZone类和MapTileStreamReader类来完成的。每个地图包对应于一个MapTiledZone,地图包对每张地图建立的索引以加速图片检索将记录地图包存放的区域和缩放级别。而类MapTileStreamReader提供了管理这些地图包的方法.

离线地图工具参见: 和

下面的示例从SD卡guidebee子目录读取world.map, china.map, nanjing.map ,更一般的做法是读取子目录下所有扩展名为.map 离线地图包,这样可以随时添加或删除离线地图包。因为是示例,所以在代码中指定读取这三个地图包。下面离线地图包为例子中用到的,是使用离线工具制作的。

文件名 大小 说明 下载
world.map 449k 世界地图 1-4级 下载
china.map 181M 中国地图 4-11级 下载
nanjing.map 64M 南京地图 11-17级 下载

随着缩放级别的增大,需下载的离线地图图片呈现指数增长,一般不可能将一个国家从1到17放在一个离线地图包,可以分几个层次下载制作离线地图包。如上图分3个层次。世界?>中国?>南京。

例子从SD卡中读取地图文件,在项目中添加一个MapTiledZone的派生类FileMapTiledZone ,从文件中读取离线地图。

  1. package com.pstreets.gisengine.demo;
  2. import com.mapdigit.gis.raster.MapTiledZone;
  3. import com.mapdigit.util.Log;
  4. import java.io.DataInputStream;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8.  
  9. public class FileMapTiledZone extends MapTiledZone{
  10.  
  11.     ////////////////////////////////////////////////////////////////////////////
  12.     //--------------------------------- REVISIONS ------------------------------
  13.     // Date       Name                 Tracking #         Description
  14.     // ---------  -------------------  -------------      ----------------------
  15.     // 14AUG2009  James Shen                            Code review
  16.     ////////////////////////////////////////////////////////////////////////////
  17.     /**
  18.      * constructor.
  19.      * @param fileName  file name of the map zone.
  20.      * @param isMarkSupported  does device support mark or not.
  21.      * @throws FileNotFoundException
  22.      */
  23.     public FileMapTiledZone(String fileName,boolean isMarkSupported)
  24.      throws FileNotFoundException  {
  25.         super(null);
  26.         this.fileName=fileName;
  27.         this.isMarkSupported=isMarkSupported;
  28.        
  29.  
  30.     }
  31.  
  32.     ////////////////////////////////////////////////////////////////////////////
  33.     //--------------------------------- REVISIONS ------------------------------
  34.     // Date       Name                 Tracking #         Description
  35.     // ---------  -------------------  -------------      ----------------------
  36.     // 14AUG2009  James Shen                            Code review
  37.     ////////////////////////////////////////////////////////////////////////////
  38.     /**
  39.      * constructor.
  40.      * @param fileName  file name of the map zone.
  41.      * @throws FileNotFoundException
  42.      */
  43.     public FileMapTiledZone(String fileName) throws FileNotFoundException  {
  44.         this(fileName,false);
  45.     }
  46.  
  47.     public void ensureClose() throws IOException {
  48.         if (!isMarkSupported) {
  49.             super.ensureClose();
  50.             try {
  51.              dataInputStream.close();
  52.                 fileConnection.close();
  53.             } catch (IOException ex) {
  54.                 ex.printStackTrace();
  55.             }
  56.         }
  57.     }
  58.  
  59.      public DataInputStream skipBytes(long offset) {
  60.         try {
  61.          fileConnection=new FileInputStream(fileName);
  62.             dataInputStream=new DataInputStream(fileConnection);
  63.             if (offset > 0) {
  64.              fileConnection.skip(offset);
  65.             }
  66.  
  67.         } catch (Exception e) {
  68.             Log.p("seek error:" + e.getMessage(), Log.ERROR);
  69.         }
  70.         return dataInputStream;
  71.     }
  72.  
  73.     private FileInputStream fileConnection=null;
  74.     private DataInputStream dataInputStream;
  75. }

下面代码从SD卡上读取离线地图信息:

  1. package com.pstreets.gisengine.demo;
  2.  
  3. import com.mapdigit.gis.geometry.GeoLatLng;
  4. import com.mapdigit.gis.raster.MapTileStreamReader;
  5. import com.mapdigit.gis.raster.MapType;
  6. import com.pstreets.gisengine.R;
  7. import com.pstreets.gisengine.SharedMapInstance;
  8.  
  9. import android.app.Activity;
  10. import android.os.Bundle;
  11.  
  12. public class StoredMap extends Activity {
  13.  
  14.  public void onCreate(Bundle savedInstanceState) {
  15.   super.onCreate(savedInstanceState);
  16.   setContentView(R.layout.main);
  17.  }
  18.  
  19.  public void onStart() {
  20.   super.onStart();
  21.   GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
  22.   SharedMapInstance.map.setCenter(center, 4, MapType.MICROSOFTCHINA);
  23.   MapTileStreamReader streamReader=SharedMapInstance
  24.      .mapTileDownloadManager.getInteralMapTileStreamReader();
  25.  
  26.   try{
  27.    FileMapTiledZone mapTileZone
  28. =new FileMapTiledZone("/sdcard/guidebee/world.map",false);
  29.    streamReader.addZone(mapTileZone);
  30.    mapTileZone=new FileMapTiledZone("/sdcard/guidebee/china.map",false);
  31.    streamReader.addZone(mapTileZone);
  32.    mapTileZone=new FileMapTiledZone("/sdcard/guidebee/nanjing.map",false);
  33.    streamReader.addZone(mapTileZone);
  34.    streamReader.open();
  35.   }catch(Exception e){
  36.    
  37.   }
  38.  
  39.  }
  40.  
  41. }
  42.  

MapTileManager 内部定义一个MapTileStreamReader ,如果设置了本地地图,MapTileManager会先从本地读取,如果本地没有需要图片,然后再从地图服务器上下载。可以使用MapTileStreamReader的addZone 方法添加多个地图包,地图包可以有重叠的区域,这时添加的先后顺序对结果就会影响,如果有多个,则取第一个。