志不强者智不达,言不信者行不果。这篇文章主要讲述android POI搜索,附近搜索,周边搜索定位介绍相关的知识,希望能为你提供帮助。
POI搜索有三种方式。依据范围和检索词发起范围检索poiSearchInbounds。城市poi检索poiSearchInCity,周边检索poiSearchNearBy。
下以周边检索为例介绍怎样进行检索并显示覆盖物PoiOverlay:
【android POI搜索,附近搜索,周边搜索定位介绍】
public int
poiSearchNearBy(java.lang.String key,
GeoPoint
pt,
int radius)
- 依据中心点、半径与检索词发起周边检索.
- 异步函数,返回结果在MKSearchListener里的onGetPoiResult方法通知
- 參数:
- key - 关键词
- pt - 中心点地理坐标
- radius - 半径。单位:米
- 返回:
- 成功返回0,否则返回-1
[java] view plaincopy
- mMKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);
文章图片
实现MySearchListener的onGetPoiResult,并展示检索结果:
[java] view plaincopy
- 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);
- }
详细实现:[java] view plaincopy
- 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();
- }
- }
推荐阅读
- 三. 200多万元得到的创业教训--创业并不须要app
- Android 状态栏那些小坑()
- AppletScriptlet与Servlet
- HDU 5303 Delicious Apples (贪心 枚举 好题)
- Ubuntu使用adb连接android手机失败unknown的解决的方法
- Delphi 10.1.2 berlin开发跨平台APP的几点经验
- Android 流媒体播放 live streaming
- Android的方法和属性
- Appium 连手机失败Error: Android bootstrap socket crashed: Error: getaddrinfo ENOTFOUND localhost undefine