完成前文的準備工作, 現在我們可以動手編寫百度地图應用程序.
- 修改 main.xml, 加入 com.baidu.mapapi.MapView, 這是百度地图的視圖.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <com.baidu.mapapi.MapView android:id="@+id/bmapsView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
- 修改活動的主要 Java 代碼, 注意它是擴展 MapActivity, 不是 Activity. MY_KEY字串須使用你自己的密鑰, 參考前文"準備工作".
package com.BaiduMap; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.GeoPoint; import com.baidu.mapapi.MapActivity; import com.baidu.mapapi.MapController; import com.baidu.mapapi.MapView; import android.os.Bundle; public class BaiduMapActivity extends MapActivity { //使用你自己的密鑰 //參考準備工作 http://goo.gl/j6HkK final static String MY_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; BMapManager mBMapMan; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBMapMan = new BMapManager(getApplication()); mBMapMan.init(MY_KEY, null); super.initMapActivity(mBMapMan); MapView mMapView = (MapView) findViewById(R.id.bmapsView); mMapView.setBuiltInZoomControls(true); MapController mMapController = mMapView.getController(); GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)); mMapController.setCenter(point); mMapController.setZoom(12); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } @Override protected void onDestroy() { if (mBMapMan != null) { mBMapMan.destroy(); mBMapMan = null; } super.onDestroy(); } @Override protected void onPause() { if (mBMapMan != null) { mBMapMan.stop(); } super.onPause(); } @Override protected void onResume() { if (mBMapMan != null) { mBMapMan.start(); } super.onResume(); } }
相關文章:
- 在百度地图(Baidu Map)顯示我的位置(MyLocation)和指南針(Compass)
沒有留言:
發佈留言