振兴

壁立千仞,无欲则刚

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1 import android.os.Bundle;
  2 import android.view.View;
  3 import android.view.View.OnClickListener;
  4 import android.widget.Button;
  5 import android.widget.EditText;
  6 import android.widget.TextView;
  7 import com.baidu.mapapi.BMapManager;
  8 import com.baidu.mapapi.GeoPoint;
  9 import com.baidu.mapapi.MKAddrInfo;
 10 import com.baidu.mapapi.MKBusLineResult;
 11 import com.baidu.mapapi.MKDrivingRouteResult;
 12 import com.baidu.mapapi.MKPoiInfo;
 13 import com.baidu.mapapi.MKPoiResult;
 14 import com.baidu.mapapi.MKSearch;
 15 import com.baidu.mapapi.MKSearchListener;
 16 import com.baidu.mapapi.MKTransitRouteResult;
 17 import com.baidu.mapapi.MKWalkingRouteResult;
 18 import com.baidu.mapapi.MapActivity;
 19 
 20 public class QueryAddressActivity extends MapActivity {
 21 
 22     private BMapManager mapManager;
 23     private MKSearch mMKSearch;
 24     private EditText longitudeEditText;
 25     private EditText latitudeEditText;
 26     private TextView addressTextView;
 27     private Button queryButton;
 28 
 29     @Override
 30     public void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.main);
 33 
 34         mapManager = new BMapManager(getApplication());
 35         mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);
 36         super.initMapActivity(mapManager);
 37 
 38         mMKSearch = new MKSearch();
 39         mMKSearch.init(mapManager, new MySearchListener());
 40 
 41         longitudeEditText = (EditText) findViewById(R.id.longitude_input);
 42         longitudeEditText.setText("119.965683");
 43         latitudeEditText = (EditText) findViewById(R.id.latitude_input);
 44         latitudeEditText.setText("31.682259");
 45         addressTextView = (TextView) findViewById(R.id.address_text);
 46         queryButton = (Button) findViewById(R.id.query_button);
 47 
 48         queryButton.setOnClickListener(new OnClickListener() {
 49             @Override
 50             public void onClick(View v) {
 51                 String longitudeStr = longitudeEditText.getText().toString();
 52                 String latitudeStr = latitudeEditText.getText().toString();
 53 
 54                 try {
 55                     int longitude = (int) (1000000 * Double
 56                             .parseDouble(longitudeStr));
 57                     int latitude = (int) (1000000 * Double
 58                             .parseDouble(latitudeStr));
 59 
 60                     mMKSearch.reverseGeocode(new GeoPoint(latitude, longitude));
 61                 } catch (Exception e) {
 62                     addressTextView.setText("查询出错,请检查您输入的经纬度值!");
 63                 }
 64             }
 65         });
 66     }
 67 
 68     @Override
 69     protected boolean isRouteDisplayed() {
 70         return false;
 71     }
 72 
 73     @Override
 74     protected void onDestroy() {
 75         if (mapManager != null) {
 76             // 程序退出前需调用此方法
 77             mapManager.destroy();
 78             mapManager = null;
 79         }
 80         super.onDestroy();
 81     }
 82 
 83     @Override
 84     protected void onPause() {
 85         if (mapManager != null) {
 86             // 终止百度地图API
 87             mapManager.stop();
 88         }
 89         super.onPause();
 90     }
 91 
 92     @Override
 93     protected void onResume() {
 94         if (mapManager != null) {
 95             // 开启百度地图API
 96             mapManager.start();
 97         }
 98         super.onResume();
 99     }
100 
101     public class MySearchListener implements MKSearchListener {
102         /**
103          * 根据经纬度搜索地址信息结果
104          * 
105          * @param result
106          *            搜索结果
107          * @param iError
108          *            错误号(0表示正确返回)
109          */
110         @Override
111         public void onGetAddrResult(MKAddrInfo result, int iError) {
112             if (result == null) {
113                 return;
114             }
115             StringBuffer sb = new StringBuffer();
116             // 经纬度所对应的位置
117             sb.append(result.strAddr).append("\n");
118 
119             // 判断该地址附近是否有POI(Point of Interest,即兴趣点)
120             if (null != result.poiList) {
121                 // 遍历所有的兴趣点信息
122                 for (MKPoiInfo poiInfo : result.poiList) {
123                     sb.append("").append("\n");
124                     sb.append("名称:").append(poiInfo.name).append("\n");
125                     sb.append("地址:").append(poiInfo.address).append("\n");
126                     sb.append("经度:").append(poiInfo.pt.getLongitudeE6() / 1000000.0f).append("\n");
127                     sb.append("纬度:").append(poiInfo.pt.getLatitudeE6() / 1000000.0f).append("\n");
128                     sb.append("电话:").append(poiInfo.phoneNum).append("\n");
129                     sb.append("邮编:").append(poiInfo.postCode).append("\n");
130                     // poi类型,0:普通点,1:公交站,2:公交线路,3:地铁站,4:地铁线路
131                     sb.append("类型:").append(poiInfo.ePoiType).append("\n");
132                 }
133             }
134             // 将地址信息、兴趣点信息显示在TextView上
135             addressTextView.setText(sb.toString());
136         }
137 
138         /**
139          * 驾车路线搜索结果
140          * 
141          * @param result
142          *            搜索结果
143          * @param iError
144          *            错误号(0表示正确返回)
145          */
146         @Override
147         public void onGetDrivingRouteResult(MKDrivingRouteResult result,
148                 int iError) {
149         }
150 
151         /**
152          * POI搜索结果(范围检索、城市POI检索、周边检索)
153          * 
154          * @param result
155          *            搜索结果
156          * @param type
157          *            返回结果类型(11,12,21:poi列表 7:城市列表)
158          * @param iError
159          *            错误号(0表示正确返回)
160          */
161         @Override
162         public void onGetPoiResult(MKPoiResult result, int type, int iError) {
163         }
164 
165         /**
166          * 公交换乘路线搜索结果
167          * 
168          * @param result
169          *            搜索结果
170          * @param iError
171          *            错误号(0表示正确返回)
172          */
173         @Override
174         public void onGetTransitRouteResult(MKTransitRouteResult result,
175                 int iError) {
176         }
177 
178         /**
179          * 步行路线搜索结果
180          * 
181          * @param result
182          *            搜索结果
183          * @param iError
184          *            错误号(0表示正确返回)
185          */
186         @Override
187         public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {
188         }
189 
190         @Override
191         public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
192         }
193 
194     }
195 }

源码下载地址:http://filemarkets.com/fs/0todngf19d9962217/

posted on 2013-09-19 22:41  振兴  阅读(1073)  评论(0编辑  收藏  举报