经纬度和地址的转换
在实际开发中,我们会根据地理坐标查询地址信息(包含经度和纬度)
我们可以使用geocode返回结果在MKSearchListener里的onGetAddrResult方法
reverseGeocode返回结果在MKSearchListener里的onGetAddrResult方法
根据关键词查询所在的位置
综合实战案例:经纬度以及根据关键字查询
package com.android.android_baidu_reversemap; import java.util.ArrayList; import java.util.List; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.GeoPoint; import com.baidu.mapapi.ItemizedOverlay; import com.baidu.mapapi.MKAddrInfo; import com.baidu.mapapi.MKBusLineResult; import com.baidu.mapapi.MKDrivingRouteResult; import com.baidu.mapapi.MKGeneralListener; import com.baidu.mapapi.MKPlanNode; import com.baidu.mapapi.MKPoiResult; import com.baidu.mapapi.MKSearch; import com.baidu.mapapi.MKSearchListener; import com.baidu.mapapi.MKSuggestionResult; import com.baidu.mapapi.MKTransitRouteResult; import com.baidu.mapapi.MKWalkingRouteResult; import com.baidu.mapapi.MapActivity; import com.baidu.mapapi.MapView; import com.baidu.mapapi.OverlayItem; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends MapActivity implements OnClickListener{ // 添加百度地图的相关控件 private MapView mapView; // 加载地图的引擎 private BMapManager bMapManager; // 百度地图的key private String KeyString = "8C426377B3526B6BF3672C9BA3BB544D1E8289CD"; // 用于位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索等 private MKSearch mkSearch; //定义一个起始点,终点 private MKPlanNode start; private MKPlanNode end; private Button button1,button2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) this.findViewById(R.id.bmapView); button1 = (Button) this.findViewById(R.id.reverseCode); button1.setOnClickListener(this); button2 = (Button) this.findViewById(R.id.geocode); button2.setOnClickListener(this); bMapManager = new BMapManager(MainActivity.this); // 必须要加载key bMapManager.init(KeyString, new MKGeneralListener() { public void onGetPermissionState(int arg0) { } public void onGetNetworkState(int arg0) { if (arg0 == 300) { Toast.makeText(MainActivity.this, "输入的key有错,请核实!!", 1).show(); } } }); bMapManager.start(); this.initMapActivity(bMapManager); mapView.setBuiltInZoomControls(true); //实例化地图的查询类 mkSearch = new MKSearch(); mkSearch.init(bMapManager, new MySearchListener()); //给起始点构建一个坐标 start = new MKPlanNode(); start.pt = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6)); end = new MKPlanNode(); end.pt = new GeoPoint(40057031,116307852); //mkSearch.transitSearch("北京", start, end); } public class OverItemT extends ItemizedOverlay<OverlayItem>{ //可以从服务器端 接受一组经纬度标注在地图上 private List<OverlayItem> list = new ArrayList<OverlayItem>(); public OverItemT(Drawable marker,Context context,GeoPoint geoPoint,String title) { super(boundCenterBottom(marker)); // TODO Auto-generated constructor stub list.add(new OverlayItem(geoPoint, title, null)); populate(); } @Override protected OverlayItem createItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public int size() { // TODO Auto-generated method stub return list.size(); } } public class MySearchListener implements MKSearchListener { public void onGetAddrResult(MKAddrInfo res, int error) { // TODO Auto-generated method stub if(error!=0){ String msg = String.format("错误号:%d",error); Toast.makeText(MainActivity.this, msg, 1).show(); return; } mapView.getController().animateTo(res.geoPt); String msg = String.format("纬度:%f 经度:%f\r\n", res.geoPt.getLatitudeE6()/1e6,res.geoPt.getLongitudeE6()/1e6); Drawable drawable = getResources().getDrawable(R.drawable.icon_marka); //让图片直接标注在地图上,设置图片的位置 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); mapView.getOverlays().clear(); //创建一个图层 //mapView.getOverlays().add(); } public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) { // TODO Auto-generated method stub } public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) { // TODO Auto-generated method stub } public void onGetPoiResult(MKPoiResult result, int type, int iError) { // TODO Auto-generated method stub if(result==null||iError!=0){ Toast.makeText(MainActivity.this,"解析失败", 1).show(); return; } //查询的点数大于0,表示查找到 if(result!=null&&result.getCurrentNumPois()>0){ GeoPoint geoPoint = result.getAllPoi().get(0).pt; //将查找到的点标注在地图上 mapView.getController().animateTo(geoPoint); //从GeoPoint中取出经纬度转换 String msg = String.format("纬度:%f 经度:%f\f\n", geoPoint.getLatitudeE6()/1e6,geoPoint.getLongitudeE6()/1e6); msg+="\r\n附近有:"; for(int i=0;i<result.getAllPoi().size();i++){ msg+=(result.getAllPoi().get(i).name+";"); } Toast.makeText(MainActivity.this, msg, 1).show(); } } public void onGetRGCShareUrlResult(String arg0, int arg1) { // TODO Auto-generated method stub } public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) { // TODO Auto-generated method stub } public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) { // TODO Auto-generated method stub } public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) { // TODO Auto-generated method stub } } @Override protected void onDestroy() { super.onDestroy(); if (bMapManager != null) { bMapManager.destroy(); bMapManager = null; } } @Override protected void onResume() { super.onResume(); if (bMapManager != null) { bMapManager.start(); } } @Override protected void onPause() { super.onPause(); if (bMapManager != null) { bMapManager.stop(); } } @Override protected boolean isRouteDisplayed() { return false; } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.reverseCode: GeoPoint geoPoint = new GeoPoint(39904965,116327764); mkSearch.reverseGeocode(geoPoint); break; case R.id.geocode: //查询所在的省市 EditText city = (EditText) this.findViewById(R.id.city); //查询所在的地址 EditText geocodekey = (EditText) this.findViewById(R.id.geocodekey); mkSearch.geocode(city.getText().toString(), geocodekey.getText().toString()); break; default: break; } } }