转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37758097
一直觉得地图应用支持离线地图很重要啊,我等移动2G屌丝,流量不易,且用且珍惜。
对于官方开发指南对于离线地图的教程,提供了两种方案:
第一,手动导入,先将从官网下载的离线包解压,把vmp文件夹拷入SD卡根目录下的BaiduMapSDK文件夹内。好吧,我表示不能接受,无视了。
第二,接口下载方法如下:mOffline.start(cityid);还比较靠谱,就是没详细介绍。
今天,我们主要对第二种方式进行详细介绍,然后集成到我们的已经集成了定位方向传感器的地图中,如果你还不了解:Android百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
package com.zhy.zhy_baidu_ditu_demo00; public class OfflineMapCityBean { private String cityName; private int cityCode; /** * 下载的进度 */ private int progress; private Flag flag = Flag.NO_STATUS; /** * 下载的状态:无状态,暂停,正在下载 * @author zhy * */ public enum Flag { NO_STATUS,PAUSE,DOWNLOADING } public Flag getFlag() { return flag; } public void setFlag(Flag flag) { this.flag = flag; } public OfflineMapCityBean() { } public OfflineMapCityBean(String cityName, int cityCode, int progress) { this.cityName = cityName; this.cityCode = cityCode; this.progress = progress; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public int getCityCode() { return cityCode; } public void setCityCode(int cityCode) { this.cityCode = cityCode; } public int getProgress() { return progress; } public void setProgress(int progress) { this.progress = progress; } }
/** * 初始化离线地图 */ private void initOfflineMap() { mOfflineMap = new MKOfflineMap(); // 设置监听 mOfflineMap.init(new MKOfflineMapListener() { @Override public void onGetOfflineMapState(int type, int state) { switch (type) { case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: // 离线地图下载更新事件类型 MKOLUpdateElement update = mOfflineMap.getUpdateInfo(state); Log.e(TAG, update.cityName + " ," + update.ratio); for (OfflineMapCityBean bean : mDatas) { if (bean.getCityCode() == state) { bean.setProgress(update.ratio); bean.setFlag(Flag.DOWNLOADING); break; } } mAdapter.notifyDataSetChanged(); Log.e(TAG, "TYPE_DOWNLOAD_UPDATE"); break; case MKOfflineMap.TYPE_NEW_OFFLINE: // 有新离线地图安装 Log.e(TAG, "TYPE_NEW_OFFLINE"); break; case MKOfflineMap.TYPE_VER_UPDATE: // 版本更新提示 break; } } }); }设置离线地图的下载监听接口,目前我们只关注type为MKOfflineMap.TYPE_DOWNLOAD_UPDATE , 此时传入的state为cityId, 然后我们通过mOfflineMap.getUpdateInfo(state);可以获得该城市的下载数据,接下来更新我们listview的数据集,最后刷新界面。
private void initData() { // 获得所有热门城市 ArrayList<MKOLSearchRecord> offlineCityList = mOfflineMap .getHotCityList(); // 手动添加了西安 MKOLSearchRecord xian = new MKOLSearchRecord(); xian.cityID = 233; xian.cityName = "西安市"; offlineCityList.add(xian); // 获得所有已经下载的城市列表 ArrayList<MKOLUpdateElement> allUpdateInfo = mOfflineMap .getAllUpdateInfo(); // 设置所有数据的状态 for (MKOLSearchRecord record : offlineCityList) { OfflineMapCityBean cityBean = new OfflineMapCityBean(); cityBean.setCityName(record.cityName); cityBean.setCityCode(record.cityID); if (allUpdateInfo != null)//没有任何下载记录,返回null,为啥不返回空列表~~ { for (MKOLUpdateElement ele : allUpdateInfo) { if (ele.cityID == record.cityID) { cityBean.setProgress(ele.ratio); } } } mDatas.add(cityBean); } }
private void initListView() { mListView = (ListView) findViewById(R.id.id_offline_map_lv); mAdapter = new MyOfflineCityBeanAdapter(); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int cityId = mDatas.get(position).getCityCode(); if (mCityCodes.contains(cityId)) { removeTaskFromQueue(position, cityId); } else { addToDownloadQueue(position, cityId); } } }); }
/** * 热门城市地图列表的Adapter * * @author zhy * */ class MyOfflineCityBeanAdapter extends BaseAdapter { @Override public boolean isEnabled(int position) { if (mDatas.get(position).getProgress() == 100) { return false; } return super.isEnabled(position); } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { OfflineMapCityBean bean = mDatas.get(position); ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.offlinemap_item, parent, false); holder.cityName = (TextView) convertView .findViewById(R.id.id_cityname); holder.progress = (TextView) convertView .findViewById(R.id.id_progress); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.cityName.setText(bean.getCityName()); int progress = bean.getProgress(); String progressMsg = ""; // 根据进度情况,设置显示 if (progress == 0) { progressMsg = "未下载"; } else if (progress == 100) { bean.setFlag(Flag.NO_STATUS); progressMsg = "已下载"; } else { progressMsg = progress + "%"; } // 根据当前状态,设置显示 switch (bean.getFlag()) { case PAUSE: progressMsg += "【等待下载】"; break; case DOWNLOADING: progressMsg += "【正在下载】"; break; default: break; } holder.progress.setText(progressMsg); return convertView; } private class ViewHolder { TextView cityName; TextView progress; } }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.id_menu_map_offline: Intent intent = new Intent(MainActivity.this, OfflineMapActivity.class); startActivity(intent); break; ... } }
Android 百度地图 SDK v3.0.0 (四) 引入离线地图功能,布布扣,bubuko.com
Android 百度地图 SDK v3.0.0 (四) 引入离线地图功能
原文地址:http://blog.csdn.net/lmj623565791/article/details/37758097