前言
最近公司项目需求添加百度地图离线下载后离线地图查看功能,本以为看着文档写一下就行了,看了文档后发现百度文档着实坑爹,文档与实际开发不符合,网上搜了一下,也没搜到切实有用的文章,遂决定写一篇;
步骤
一;百度离线地图下载完成后,会在手机根目录中生成一个BaiduMapSDKNew的文件,打开里面的vmp文件夹会看到下载到的离线地图包;
二,给离线地图下载完成的列表添加点击事件;
Adapter里面:
//查看离线地图
listItemView.name.setOnClickListener(new View.OnClickListener() {@Overridepublic 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;@Overrideprotected 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);}// 初始化Viewprivate 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();}}}@Overrideprotected void onPause() {super.onPause();mMapView.onPause();}@Overrideprotected void onResume() {super.onResume();mMapView.onResume();}@Overrideprotected void onDestroy() {super.onDestroy();mMapView.onDestroy();}@Overridepublic void onGetOfflineMapState(int i, int i1) {}
}
四、完美展示!
文章有什么不对的地方,还望大神指出。
如果文章对你有用,请点个赞!















