package baidumapsdk.demo.search;java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Toast;android
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.GroundOverlayOptions;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.overlayutil.PoiOverlay;api
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiBoundSearchOption;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;app
import java.util.ArrayList;
import java.util.List;
import baidumapsdk.demo.R;ide
/**ui
演示poi搜索功能
*/
public class PoiSearchDemo extends FragmentActivity implements
OnGetPoiSearchResultListener, OnGetSuggestionResultListener {this
LatLng center = new LatLng(39.92235, 116.380338);
int radius = 100;
LatLng southwest = new LatLng( 39.92235, 116.380338 );
LatLng northeast = new LatLng( 39.947246, 116.414977);
LatLngBounds searchbound = new LatLngBounds.Builder().include(southwest).include(northeast).build();spa
int searchType = 0; // 搜索的類型,在顯示時區分code
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poisearch);
// 初始化搜索模塊,註冊搜索事件監聽
mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(this);事件
// 初始化建議搜索模塊,註冊建議搜索事件監聽 mSuggestionSearch = SuggestionSearch.newInstance(); mSuggestionSearch.setOnGetSuggestionResultListener(this); editCity = (EditText) findViewById(R.id.city); keyWorldsView = (AutoCompleteTextView) findViewById(R.id.searchkey); sugAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line); keyWorldsView.setAdapter(sugAdapter); keyWorldsView.setThreshold(1); mBaiduMap = ((SupportMapFragment) (getSupportFragmentManager() .findFragmentById(R.id.map))).getBaiduMap(); /** * 當輸入關鍵字變化時,動態更新建議列表 */ keyWorldsView.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { if (cs.length() <= 0) { return; } /** * 使用建議搜索服務獲取建議列表,結果在onSuggestionResult()中更新 */ mSuggestionSearch .requestSuggestion((new SuggestionSearchOption()) .keyword(cs.toString()).city(editCity.getText().toString())); } });
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
mPoiSearch.destroy();
mSuggestionSearch.destroy();
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
public void goToNextPage(View v) {
loadIndex++;
searchButtonProcess(null);
}
}
/**@param result
*/
public void onGetPoiResult(PoiResult result) {
if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(PoiSearchDemo.this, "未找到結果", Toast.LENGTH_LONG)
.show();
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
mBaiduMap.clear();
PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result);
overlay.addToMap();
overlay.zoomToSpan();
switch( searchType ) { case 2: showNearbyArea(center, radius); break; case 3: showBound(searchbound); break; default: break; } return;
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
// 當輸入關鍵字在本市沒有找到,但在其餘城市找到時,返回包含該關鍵字信息的城市列表 String strInfo = "在"; for (CityInfo cityInfo : result.getSuggestCityList()) { strInfo += cityInfo.city; strInfo += ","; } strInfo += "找到結果"; Toast.makeText(PoiSearchDemo.this, strInfo, Toast.LENGTH_LONG) .show();
}
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
}
/**private class MyPoiOverlay extends PoiOverlay {
public MyPoiOverlay(BaiduMap baiduMap) { super(baiduMap); } @Override public boolean onPoiClick(int index) { super.onPoiClick(index); PoiInfo poi = getPoiResult().getAllPoi().get(index); // if (poi.hasCaterDetails) { mPoiSearch.searchPoiDetail((new PoiDetailSearchOption()) .poiUid(poi.uid)); // } return true; }
}
/**@param radius
*/
public void showNearbyArea( LatLng center, int radius) {
BitmapDescriptor centerBitmap = BitmapDescriptorFactory
.fromResource(R.drawable.icon_geo);
MarkerOptions ooMarker = new MarkerOptions().position(center).icon(centerBitmap);
mBaiduMap.addOverlay(ooMarker);
OverlayOptions ooCircle = new CircleOptions().fillColor( 0xCCCCCC00 )
.center(center).stroke(new Stroke(5, 0xFFFF00FF ))
.radius(radius);
mBaiduMap.addOverlay(ooCircle);
}
@param bounds
*/
public void showBound( LatLngBounds bounds) {
BitmapDescriptor bdGround = BitmapDescriptorFactory
.fromResource(R.drawable.ground_overlay);
OverlayOptions ooGround = new GroundOverlayOptions()
.positionFromBounds(bounds).image(bdGround).transparency(0.8f);
mBaiduMap.addOverlay(ooGround);
MapStatusUpdate u = MapStatusUpdateFactory
.newLatLng(bounds.getCenter());
mBaiduMap.setMapStatus(u);
bdGround.recycle(); } }