首先在工程里新建libs文件夹,并API开发包里的baidumapapi.jar拷贝到libs根目录下,将libBMapApiEngine.so 拷贝到libs\armeabi目录下。官网demo里已有这两个文件,如果要集成到自己的工程里,就需要自己添加。然后在工程属性->Java Build Path->Libraries中选择“Add External JARs”,选定baidumapapi.jar,确定后返回,这样您就可以在您的程序中使用API了。
百度地图的“Hello,World”
<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:largeScreens ="true" android:normalScreens ="true" android:smallScreens ="true" android:resizeable ="true" android:anyDensity ="true" /> <uses-sdk android:minSdkVersion ="3" > </uses-sdk>
import java.util.ArrayList ; import java.util.List ; import android.content.Context ; import android.graphics.Canvas ; import android.graphics.Paint ; import android.graphics.Point ; import android.graphics.drawable.Drawable ; import android.location.Location ; import android.os.Bundle ; import android.util.Log ; import android.view.View ; import android.widget.Toast ; import com.baidu.mapapi.BMapManager ; import com.baidu.mapapi.GeoPoint ; import com.baidu.mapapi.ItemizedOverlay ; import com.baidu.mapapi.LocationListener ; import com.baidu.mapapi.MKAddrInfo ; import com.baidu.mapapi.MKDrivingRouteResult ; import com.baidu.mapapi.MKGeneralListener ; import com.baidu.mapapi.MKLocationManager ; import com.baidu.mapapi.MKPlanNode ; 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.MKSuggestionResult ; import com.baidu.mapapi.MapActivity ; import com.baidu.mapapi.MapController ; import com.baidu.mapapi.MapView ; import com.baidu.mapapi.MyLocationOverlay ; import com.baidu.mapapi.Overlay ; import com.baidu.mapapi.OverlayItem ; import com.baidu.mapapi.PoiOverlay ; import com.baidu.mapapi.RouteOverlay ; import com.baidu.mapapi.TransitOverlay ; public class MyMapActivity extends MapActivity { @Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R. layout . main ) ; } @Override protected boolean isRouteDisplayed ( ) { return false ; } }
<?xml version ="1.0" encoding ="utf-8" ?> <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:orientation ="vertical" android:layout_width ="fill_parent" android:layout_height ="fill_parent" > <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>
在地图Activity中定义变量: BMapManager mBMapMan = null; 在onCreate方法中增加以下代码,并将您申请的Key替换“我的Key”:
BMapManager mBMapMan = new BMapManager ( getApplication ( ) ) ; mBMapMan. init ( "我的Key" , null ) ; super . initMapActivity ( mBMapMan ) ; MapView mMapView = ( MapView ) findViewById ( R. id . bmapsView ) ; mMapView. setBuiltInZoomControls ( true ) ; //设置启用内置的缩放控件 MapController mMapController = mMapView. getController ( ) ; // 得到mMapView的控制权,可以用它控制和驱动平移和缩放 GeoPoint point = new GeoPoint ( ( int ) ( 39.915 * 1E6 ) , ( int ) ( 116.404 * 1E6 ) ) ; //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6) mMapController. setCenter ( point ) ; //设置地图中心点 mMapController. setZoom ( 12 ) ; //设置地图zoom级别
Override以下方法,管理API:
@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 ( ) ;百度地图申请key地址:http://dev.baidu.com/wiki/static/imap/key/