android POI搜索,附近搜索,周边搜索定位介绍
Posted cxchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android POI搜索,附近搜索,周边搜索定位介绍相关的知识,希望对你有一定的参考价值。
POI搜索有三种方式。依据范围和检索词发起范围检索poiSearchInbounds。城市poi检索poiSearchInCity,周边检索poiSearchNearBy。
下以周边检索为例介绍怎样进行检索并显示覆盖物PoiOverlay:
public int poiSearchNearBy(java.lang.String key, GeoPoint pt, int radius)
- 依据中心点、半径与检索词发起周边检索.
- 异步函数,返回结果在MKSearchListener里的onGetPoiResult方法通知
- 參数:
- key - 关键词
- pt - 中心点地理坐标
- radius - 半径。单位:米
- 返回:
- 成功返回0,否则返回-1
- mMKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);
实现MySearchListener的onGetPoiResult,并展示检索结果:
- public void onGetPoiResult(MKPoiResult result, int type, int iError) {
- if (result == null) {
- return;
- }
- PoiOverlay poioverlay = new PoiOverlay(MyMapActivity.this, mMapView);
- poioverlay.setData(result.getAllPoi());
- mMapView.getOverlays().add(poioverlay);
- }
详细实现:
- package xiaosi.baiduMap;
- import android.os.Bundle;
- import com.baidu.mapapi.BMapManager;
- import com.baidu.mapapi.GeoPoint;
- import com.baidu.mapapi.MKAddrInfo;
- import com.baidu.mapapi.MKDrivingRouteResult;
- import com.baidu.mapapi.MKPoiResult;
- import com.baidu.mapapi.MKSearch;
- import com.baidu.mapapi.MKSearchListener;
- import com.baidu.mapapi.MKTransitRouteResult;
- import com.baidu.mapapi.MKWalkingRouteResult;
- import com.baidu.mapapi.MapActivity;
- import com.baidu.mapapi.MapController;
- import com.baidu.mapapi.MapView;
- import com.baidu.mapapi.PoiOverlay;
- public class BaiduMapActivity extends MapActivity
- {
- /** Called when the activity is first created. */
- private BMapManager mapManager = null;
- private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
- private MapView mapView = null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mapManager = new BMapManager(getApplication());
- mapManager.init(key, null);
- super.initMapActivity(mapManager);
- mapView = (MapView) findViewById(R.id.mapsView);
- mapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
- MapController mapController = mapView.getController(); // 得到mMapView的控制权,能够用它控制和驱动平移和缩放
- mapController.setZoom(12); // 设置地图zoom级别
- MKSearch mKSearch = new MKSearch();
- mKSearch.init(mapManager, new MySearchListener());// 注意。MKSearchListener仅仅支持一个。以最后一次设置为准
- mKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6),
- (int) (116.404 * 1E6)), 5000);
- }
- public class MySearchListener implements MKSearchListener
- {
- public void onGetAddrResult(MKAddrInfo arg0, int arg1)
- {}
- public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
- {}
- public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
- {
- if (arg0 == null) {
- return;
- }
- PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this, mapView);
- poioverlay.setData(arg0.getAllPoi());
- mapView.getOverlays().add(poioverlay);
- }
- public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
- {}
- public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
- {}
- }
- @Override
- protected boolean isRouteDisplayed()
- {
- return false;
- }
- @Override
- protected void onDestroy()
- {
- if (mapManager != null)
- {
- mapManager.destroy();
- mapManager = null;
- }
- super.onDestroy();
- }
- @Override
- protected void onPause()
- {
- if (mapManager != null)
- {
- mapManager.stop();
- }
- super.onPause();
- }
- @Override
- protected void onResume()
- {
- if (mapManager != null)
- {
- mapManager.start();
- }
- super.onResume();
- }
- }
以上是关于android POI搜索,附近搜索,周边搜索定位介绍的主要内容,如果未能解决你的问题,请参考以下文章
Unity与Android交互-Unity接入高德地图实现定位以及搜索周边的功能(使用Android Studio)详细操作