android平台的百度地图的开发(一)

最近在学习android的地图开发,写下一些东西作为自己学习的记录:

1、先到http://developer.baidu.com/map/sdk-android.htm 下载相关的文件

2、下载完毕后,我们在申请一个密钥,网址:http://developer.baidu.com/map/apply-key.htm


3、在AndroidManifest.xml文件中application的结尾加上:


    <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" />


在你的layout文件中,加入地图显示控件



    <com.baidu.mapapi.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />



将库文件引入工程:baidumapapi.jar 和 libs/armeabi/libBMapApiEngine_v1_3_3.so(这一步很重要)

再编写对应的activity类:注意点--继承的类不是Activity而是MapActivity


代码如下:



public class MainActivity extends MapActivity
{

	private MapView mapView;
	private BMapManager bMapManager;
	private MapController controller;
	private String key = "你申请的key";

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mapView = (MapView) this.findViewById(R.id.bmapView);
		bMapManager = new BMapManager(this);

		// init bmapmanager
		bMapManager.init(key, new MKGeneralListener()
		{

			@Override
			public void onGetPermissionState(int arg0)
			{
				if (arg0 == 300)
				{
					Toast.makeText(MainActivity.this, "key error !", Toast.LENGTH_LONG).show();
				}
			}

			@Override
			public void onGetNetworkState(int arg0)
			{
				// TODO Auto-generated method stub

			}
		});

		//一定要在使用mapcontroller之前初始化
		this.initMapActivity(bMapManager);
		
		// config map
		mapView.setBuiltInZoomControls(true);
		controller = mapView.getController();
		GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));

		Log.i("msg", mapView == null ? "mapview is null" : " mapview is not null ");
		Log.i("msg", controller == null ? "controller is null" : "controller is not null ");
		controller.setCenter(point);
		controller.setZoom(12);
		
		//显示交通状况
		mapView.setTraffic(true);
		//显示卫星地图
		mapView.setSatellite(true);
		
		//地图覆盖物
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		return true;
	}

	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}

	@Override
	protected void onResume()
	{
		if (bMapManager == null)
		{
			bMapManager.start();
		}
		super.onResume();
	}

	@Override
	protected void onDestroy()
	{
		if (bMapManager == null)
		{
			bMapManager.destroy();
		}
		super.onDestroy();
	}

	@Override
	protected void onPause()
	{
		if (bMapManager == null)
		{
			bMapManager.stop();
		}
		super.onPause();
	}
}


 

 

你可能感兴趣的:(android平台的百度地图的开发(一))