Android学习笔记之初步学百度地图
<1>新建一个工程,将API添加到Andoid工程中
必须显得下载API开发包:点击下载
解压之后包括两个文件baidumapapi.jar和libBMapApiEngine.so。在工程根目录下创建ibs\armeabi目录,并拷贝到其目录下。
在工程属性->Java Build Path->Libraries中选择“Add JARs”,选定baidumapapi.jar,确定后如下:
返回,这样您就可以在您的程序中使用API了。
<2>在Manifest中添加使用权限
- <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>
<3>在Manifest中添加Android版本支持
- <supports-screens android:largeScreens="true"
- android:normalScreens="true" android:smallScreens="true"
- android:resizeable="true" android:anyDensity="true"/>
- <uses-sdk android:minSdkVersion="3"></uses-sdk>
<4>让创建的地图Activity继承com.baidu.mapapi.MapActivity, 并import相关类
package com.myandroid.lmap;
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.app.Activity;
import android.os.Bundle;
public class LMapDemoActivity extends MapActivity{
BMapManager mapManager = null;
private static String KEY = "BF9C666C307D5D2555A964A0DF1B091F6C570C7F";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapManager = new BMapManager(getApplication());
mapManager.init(KEY, null);
super.initMapActivity(mapManager);
MapView mapView = (MapView) findViewById(R.id.bmapsView);
mapView.setBuiltInZoomControls(true); //设置启用内置的缩放控件
MapController mapController = mapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
(int) (116.404 * 1E6)); //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
mapController.setCenter(point); //设置地图中心点
mapController.setZoom(12); //设置地图zoom级别
}
//必须Override这个方法
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
//这个也
@Override
protected void onDestroy() {
if (mapManager != null) {
mapManager.destroy();
mapManager = null;
}
super.onDestroy();
}
@Override
protected void onPause() {
if (mapManager != null) {
mapManager.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if (mapManager != null) {
mapManager.start();
}
super.onResume();
}
}
-----------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myandroid.lmap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<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>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".LMapDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
------------------------------------------------------------------------------------------------------
贴个图: