Android學習筆記之百度地圖(根據地名查詢經緯度)

來源:互聯網
上載者:User

重要方法:

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 = null;private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";private MapView mapView = null;private MKSearch mKSearch;private MapController mapController = null;@Overridepublic 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表示正確返回 */}}@Overrideprotected boolean isRouteDisplayed(){return false;}@Overrideprotected void onDestroy(){if (mapManager != null){mapManager.destroy();mapManager = null;}super.onDestroy();}@Overrideprotected void onPause(){if (mapManager != null){mapManager.stop();}super.onPause();}@Overrideprotected void onResume(){if (mapManager != null){mapManager.start();}super.onResume();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.