Android学习笔记之百度地图(根据地名查询经纬度)

本文介绍如何使用百度地图API的geocode方法进行地址解析,包括将地址名转换为经纬度坐标,并展示了具体的Java代码实现。通过异步函数geocode,可以获取地址信息,成功返回0,否则返回-1。
               



重要方法:

public int geocode(java.lang.String strAddr, java.lang.String city)

根据地址名获取地址信息 

异步函数,返回结果在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/** Called when the activity is first created. */ private BMapManager mapManager = nullprivate String key = "1B79478DA01F7800AEA8602517A6D89B38151105"private MapView mapView = nullprivate 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);  // 得到mMapView的控制权,可以用它控制和驱动平移和缩放  mapController = mapView.getController();  // 设置地图zoom级别  mapController.setZoom(12);  mKSearch = new MKSearch();  // 注意,MKSearchListener只支持一个,以最后一次设置为准  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)  {   /*    * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息    */   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)  {   /*    * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回    */  }  public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)  {   /*    * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回    */  }  public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)  {   /*    * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息    */  }  public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)  {   /*    * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回    */  } } @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(); }}




           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值