百度地图--poi搜索
BaiduMap mBaidumap; PoiSearch mPoiSearch = null; LatLng mCenterlLatLng = null; private double lng; private double lat;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { SDKInitializer.initialize(getApplicationContext()); } catch (Exception e) { // TODO: handle exception ToastUtils.shortToast(getApplicationContext(), "地图api初始化失败"); return; } setContentView(R.layout.activity_yi_yuan_map); initSystemBar(this); initTitle(); lng = Constant.lng; lat = Constant.lat; if (lat!=0&&lng!=0){ mCenterlLatLng=new LatLng(lat, lng); }else { Toast.makeText(this,"定位失败",Toast.LENGTH_LONG).show(); } // 初始化搜索模块,注册搜索事件监听 mPoiSearch = PoiSearch.newInstance(); mPoiSearch.setOnGetPoiSearchResultListener(this); //mMapView = (MapView) findViewById(R.id.bmapView); mBaidumap = ((SupportMapFragment) (getSupportFragmentManager() .findFragmentById(R.id.map))).getBaiduMap(); new Thread(new Runnable() { @Override public void run() { try { Thread.currentThread().sleep(5000); //阻断2秒 } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword("医院").sortType(PoiSortType.distance_from_near_to_far).location(mCenterlLatLng) .radius(500).pageNum(0); mPoiSearch.searchNearby(nearbySearchOption); }
@Override protected void onDestroy() { mPoiSearch.destroy(); super.onDestroy(); }@Override protected void onResume() { super.onResume(); }@Override protected void onPause() { super.onPause(); }@Override public void onGetPoiResult(PoiResult result) { if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) { Toast.makeText(YiYuanMapActivity.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(); BitmapDescriptor centerBitmap = BitmapDescriptorFactory .fromResource(R.drawable.icon_geo); MarkerOptions ooMarker = new MarkerOptions().position(mCenterlLatLng).icon(centerBitmap); mBaidumap.addOverlay(ooMarker); return; } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {// 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表 String strInfo = "在"; for (CityInfo cityInfo : result.getSuggestCityList()) { strInfo += cityInfo.city; strInfo += ","; } strInfo += "找到结果"; Toast.makeText(YiYuanMapActivity.this, strInfo, Toast.LENGTH_LONG) .show(); } }@Override public void onGetPoiDetailResult(PoiDetailResult result) { if (result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(YiYuanMapActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT) .show(); } else { Toast.makeText(YiYuanMapActivity.this, result.getName() + ": " + result.getAddress(), Toast.LENGTH_SHORT) .show(); } }@Override public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {}@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); }@Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); }private class MyPoiOverlay extends PoiOverlay { public MyPoiOverlay(BaiduMap arg0) { super(arg0); }@Override public boolean onPoiClick(int arg0) { super.onPoiClick(arg0); PoiInfo poiInfo = getPoiResult().getAllPoi().get(arg0); // 检索poi详细信息 mPoiSearch.searchPoiDetail(new PoiDetailSearchOption() .poiUid(poiInfo.uid)); return true; } }
文章图片
推荐阅读
- 拿着旧地图,找不到新大陆
- 百度云极速下载,来体验飞的感觉,还可以看最新动漫的百度云视频哦
- 地图|地图 旅行 菜系
- 虎刺梅
- 假如给我三天光明
- 儿时的年味/记忆中的年味
- 斗罗大陆1(第39集)百度云下载【1080熟肉共享】完整版资源
- 嘘,小声点
- 为什么你有那么多百度微积分(你的微积分是哪里来的?)
- 百度地图多边形时候射线问题2018-09-01