前言
最近公司項目需求添加百度地圖離線下載後離線地圖查看功能,本以爲看着文檔寫一下就行了,看了文檔後發現百度文檔着實坑爹,文檔與實際開發不符合,網上搜了一下,也沒搜到確切實用的文章,遂決定寫一篇;
步驟
一;百度離線地圖下載完成後,會在手機根目錄中生成一個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) { } }
四、完美展示!
文章有什麼不對的地方,還望大神指出。