前段時間在開發一個地圖相關的軟體,本來要實現Poi搜尋完成後的高亮,類似高德官方的地圖,如下圖:

用高德的API一直不得要領,沒辦法,只好自己重新按marker的方法來實現,然後就沒有用高德API提供的POI搜尋類。下面貼出部分程式碼, /** * 目的地非同步搜尋 */@Overridepublic void onPoiSearched(PoiResult result, int rCode) {// TODO Auto-generated method stubloading.dismiss();// 隱藏對話方塊searchCount.setVisibility(View.GONE);if (rCode == 0) {if (result != null && result.getQuery() != null) {// 搜尋poi的結果if (result.getQuery().equals(query)) {// 是否是同一條poiResult = result;// 取得搜尋到的poiitems有多少頁poiItems = poiResult.getPois();// 取得第一頁的poiitem資料,頁數從數字0開始List suggestionCities = poiResult.getSearchSuggestionCitys();// 當搜尋不到poiitem資料時,會返回含有搜尋關鍵字的城市資訊Log.d(TAG, "poiItems---->" + poiItems.toString());if (poiItems != null && poiItems.size() > 0) {aMap.clear();/*註釋掉高德原有的PoiOverlay*/// PoiOverlay poiOverlay = new PoiOverlay(aMap,// poiItems);/*poiOverlay = new MyOverlay(aMap, poiItems);poiOverlay.removeFromMap();poiOverlay.addToMap();poiOverlay.zoomToSpan();*/addPoiToMap(poiItems);geoLat = poiItems.get(0).getLatLonPoint().getLatitude();geoLng = poiItems.get(0).getLatLonPoint().getLongitude();aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(geoLat,geoLng), 19.5f));//showPoiMsg(poiItems);Log.d(TAG, "mTarget---->" + mTarget);search_flag = 1;} else if (suggestionCities != null&& suggestionCities.size() > 0) {ToastUtil.show(MainActivity.this, R.string.search_error);} else {ToastUtil.show(MainActivity.this, R.string.no_result);}}} else {ToastUtil.show(MainActivity.this, R.string.no_result);}} else if (rCode == 27) {ToastUtil.show(MainActivity.this, R.string.error_network);} else if (rCode == 32) {ToastUtil.show(MainActivity.this, R.string.error_key);} else if (rCode == 31) {ToastUtil.show(MainActivity.this, "只能搜尋到當前城市停車場資訊!");} else {ToastUtil.show(MainActivity.this, getString(R.string.error_other)+ rCode);}}

      這裡就不用高德本身的API,自己寫了個addPoiToMap的方法然後通過marker的方式直接往地圖上面 private void addPoiToMap(List list) {// TODO Auto-generated method stublistMarkers.clear();for(int i = 0 ; i < list.size(); i++ ){MarkerOptions markerOption = new MarkerOptions();markerOption.position(new LatLng(list.get(i).getLatLonPoint().getLatitude(), list.get(i).getLatLonPoint().getLongitude())).icon(ImageNormal(i+1)).title(i+1+"、"+list.get(i).getTitle()).snippet(list.get(i).getSnippet());listMarkers.add(aMap.addMarker(markerOption));}listMarkers.get(0).setIcon(ImagePress(1));tempMarker = listMarkers.get(0);tempIndex = 1;}

然後新增自定義圖片,這種自定義的新增比複寫PoiOverlay的方式好的多,靈活度比高 private BitmapDescriptor ImageNormal(int i) {View view = null;view = getLayoutInflater().inflate(R.layout.poi_view, null);LinearLayout ly = (LinearLayout) view.findViewById(R.id.poplayout);TextView tv = (TextView) view.findViewById(R.id.poi_mark_img);tv.setText(i + "");Log.d(TAG, "ImageNormal=======>" + i);tv.setPadding(0, 0, 0, 25);tv.setBackgroundResource(R.drawable.poi_mark_normal);BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);return bitmap;}private BitmapDescriptor ImagePress(int i) {View view = null;Log.d(TAG, "imagePress--->");view = getLayoutInflater().inflate(R.layout.poi_view, null);TextView tv = (TextView) view.findViewById(R.id.poi_mark_img);tv.setText(i + "");tv.setPadding(0, 0, 0, 25);Log.d(TAG, "imagePress-iii-->" + i);tv.setBackgroundResource(R.drawable.poi_mark_press);BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);return bitmap;}

然後在maker被點選的時候做處理

@Overridepublic boolean onMarkerClick(Marker marker) {// TODO Auto-generated method stubImageFat(marker ,listMarkers.indexOf(marker)+1);return false;} 

private void ImageFat(Marker marker , int i) {Log.d(TAG,"i--->"+i);if (tempIndex == i) {Log.d(TAG,"if--->");marker.setIcon(ImagePress(i));tempMarker = marker;} else {Log.d(TAG,"else--->");tempMarker.setIcon(ImageNormal(tempIndex));tempIndex = i;tempMarker = marker;Log.d(TAG,"else--->tempIndex "+tempIndex);marker.setIcon(ImagePress(i));}} 

因為自定義了一個List<Marker> listMarkers,用這個listMarkers來裝載所有的poi marker,每次點選的時候通過listMarkers.indexOf方法來確定被點選marker的位置,然後用一個int的變數temp來記錄上一次點選的位置,完成後的效果如下

因為是個大工程,原始碼不好打包上傳,大家看看程式碼就行了。總覺得這樣做有點奇葩,如有更好的辦法希望大家指點下!