1、AndroidManifest.xml
在AndroidManifest.xml中加上以下代碼:
[html]
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
2、Main.xml
在裡面加上以下代碼:
[html]
<com.baidu.mapapi.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
3、在lib目錄下導入相應的jar包(這個可以在官方文檔中的標稱裡面找)
4、MainAcitivity
[java]
package com.njupt.bmap_helloworld;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends MapActivity {
private MapView mapView;//百度地圖的相關控件
private BMapManager bMapManager;//加載地圖的引擎
private String keyString = "04763B1D8C925195965E838C6E59DB767DAE4BA7";//百度地圖的key
private MapController mapController;//在百度地圖上添加一些控件,比如放大或縮小控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.bmapView);
bMapManager = new BMapManager(MainActivity.this);
//必須要加載key
bMapManager.init(keyString, new MKGeneralListener() {
@Override
public void onGetPermissionState(int arg0) {
if(arg0 == 300){
Toast.makeText(MainActivity.this, "輸入的key有誤,請核實...", 1).show();
}
}
@Override
public void onGetNetworkState(int arg0) {
// TODO Auto-generated method stub
}
});
initMapActivity(bMapManager);
mapView.setBuiltInZoomControls(true);//表示可以設置縮放功能.需要注意的是,這一句一定要放在initMapActivity之後才會起作用
mapController = mapView.getController();
GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6), (int)(116.404*1E6));//定義經緯度
mapController.setCenter(geoPoint);//設置一個中心點
mapController.setZoom(12);//設置縮放級別
}
@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() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}