转发请标明出处:http://blog.csdn.net/tanzuai/article/details/43793455
本篇博客要实现的功能
先上效果图:
下面我们来介绍怎么一一实现
1、功能介绍
对于官方开发指南对于离线地图的教程,提供了两种方案:
第一,手动导入,先将从官网下载的离线包解压,把vmp文件夹拷入SD卡根目录下的BaiduMapSDK文件夹内。
第二,接口下载方法如下:mOffline.start(cityid);还比较靠谱,我相信这是大家最喜欢的。
a 、类 MKOfflineMap 提供地图的下载,离线地图列表的获取,已下载地图的查询等
java.util.ArrayList<MKOLUpdateElement> getAllUpdateInfo() 返回各城市离线地图更新信息
java.util.ArrayList<MKOLSearchRecord> getHotCityList() 返回热门城市列表
java.util.ArrayList<MKOLSearchRecord> getOfflineCityList() 返回支持离线地图城市列表
MKOLUpdateElement getUpdateInfo(int cityID) 返回指定城市ID离线地图更新信息
java.util.ArrayList<MKOLSearchRecord> searchCity(java.lang.String cityName) 根据城市名搜索该城市离线地图记录
boolean init(MKOfflineMapListener listener) 初使化
boolean pause(int cityID) 暂停下载指定城市ID的离线地图
boolean remove(int cityID) 删除指定城市ID的离线地图
boolean start(int cityID) 启动下载指定城市ID的离线地图
void destroy() 销毁离线地图管理模块,不用时调用
接口 MKOfflineMapListener 离线地图事件通知接口。
void onGetOfflineMapState(int type, int state)
类 MKOLUpdateElement 和 类 MKOLSearchRecord
基本就是包含一些cityName , cityId, size 等等
3.实例介绍
3.1 初始化布局控件
/** * 初始化布局控件 */ private void initOfflinelayout() { cidView = (TextView) findViewById(R.id.cityid);//城市id cityNameView = (EditText) findViewById(R.id.city);//城市名称 stateView = (TextView) findViewById(R.id.state);//下载状态 hotCityList = (ListView) findViewById(R.id.hotcitylist);//热门城市 allCityList = (ListView) findViewById(R.id.allcitylist);//全国城市 allcity = (LinearLayout) findViewById(R.id.citylist_layout);//全国城市总布局 localcity = (LinearLayout) findViewById(R.id.localmap_layout);//已下载城市布局 localMapListView = (ListView) findViewById(R.id.localmaplist);//已下载城市 }
3.2 初始化离线地图
/** * 初始化离线地图 */ private void initOfflineMap() { mOffline = new MKOfflineMap(); mOffline.init(this); // 获取热闹城市 records1 = mOffline.getHotCityList(); if (records1 != null) { for (MKOLSearchRecord r : records1) { hotCities.add(r.cityName + "(" + r.cityID + ")" + " --"+ this.formatDataSize(r.size)); } } hAdapter = (ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hotCities); hotCityList.setAdapter(hAdapter); // 获取所有支持离线地图的城市 allCities = new ArrayList<String>(); records2 = mOffline.getOfflineCityList(); if (records1 != null) { for (MKOLSearchRecord r : records2) { allCities.add(r.cityName + "(" + r.cityID + ")" + " --"+ this.formatDataSize(r.size)); } } allAdapter = (ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, allCities); allCityList.setAdapter(allAdapter); allcity.setVisibility(View.GONE); localcity.setVisibility(View.VISIBLE); // 获取已下过的离线地图信息 localMapList = mOffline.getAllUpdateInfo(); if (localMapList == null) { localMapList = new ArrayList<MKOLUpdateElement>(); } ListView localMapListView = (ListView) findViewById(R.id.localmaplist); lAdapter = new LocalMapAdapter(); localMapListView.setAdapter(lAdapter); }3.3.初始化监听器
@Override public void onGetOfflineMapState(int type, int state) { switch (type) { case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: { MKOLUpdateElement update = mOffline.getUpdateInfo(state); // 处理下载进度更新提示 if (update != null) { stateView.setText(String.format("%s : %d%%", update.cityName,update.ratio)); updateView(); } } break; case MKOfflineMap.TYPE_NEW_OFFLINE: // 有新离线地图安装 Log.d("OfflineDemo", String.format("add offlinemap num:%d", state)); break; case MKOfflineMap.TYPE_VER_UPDATE: // 版本更新提示 // MKOLUpdateElement e = mOffline.getUpdateInfo(state); break; } }
3.4配置适配器
/** * 离线地图管理列表适配器 */ public class LocalMapAdapter extends BaseAdapter { @Override public int getCount() { return localMapList.size(); } @Override public Object getItem(int index) { return localMapList.get(index); } @Override public long getItemId(int index) { return index; } @Override public View getView(int index, View view, ViewGroup arg2) { MKOLUpdateElement e = (MKOLUpdateElement) getItem(index); view = View.inflate(OfflineActivity.this,R.layout.offline_localmap_list, null); initViewItem(view, e); return view; } void initViewItem(View view, final MKOLUpdateElement e) { Button display = (Button) view.findViewById(R.id.display); Button remove = (Button) view.findViewById(R.id.remove); TextView title = (TextView) view.findViewById(R.id.title); TextView update = (TextView) view.findViewById(R.id.update); TextView ratio = (TextView) view.findViewById(R.id.ratio); ratio.setText(e.ratio + "%"); title.setText(e.cityName); if (e.update) { update.setText("可更新"); } else { update.setText("最新"); } if (e.ratio != 100) { display.setEnabled(false); } else { display.setEnabled(true); } remove.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { mOffline.remove(e.cityID); updateView(); } }); display.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("x", e.geoPt.longitude); intent.putExtra("y", e.geoPt.latitude); intent.setClass(OfflineActivity.this, MainActivity.class); startActivity(intent); } }); } }3.5下面就来实现每个功能的点击事件
/** * 切换至城市列表 * * @param view */ public void clickCityListButton(View view) { allcity = (LinearLayout) findViewById(R.id.citylist_layout); localcity = (LinearLayout) findViewById(R.id.localmap_layout); allcity.setVisibility(View.GONE); localcity.setVisibility(View.VISIBLE); } /** * 开始下载 * * @param view */ public void start(View view) { int cityid = Integer.parseInt(cidView.getText().toString()); mOffline.start(cityid); clickLocalMapListButton(null); Toast.makeText(this, "开始下载离线地图. cityid: " + cityid, Toast.LENGTH_SHORT) .show(); updateView(); } /** * 切换至下载管理列表 * * @param view */ public void clickLocalMapListButton(View view) { allcity = (LinearLayout) findViewById(R.id.citylist_layout); localcity = (LinearLayout) findViewById(R.id.localmap_layout); allcity.setVisibility(View.VISIBLE); localcity.setVisibility(View.GONE); } /** * 暂停下载 * * @param view */ public void stop(View view) { int cityid = Integer.parseInt(cidView.getText().toString()); mOffline.pause(cityid); Toast.makeText(this, "暂停下载离线地图. cityid: " + cityid, Toast.LENGTH_SHORT).show(); updateView(); } /** * 删除离线地图 * * @param view */ public void remove(View view) { int cityid = Integer.parseInt(cidView.getText().toString()); mOffline.remove(cityid); Toast.makeText(this, "删除离线地图. cityid: " + cityid, Toast.LENGTH_SHORT).show(); updateView(); } /** * 搜索离线需市 * * @param view */ public void search(View view) { ArrayList<MKOLSearchRecord> records = mOffline.searchCity(cityNameView .getText().toString()); if (records == null || records.size() != 1) return; cidView.setText(String.valueOf(records.get(0).cityID)); } /** * 从SD卡导入离线地图安装包 * * @param view */ public void importFromSDCard(View view) { int num = mOffline.importOfflineData(); String msg = ""; if (num == 0) { msg = "没有导入离线包,这可能是离线包放置位置不正确,或离线包已经导入过"; } else { msg = String.format("成功导入 %d 个离线包,可以在下载管理查看", num); } Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); updateView(); }
@Override protected void onPause() { int cityid = Integer.parseInt(cidView.getText().toString()); MKOLUpdateElement temp = mOffline.getUpdateInfo(cityid); if (temp != null && temp.status == MKOLUpdateElement.DOWNLOADING) { mOffline.pause(cityid); } super.onPause(); } @Override protected void onResume() { super.onResume(); } @Override protected void onDestroy() { /** * 退出时,销毁离线地图模块 */ mOffline.destroy(); super.onDestroy(); }
Intent intent = getIntent(); if (intent.hasExtra("x") && intent.hasExtra("y")) { // 当用intent参数时,设置中心点为指定点 Bundle b = intent.getExtras(); LatLng p = new LatLng(b.getDouble("y"), b.getDouble("x")); mMapView = new MapView(this,new BaiduMapOptions().mapStatus(new MapStatus.Builder().target(p).build())); } else { mMapView = new MapView(this, new BaiduMapOptions()); } setContentView(mMapView);
好了!其实也是蛮简单的!看完这篇博客相信大家已经知道离线地图怎么实现了!还有更好的实现方法,大家可以到评论栏发表各自的评论!
下面为源码下载地址:
Android 百度地图 SDK v3_3_0 (三) ---离线地图
原文地址:http://blog.csdn.net/tanzuai/article/details/43793455