Android 百度离线地图下载完后调用下载好的离线包

前言  

    最近公司项目需求添加百度地图离线下载后离线地图查看功能,本以为看着文档写一下就行了,看了文档后发现百度文档着实坑爹,文档与实际开发不符合,网上搜了一下,也没搜到切实有用的文章,遂决定写一篇;

步骤

    一;百度离线地图下载完成后,会在手机根目录中生成一个BaiduMapSDKNew的文件,打开里面的vmp文件夹会看到下载到的离线地图包;

 

   二,给离线地图下载完成的列表添加点击事件;

        Adapter里面:

//查看离线地图
listItemView.name.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (list.get(fposition).status==MKOLUpdateElement.FINISHED&&list.get(fposition).ratio==100){
            context.setIntent(list.get(fposition).cityID);
        }else if (list.get(fposition).ratio<100){
            Toast.makeText(context,"请先下载完离线地图",Toast.LENGTH_SHORT).show();
        }
    }
});
Activity里面:
//将点击的城市市中心坐标传递到离线地图加载页
public void setIntent(int cityid){
    MKOLUpdateElement map=mOffline.getUpdateInfo(cityid);
    Intent intent = new Intent(MyDownMapActivity.this, DownMapShowActivity.class);
    Bundle bundle=new Bundle();
    String lon=String.valueOf(map.geoPt.longitude);
    bundle.putString("lon",lon);
    String lat=String.valueOf(map.geoPt.latitude);
    bundle.putString("lat",lat );
    intent.putExtras(bundle);
    startActivity(intent);

}

三、在离线地图查看页获取到坐标并添加离线地图展示,具体看代码;

 
 
public class DownMapShowActivity extends Activity implements MKOfflineMapListener{
    private MapView mMapView;
    FrameLayout layout;
    Double lat;
    Double lon;
    LatLng center;
    float zoom;
    MKOfflineMap mkOfflineMap;

    BaiduMap baiduMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        //调用离线地图,否则第二次进入会加载不到
        mkOfflineMap=new MKOfflineMap();
        mkOfflineMap.init(this);

        MapStatus.Builder builder = new MapStatus.Builder();
     
        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle=intent.getExtras();
            String latstr=bundle.getString("lat");
            lat=Double.valueOf(latstr);
            String lonstr=bundle.getString("lon");
            lon=Double.valueOf(lonstr);
            Log.e("lon",lon+"");
            center = new LatLng(lat,lon);
            zoom = 13.0f;
        }
        builder.target(center).zoom(zoom);

        setMapCustomFile();

        mMapView = new MapView(this, new BaiduMapOptions());
        initView(this);

        baiduMap=mMapView.getMap();
        //默认显示普通地图
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        setContentView(layout);

    }
    // 初始化View
    private void initView(Context context) {
        layout = new FrameLayout(this);
        layout.addView(mMapView);
    }

    // 设置地图config文件路径
    private void setMapCustomFile() {
       String str =Environment.getExternalStorageState().toString()+"/BaiduMapSDKNew/vmp/";
        File file = new File(str);
            if (!(file.exists())) {
                try {
                    new File(str).createNewFile();
                    InputStream iinput = openFileInput(str + ".cfg");
                    FileOutputStream output = new FileOutputStream(str);
                    byte[] buffer = new byte[8192];
                    int i = 0;
                    while ((i = iinput.read(buffer)) > 0) {
                        output.write(buffer, 0, i);
                    }
                    output.close();
                    iinput.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }

    @Override
    protected void onPause() {
        super.onPause();
        
        mMapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
      
        mMapView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
      
        mMapView.onDestroy();
    }

    @Override
    public void onGetOfflineMapState(int i, int i1) {

    }
}

四、完美展示!

文章有什么不对的地方,还望大神指出。

如果文章对你有用,请点个赞!

 

 

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值