<code class = "hljs java" > /* 此Demo用來演示離線地圖的下載和顯示 */ public class OfflineActivity extends Activity implements MKOfflineMapListener { private MKOfflineMap mOffline = null ; private TextView cidView; private TextView stateView; private EditText cityNameView; private HashMap<string, boolean = "" > hashMap = new HashMap<string, boolean = "" >(); //是否已下載; private CityExpandableListAdapter adapter; private HotcityListAdapter hAdapter; private OfflineHandler offlineHandler; private MKOLSearchRecord currentRecord; private ArrayList<mkolupdateelement> loadingList = new ArrayList<mkolupdateelement>(); private ArrayList<mkolupdateelement> loadedList = new ArrayList<mkolupdateelement>(); public HashMap<string,string> clickMap; /** * 已下載的離線地圖信息列表 */ public ArrayList<mkolupdateelement> localMapList = null ; private LocalMapAdapter lAdapter = null ; private loadingMapAdapter dAdapter = null ; protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_offline); ImageButton back = (ImageButton) findViewById(R.id.back); back.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { OfflineActivity. this .finish(); } }); offlineHandler = new OfflineHandler( this ); mOffline = new MKOfflineMap(); mOffline.init( this ); initView(); initCurLocation(); } LocationManager lm = null ; // location管理器 LocationClient mLocClient; private void initCurLocation() { lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (lm != null ) { // 定位初始化 mLocClient = new LocationClient( this ); mLocClient.registerLocationListener( new MyLocationListenner()); LocationClientOption option = new LocationClientOption(); option.setOpenGps( true ); // 打開gps option.setCoorType( "bd09ll" ); // 設置座標類型 option.setPriority(LocationClientOption.NetWorkFirst); //設置網絡優先(不設置,默認是gps優先) option.setAddrType( "all" ); // 返回的定位結果包含地址信息 option.setScanSpan( 10000 ); // 設置發起定位請求的間隔時間爲10s(小於1秒則一次定位) mLocClient.setLocOption(option); mLocClient.start(); } else { SystemUtil.showMessage( "請打開GPS定位設置" ); } } public void setCurrentLocation(String currentLocation) { TextView current = (TextView) findViewById(R.id.current_name); current.setText(currentLocation); currentRecord = search(currentLocation); RelativeLayout currentItem = (RelativeLayout)findViewById(R.id.current_item); currentItem.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { if (hashMap.get(currentRecord.cityName)) { Toast.makeText(OfflineActivity. this , "離線地圖已下載" , Toast.LENGTH_LONG).show(); } else { TextView currentSize = (TextView) v.findViewById(R.id.current_size); currentSize.setText( "正在下載" ); start(currentRecord.cityID); } } }); } public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此處設置開發者獲取到的方向信息,順時針0-360 .direction( 100 ).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); String address=location.getAddrStr(); String city=location.getCity(); // System.out.println("地址:"+address+"城市:"+city); setCurrentLocation(city); } public void onReceivePoi(BDLocation poiLocation) { } } private void initView() { // 獲取已下過的離線地圖信息 localMapList = mOffline.getAllUpdateInfo(); if (localMapList == null ) { localMapList = new ArrayList<mkolupdateelement>(); } ListView localMapListView = (ListView) findViewById(R.id.localmaplist); lAdapter = new LocalMapAdapter(); localMapListView.setAdapter(lAdapter); ListView loadingListView = (ListView)findViewById(R.id.lodinglist); dAdapter = new loadingMapAdapter(); loadingListView.setAdapter(dAdapter); // cidView = (TextView) findViewById(R.id.cityid); // cityNameView = (EditText) findViewById(R.id.city); // stateView = (TextView) findViewById(R.id.state); ListView hotCityList = (ListView) findViewById(R.id.hotcitylist); final ArrayList<integer> hotCities = new ArrayList<integer>(); // 獲取熱門城市列表 final ArrayList<mkolsearchrecord> records1 = mOffline.getHotCityList(); if (records1 != null ) { for (MKOLSearchRecord r : records1) { hotCities.add(r.cityID); } } hAdapter = new HotcityListAdapter( this , records1,hashMap); hotCityList.setAdapter(hAdapter); hotCityList.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<!--?--> parent, View view, int position, long id) { if (hashMap.get(records1.get(position).cityName)) { Toast.makeText(OfflineActivity. this , "離線地圖已下載" , Toast.LENGTH_LONG).show(); } else { TextView childSize = (TextView) view.findViewById(R.id.child_size); childSize.setText( "正在下載" ); start(records1.get(position).cityID); } } }); ExpandableListView allCityList = (ExpandableListView) findViewById(R.id.allcitylist); // 獲取全部支持離線地圖的城市 final ArrayList<mkolsearchrecord> records2 = mOffline.getOfflineCityList(); clickMap = new HashMap<string, string= "" >(); if (records1 != null ) { for (MKOLSearchRecord r : records2) { // allCities.add(r.cityName+"--" + this.formatDataSize(r.size)); // allCitiyIds.add(r.cityID); hashMap.put(r.cityName,downList(r.cityName)); clickMap.put(r.cityName, "0" ); if (r.childCities != null && r.childCities.size() != 0 ) { ArrayList<mkolsearchrecord> childrecord = r.childCities; // for (MKOLSearchRecord cr : childrecord) { hashMap.put(cr.cityName,downList(cr.cityName)); } } } } adapter = new CityExpandableListAdapter( this ,records2,hashMap); allCityList.setAdapter(adapter); allCityList.setGroupIndicator( null ); hAdapter.notifyDataSetChanged(); allCityList.setOnGroupClickListener( new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { if (records2 != null ) { MKOLSearchRecord record = records2.get(groupPosition); if (record.childCities == null ) { if (hashMap.get(record.cityName)) { Toast.makeText(OfflineActivity. this , "離線地圖已下載" , Toast.LENGTH_LONG).show(); } else { // System.out.println("simplename:"+v.getClass().getSimpleName()); int cd = record.cityID; start(cd); /* int size = ((ViewGroup)v).getChildCount(); for (int i = 0 ; i< size; i++) { View child = ((ViewGroup)v).getChildAt(i); System.out.println("simplename:"+child.getClass().getSimpleName()); } View child = ((ViewGroup)v).getChildAt(1); ((TextView)child).setText("正在下載");*/ clickMap.put(record.cityName, "1" ); // adapter.notifyDataSetChanged(); } } } return false ; } }); allCityList.setOnChildClickListener( new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { if (records2 != null ) { MKOLSearchRecord record = records2.get(groupPosition); MKOLSearchRecord cldred = record.childCities.get(childPosition); if (hashMap.get(cldred.cityName)) { Toast.makeText(OfflineActivity. this , "離線地圖已下載" , Toast.LENGTH_LONG).show(); } else { TextView childSize = (TextView) v.findViewById(R.id.child_size); childSize.setText( "正在下載" ); start(cldred.cityID); } } return false ; } }); LinearLayout cl = (LinearLayout) findViewById(R.id.citylist_layout); LinearLayout lm = (LinearLayout) findViewById(R.id.localmap_layout); lm.setVisibility(View.GONE); cl.setVisibility(View.VISIBLE); } public boolean downList(String cityName) { Boolean flag = false ; if (localMapList != null ) { for ( int i = 0 ; i <localmaplist.size(); button= "" cl= "(LinearLayout)" clbutton= "(Button)findViewById(R.id.clButton);" element= "localMapList.get(i);" else = "" flag= "true;" if = "" linearlayout= "" lm= "(LinearLayout)" localbutton= "(Button)" mkolsearchrecord= "" mkolupdateelement= "" param= "" public = "" return = "" string= "" view= "" void = "" > records = mOffline.searchCity(city); if (records == null || records.size() != 1 ) { return null ; } // cidView.setText(String.valueOf(records.get(0).cityID)); TextView current_size = (TextView) findViewById(R.id.current_size); if (hashMap.get(records.get( 0 ).cityName)) { current_size.setText( "已下載" ); } else { current_size.setText(formatDataSize(records.get( 0 ).size)); } return records.get( 0 ); } /** * 開始下載 * * @param */ public void start( int cityid) { // int cityid = Integer.parseInt(cidView.getText().toString()); mOffline.start(cityid); clickLocalMapListButton( null ); // Toast.makeText(this, "開始下載離線地圖. cityid: " + cityid, Toast.LENGTH_SHORT).show(); updateView( null , false ); } /** * 暫停下載 * * @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( null , false ); } /** * 刪除離線地圖 * * @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( null , false ); } /** * 更新狀態顯示 */ public void updateView(MKOLUpdateElement element, boolean flag) { localMapList = mOffline.getAllUpdateInfo(); if (localMapList == null ) { localMapList = new ArrayList<mkolupdateelement>(); } loadingList.clear(); loadedList.clear(); for (MKOLUpdateElement element1 : localMapList) { if (element1.ratio != 100 ) { loadingList.add(element1); } else { loadedList.add(element1); } } if (element != null ) { hashMap.put(element.cityName, flag); if (currentRecord.cityID == element.cityID) { TextView currentSize = (TextView) findViewById(R.id.current_size); if (flag) { currentSize.setText( "已下載" ); } else { currentSize.setText(formatDataSize(element.size)); } } else { adapter.notifyDataSetChanged(); hAdapter.notifyDataSetChanged(); } } lAdapter.notifyDataSetChanged(); dAdapter.notifyDataSetChanged(); } @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(); } public String formatDataSize( int size) { String ret = "" ; if (size < ( 1024 * 1024 )) { ret = String.format( "%dK" , size / 1024 ); } else { ret = String.format( "%.1fM" , size / ( 1024 * 1024.0 )); } return ret; } @Override protected void onDestroy() { /** * 退出時,銷燬離線地圖模塊 */ mOffline.destroy(); super .onDestroy(); } @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)); // System.out.println("ratio:"+update.ratio); if (update.ratio == 100 ) { updateView(update, true ); } else { updateView( null , false ); } } } 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 ; default : break ; } } /** * 正在下載城市列表適配器 */ public class loadingMapAdapter extends BaseAdapter{ @Override public int getCount() { return loadingList.size(); } @Override public Object getItem( int position) { return loadingList.get(position); } @Override public long getItemId( int position) { return position; } @Override public View getView( int position, View convertView, ViewGroup parent) { if (convertView == null ) { convertView = LayoutInflater.from(OfflineActivity. this ).inflate(R.layout.loding_list, null ); } TextView name = (TextView) convertView.findViewById(R.id.city_name); TextView size = (TextView)convertView.findViewById(R.id.city_size); TextView ratio = (TextView)convertView.findViewById(R.id.down_ratio); ImageButton manager= (ImageButton) convertView.findViewById(R.id.down_manager); final MKOLUpdateElement ele = loadingList.get(position); name.setText(ele.cityName); size.setText(formatDataSize(ele.size)); ratio.setText(ele.ratio+ "%" ); manager.setOnClickListener( new OnClickListener() { boolean flag = true ; @Override public void onClick(View v) { if (flag) { mOffline.pause(ele.cityID); v.setBackgroundResource(R.drawable.loading_start); flag = false ; } else { mOffline.start(ele.cityID); v.setBackgroundResource(R.drawable.loading_pause); flag = true ; } } }); return convertView; } } /** * 離線地圖管理列表適配器 */ public class LocalMapAdapter extends BaseAdapter { @Override public int getCount() { return loadedList.size(); } @Override public Object getItem( int index) { return loadedList.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 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); Button doUpdate = (Button) view.findViewById(R.id.exe_update); // ratio.setText(e.ratio + "%"); title.setText(e.cityName); if (e.update) { update.setText( "可更新" ); } else { update.setText( "最新" ); } remove.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { mOffline.remove(e.cityID); clickMap.put(e.cityName, "0" ); updateView(e, false ); } }); doUpdate.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { mOffline.update(e.cityID); } }); } } }</mkolupdateelement></localmaplist.size();></mkolsearchrecord></string,></mkolsearchrecord></mkolsearchrecord></integer></integer></mkolupdateelement></mkolupdateelement></string,string></mkolupdateelement></mkolupdateelement></mkolupdateelement></mkolupdateelement></string,></string,></code> |