重要方法:
public int geocode(java.lang.String strAddr, java.lang.String city)
根据地址名获取地址信息
www.itbole.net发表
异步函数,返回结果在MKSearchListener里的onGetAddrResult方法通知
参数:
strAddr - 地址名
city - 地址所在城市
返回:
成功返回0,否则返回-1
具体实现:
- package xiaosi.baiduMap;
-
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
-
- import com.baidu.mapapi.BMapManager;
- 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;
-
- public class BaiduMapActivity extends MapActivity
- {
-
- private BMapManager mapManager = null;
- private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
- private MapView mapView = null;
- private MKSearch mKSearch;
- private MapController mapController = null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
-
- private void init()
- {
- mapManager = new BMapManager(getApplication());
- mapManager.init(key, null);
- super.initMapActivity(mapManager);
- mapView = (MapView) findViewById(R.id.mapView);
-
- mapView.setBuiltInZoomControls(true);
-
- mapController = mapView.getController();
-
- mapController.setZoom(12);
- mKSearch = new MKSearch();
-
- mKSearch.init(mapManager, new MySearchListener());
- if (mKSearch.geocode("五四广场", "青岛") == 0)
- {
- System.out.println("搜索成功");
- }
- else
- {
- System.out.println("搜索失败");
- }
- }
-
- public class MySearchListener implements MKSearchListener
- {
- public void onGetAddrResult(MKAddrInfo arg0, int arg1)
- {
-
-
-
- String Location = null;
- if (arg0 == null)
- {
- Location = "没有搜索到该地址";
- }
- else
- {
-
- Location = "纬度:" + arg0.geoPt.getLatitudeE6() / 1E6 + "\n"
- + "经度:" + arg0.geoPt.getLongitudeE6() / 1E6 + "\n";
- mapController.animateTo(arg0.geoPt);
- }
- AlertDialog.Builder builder = new AlertDialog.Builder(
- BaiduMapActivity.this);
- builder.setTitle("搜索结果");
- builder.setMessage(Location);
- builder.setPositiveButton("关闭",
- new android.content.DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which)
- {
- dialog.dismiss();
- }
- });
- builder.show();
- }
-
- public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
- {
-
-
-
- }
-
- public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
- {
-
-
-
- }
-
- 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();
- }
- }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28901198/viewspace-761470/
,如需转载,请注明出处,否则将追究法律责任。