歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 百度地圖-實現POI的搜索(搜索周邊)

百度地圖-實現POI的搜索(搜索周邊)

日期:2017/3/1 10:22:20   编辑:Linux編程

百度地圖-實現POI的搜索(搜索周邊) :

  1. package com.lbsproject;
  2. import java.util.ArrayList;
  3. import Android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Point;
  8. import android.graphics.drawable.Drawable;
  9. import android.location.Criteria;
  10. import android.location.Location;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.Menu;
  15. import android.view.MenuItem;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18. import android.widget.Button;
  19. import android.widget.EditText;
  20. import android.widget.Toast;
  21. import com.baidu.mapapi.BMapManager;
  22. import com.baidu.mapapi.GeoPoint;
  23. import com.baidu.mapapi.LocationListener;
  24. import com.baidu.mapapi.MKAddrInfo;
  25. import com.baidu.mapapi.MKDrivingRouteResult;
  26. import com.baidu.mapapi.MKPoiResult;
  27. import com.baidu.mapapi.MKSearch;
  28. import com.baidu.mapapi.MKSearchListener;
  29. import com.baidu.mapapi.MKTransitRouteResult;
  30. import com.baidu.mapapi.MKWalkingRouteResult;
  31. import com.baidu.mapapi.MapActivity;
  32. import com.baidu.mapapi.MapController;
  33. import com.baidu.mapapi.MapView;
  34. import com.baidu.mapapi.MyLocationOverlay;
  35. import com.baidu.mapapi.Overlay;
  36. import com.baidu.mapapi.PoiOverlay;
  37. import com.baidu.mapapi.Projection;
  38. import com.lbsproject.LBSProjectActivity.GetOverlay;
  39. public class LBSProjectActivity extends MapActivity {
  40. protected static final String TAG = null;
  41. private BMapManager mapManager;
  42. private MapView mapView;
  43. private MapController mapController;
  44. private MKSearch mSearch = null;
  45. private GeoPoint geoPoint = new GeoPoint(122084095, 37422006);;
  46. LocationListener mLocationListener = null;
  47. MyLocationOverlay mLocationOverlay = null;
  48. private EditText editCity;
  49. private EditText editGeoCodeKey;
  50. private EditText areaText;
  51. private GetOverlay getOverlay;
  52. @Override
  53. public void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setContentView(R.layout.main);
  56. getInitLoc();// 程序打開時候獲取當前位置 顯示地圖上
  57. mapManager = new BMapManager(getApplication());
  58. // init方法的第一個參數需填入申請的APIKey
  59. mapManager.init("2150650BE0DCF874994B845CEC7B60A0518E6AFE", null);
  60. super.initMapActivity(mapManager);
  61. mapView = (MapView) findViewById(R.id.mapView);
  62. mapView.setBuiltInZoomControls(true);
  63. mapView.setDrawOverlayWhenZooming(true);
  64. // 添加定位圖層
  65. mLocationOverlay = new MyLocationOverlay(this, mapView);
  66. mapView.getOverlays().add(mLocationOverlay);
  67. getOverlay = new GetOverlay();
  68. mapView.getOverlays().add(getOverlay);
  69. gotoLocate();
  70. Button buttonGetPos = (Button) findViewById(R.id.buttonGetPos);//獲取"我的位置的方法"
  71. buttonGetPos.setOnClickListener(new View.OnClickListener() {
  72. public void onClick(View v) {
  73. getInitLoc();
  74. gotoLocate();
  75. }
  76. });
  77. editCity = (EditText) findViewById(R.id.editTextCity);
  78. editGeoCodeKey = (EditText) findViewById(R.id.editTextAera);
  79. Button buttonSearchCity = (Button) findViewById(R.id.buttonSearchCity);
  80. buttonSearchCity.setOnClickListener(new View.OnClickListener() {//點擊按鈕定位需要去的地方
  81. @Override
  82. public void onClick(View v) {
  83. mSearch.geocode(editGeoCodeKey.getText().toString(), editCity
  84. .getText().toString());
  85. // mapView.getOverlays().add(getOverlay);
  86. }
  87. });
  88. areaText = (EditText) findViewById(R.id.searchKeywords);
  89. Button buttonSearchArea = (Button) findViewById(R.id.buttonSearchArea);
  90. buttonSearchArea.setOnClickListener(new View.OnClickListener() {
  91. @Override
  92. public void onClick(View v) {
  93. mSearch.poiSearchNearBy(areaText.getText().toString(),// 搜索XX附近5000米范圍的XXX
  94. geoPoint, 5000);
  95. }
  96. });
  97. // 注冊定位事件
  98. mLocationListener = new LocationListener() {
  99. public void onLocationChanged(Location location) {
  100. if (location != null) {
  101. Log.i(TAG, "" + location.getLatitude());
  102. Log.i(TAG, "" + location.getLongitude());
  103. geoPoint = new GeoPoint(
  104. (int) (location.getLatitude() * 1e6),
  105. (int) (location.getLongitude() * 1e6));
  106. gotoLocate();
  107. }
  108. }
  109. };
  110. mSearch = new MKSearch();//搜索服務類
  111. mSearch.init(mapManager, new MKSearchListener() {
  112. public void onGetAddrResult(MKAddrInfo res, int error) {
  113. if (error != 0) {
  114. String str = String.format("錯誤號:%d", error);
  115. Toast.makeText(LBSProjectActivity.this, str,
  116. Toast.LENGTH_LONG).show();
  117. return;
  118. }
  119. mapView.getController().animateTo(res.geoPt);
  120. // String strInfo = String.format("緯度:%f 經度:%f\r\n",
  121. // res.geoPt.getLatitudeE6() / 1e6,
  122. // res.geoPt.getLongitudeE6() / 1e6);
  123. geoPoint = res.geoPt;
  124. // Toast.makeText(LBSProjectActivity.this, strInfo,
  125. // Toast.LENGTH_LONG).show();
  126. Drawable marker = getResources().getDrawable(
  127. R.drawable.iconmarka); // 得到需要標在地圖上的資源
  128. marker.setBounds(0, 0, marker.getIntrinsicWidth(),
  129. marker.getIntrinsicHeight()); // 為maker定義位置和邊界
  130. mapView.getOverlays().clear();
  131. mapView.getOverlays().add(getOverlay);
  132. mapView.getOverlays().add(
  133. new OverItemT(marker, LBSProjectActivity.this,
  134. res.geoPt, res.strAddr));
  135. }
  136. public void onGetPoiResult(MKPoiResult res, int type, int error) {
  137. if (res == null) {
  138. Log.d("onGetPoiResult", "the onGetPoiResult res is " + type
  139. + "__" + error);
  140. } else
  141. Log.d("onGetPoiResult",
  142. "the onGetPoiResult res is "
  143. + res.getCurrentNumPois() + "__"
  144. + res.getNumPages() + "__"
  145. + res.getNumPois() + "__" + type + "__"
  146. + error);
  147. // 錯誤號可參考MKEvent中的定義
  148. if (error != 0 || res == null) {
  149. Log.d("onGetPoiResult", "the onGetPoiResult res 0 ");
  150. Toast.makeText(LBSProjectActivity.this, "抱歉,未找到結果",
  151. Toast.LENGTH_LONG).show();
  152. return;
  153. }
  154. ArrayList<MKPoiResult> poiResult = res.getMultiPoiResult();
  155. if (poiResult != null)
  156. Log.d("onGetPoiResult", "the onGetPoiResult res 1__"
  157. + poiResult.size());
  158. // 將地圖移動到第一個POI中心點
  159. if (res.getCurrentNumPois() > 0) {
  160. Log.d("onGetPoiResult", "the onGetPoiResult res 2");
  161. // 將poi結果顯示到地圖上
  162. PoiOverlay poiOverlay = new PoiOverlay(
  163. LBSProjectActivity.this, mapView);
  164. poiOverlay.setData(res.getAllPoi());
  165. mapView.getOverlays().clear();
  166. mapView.getOverlays().add(getOverlay);
  167. mapView.getOverlays().add(poiOverlay);
  168. mapView.invalidate();
  169. mapView.getController().animateTo(res.getPoi(0).pt);
  170. } else if (res.getCityListNum() > 0) {
  171. Log.d("onGetPoiResult", "the onGetPoiResult res 3");
  172. String strInfo = "在";
  173. for (int i = 0; i < res.getCityListNum(); i++) {
  174. strInfo += res.getCityListInfo(i).city;
  175. strInfo += ",";
  176. }
  177. strInfo += "找到結果";
  178. Toast.makeText(LBSProjectActivity.this, strInfo,
  179. Toast.LENGTH_LONG).show();
  180. }
  181. Log.d("onGetPoiResult", "the onGetPoiResult res 4");
  182. }
  183. public void onGetDrivingRouteResult(MKDrivingRouteResult res,
  184. int error) {
  185. }
  186. public void onGetTransitRouteResult(MKTransitRouteResult res,
  187. int error) {
  188. }
  189. public void onGetWalkingRouteResult(MKWalkingRouteResult res,
  190. int error) {
  191. }
  192. });
  193. }
  194. private void gotoLocate() {// 獲取所在位置
  195. Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要標在地圖上的資源
  196. marker.setBounds(0, 0, marker.getIntrinsicWidth(),
  197. marker.getIntrinsicHeight()); // 為maker定義位置和邊界
  198. mapView.getOverlays().clear();
  199. mapView.getOverlays().add(getOverlay);
  200. mapView.getOverlays().add(
  201. new OverItemT(marker, LBSProjectActivity.this, geoPoint, ""));
  202. mapView.getController().animateTo(geoPoint);
  203. mapController = mapView.getController();
  204. // 設置地圖的中心
  205. mapController.setCenter(geoPoint);
  206. // 設置地圖默認的縮放級別
  207. mapController.setZoom(16);
  208. }
  209. private void getInitLoc() {// 初始化時候獲取坐標
  210. try {
  211. LocationManager locationManager;
  212. String context = Context.LOCATION_SERVICE;
  213. locationManager = (LocationManager) getSystemService(context);
  214. // String provider = LocationManager.GPS_PROVIDER;
  215. Criteria criteria = new Criteria();
  216. criteria.setAccuracy(Criteria.ACCURACY_FINE);
  217. criteria.setAltitudeRequired(false);
  218. criteria.setBearingRequired(false);
  219. criteria.setCostAllowed(true);
  220. criteria.setPowerRequirement(Criteria.POWER_LOW);
  221. String provider = locationManager.getBestProvider(criteria, true);
  222. Location location = locationManager.getLastKnownLocation(provider);
  223. geoPoint = new GeoPoint((int) (location.getLatitude() * 1e6),
  224. (int) (location.getLongitude() * 1e6));
  225. } catch (Exception e) {
  226. // TODO: handle exception
  227. }
  228. }
  229. @Override
  230. protected boolean isRouteDisplayed() {
  231. return false;
  232. }
  233. @Override
  234. protected void onDestroy() {
  235. if (mapManager != null) {
  236. // 程序退出前需調用此方法
  237. mapManager.destroy();
  238. mapManager = null;
  239. }
  240. super.onDestroy();
  241. }
  242. @Override
  243. protected void onPause() {
  244. if (mapManager != null) {
  245. // 終止百度地圖API
  246. mapManager.getLocationManager().removeUpdates(mLocationListener);
  247. mLocationOverlay.disableMyLocation();
  248. mLocationOverlay.disableCompass(); // 關閉指南針
  249. mapManager.stop();
  250. }
  251. super.onPause();
  252. }
  253. @Override
  254. protected void onResume() {
  255. if (mapManager != null) {
  256. // 開啟百度地圖API
  257. // 注冊定位事件,定位後將地圖移動到定位點
  258. mapManager.getLocationManager().requestLocationUpdates(
  259. mLocationListener);
  260. mLocationOverlay.enableMyLocation();
  261. mLocationOverlay.enableCompass(); // 打開指南針
  262. mapManager.start();
  263. }
  264. super.onResume();
  265. }
  266. /**
  267. * * 實現MKSearchListener接口,用於實現異步搜索服務 * @author liufeng
  268. */
  269. public class MySearchListener implements MKSearchListener {
  270. public void onGetAddrResult(MKAddrInfo result, int iError) {
  271. }
  272. public void onGetDrivingRouteResult(MKDrivingRouteResult result,
  273. int iError) {
  274. }
  275. /**
  276. * * POI搜索結果(范圍檢索、城市POI檢索、周邊檢索) * * @param result 搜索結果 * @param type
  277. * 返回結果類型(11,12,21:poi列表 7:城市列表) * @param iError 錯誤號(0表示正確返回)
  278. */
  279. @Override
  280. public void onGetPoiResult(MKPoiResult result, int type, int iError) {
  281. if (result == null) {
  282. return;
  283. }
  284. // PoiOverlay是baidu map api提供的用於顯示POI的Overlay
  285. PoiOverlay poioverlay = new PoiOverlay(LBSProjectActivity.this,
  286. mapView);
  287. // 設置搜索到的POI數據
  288. poioverlay.setData(result.getAllPoi());
  289. // 在地圖上顯示PoiOverlay(將搜索到的興趣點標注在地圖上)
  290. mapView.getOverlays().add(poioverlay);
  291. }
  292. public void onGetTransitRouteResult(MKTransitRouteResult result,
  293. int iError) {
  294. }
  295. public void onGetWalkingRouteResult(MKWalkingRouteResult result,
  296. int iError) {
  297. }
  298. }
  299. class GetOverlay extends Overlay {
  300. GeoPoint geo;
  301. @Override
  302. public void draw(Canvas canvas, MapView gmapView, boolean arg2) {
  303. super.draw(canvas, mapView, arg2);
  304. if (geo == null) {
  305. return;
  306. }
  307. Log.i("11111111111111111111", arg2 + "-------draw--");
  308. }
  309. @Override
  310. public boolean onTap(GeoPoint geo, MapView arg1) {
  311. geoPoint = geo;
  312. Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要標在地圖上的資源
  313. marker.setBounds(0, 0, marker.getIntrinsicWidth(),
  314. marker.getIntrinsicHeight()); // 為maker定義位置和邊界
  315. mapView.getOverlays().clear();
  316. mapView.getOverlays().add(getOverlay);
  317. mapView.getOverlays()
  318. .add(new OverItemT(marker, LBSProjectActivity.this,
  319. geoPoint, ""));
  320. Log.i("11111111111111111111", geo.getLongitudeE6() / 1E6
  321. + "----------" + geo.getLatitudeE6() / 1E6);
  322. return super.onTap(geo, arg1);
  323. }
  324. }
  325. private static final int TOOLBAR0 = 0;
  326. private static final int TOOLBAR1 = 1;
  327. private static final int TOOLBAR2 = 2;
  328. private static final int TOOLBAR3 = 3;
  329. public boolean onCreateOptionsMenu(Menu menu) {
  330. menu.add(0, TOOLBAR0, 1, "KTV").setIcon(
  331. android.R.drawable.ic_btn_speak_now);
  332. menu.add(0, TOOLBAR1, 2, "學校").setIcon(
  333. android.R.drawable.ic_menu_myplaces);
  334. menu.add(0, TOOLBAR2, 3, "餐廳").setIcon(
  335. android.R.drawable.ic_menu_my_calendar);
  336. menu.add(0, TOOLBAR3, 4, "公園").setIcon(
  337. android.R.drawable.ic_menu_gallery);
  338. return super.onCreateOptionsMenu(menu);
  339. }
  340. @Override
  341. public boolean onOptionsItemSelected(MenuItem item) {
  342. switch (item.getItemId()) {
  343. case 0:
  344. mSearch.poiSearchNearBy("KTV", geoPoint, 5000);//搜索ktv
  345. break;
  346. case 1:
  347. mSearch.poiSearchNearBy("學校", geoPoint, 5000);//.搜索學校
  348. break;
  349. case 2:
  350. mSearch.poiSearchNearBy("餐廳", geoPoint, 5000);//搜索餐廳
  351. break;
  352. case 3:
  353. mSearch.poiSearchNearBy("公園", geoPoint, 5000);//搜索公園
  354. break;
  355. }
  356. return super.onOptionsItemSelected(item);
  357. }
  358. }

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved