今天就写写android是如何实现百度地图api的调用的.写之前也去网上搜了下,都写得挺详细的,但是对开发没什么大用处,讲的基本都是
从百度地图api中移植过来的,实际开发过程中肯定不会是简简单单调用api这么简单,今天我就讲下百度地图上如何实现定位画图,放自己的东西在百度地图上面.
我写的任何东西只会提供核心代码(出于时间问题),所以你们可以先去看看别人的,然后再到我这来提高,后面我也会慢慢细化这块,比如距离计算,
驾车线路,经纬度定位等等其他功能呢.现在百度做了大的改动,将地图和定位分开了,所以我现在就讲最新的版本了.
个人觉得定位的速度虽然提高了,但是之前有的功能阉割了,比如ItemizedOverlay的draw方法被干掉了,这让开发很是头疼,只能等百度进一步完善了.
首先需要下载百度提供的Android SDKv2.0.0(矢量地图)的jar包,地址如下:http://developer.baidu.com/map/sdkandev-download.htm
jar包,含有armeabi文件夹和baidumapapi_v2_0_0.jar,将它们放入项目的lib文件夹下.记住如果要使用定位就必须将liblocSDK3.so放入armeabi中,locSDK_3.1.jar放入lib中,
获取经纬度地址 http://developer.baidu.com/map/jsdemo.htm
这也是新版本与老版本最大的区别.废物不多说,上代码了
首先在AndroidManifest.xml中加入如下代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.driverclient.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="10"/>
<application
android:name=".AppContext"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ui.trick.ShowCarActivity" />
<service android:name="com.baidu.location.f" android:enabled="true"
android:process=":remote">
</service>
</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>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission>
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCES_MOCK_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_GPS" />
<supports-screens android:largeScreens="true"
android:normalScreens="false" android:smallScreens="true"
android:resizeable="true" android:anyDensity="true"/>
</manifest>
特别是<service android:name="com.baidu.location.f" android:enabled="true"
android:process=":remote">
</service>这一段,否则无法提供定位服务
然后我是使用三个类来完成如下操作
AppContext 全局应用程序类:用于保存和调用全局应用配置及访问网络数据
ShowCarActivity 显示当前位置车辆分布
OverItemT 覆盖物
/**
* 全局应用程序类:用于保存和调用全局应用配置及访问网络数据
*
* @author Bert Guo 2013-2-27
*/
public class AppContext extends Application {
private List<Activity> activityList = new LinkedList<Activity>(); // Activity集合
private static AppContext mInstance = null;
public boolean m_bKeyRight = true; // 授权Key正确,验证通过
public BMapManager mBMapManager = null; // 百度地图引擎
// 申请地址:http://dev.baidu.com/wiki/static/imap/key/
public static final String strKey = "25133AF7B7FDF77723954D4C98A3C3B3F3AF973F"; // 授权Key
@Override
public void onCreate() {
mInstance = this;
initEngineManager(this);
}
@Override
// 建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗
public void onTerminate() {
// TODO Auto-generated method stub
if (mBMapManager != null) {
mBMapManager.destroy(); // 程序退出前调用
mBMapManager = null;
}
super.onTerminate();
}
public void initEngineManager(Context context) {
if (mBMapManager == null) {
mBMapManager = new BMapManager(context); // 初始化地图引擎
}
if (!mBMapManager.init(strKey, new MyGeneralListener())) { // 初始化失败
Toast.makeText(AppContext.getInstance().getApplicationContext(),
"BMapManager 初始化错误!", Toast.LENGTH_LONG).show();
}
}
/**
* 获取应用程序实例 单例模式中获取唯一的Application 实例
*
* @return 当前应用程序
*/
public static AppContext getInstance() {
if (null == mInstance) {
mInstance = new AppContext();
}
return mInstance;
}
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
static class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
Toast.makeText(
AppContext.getInstance().getApplicationContext(),
"您的网络出错啦!", Toast.LENGTH_LONG).show();
} else if (iError == MKEvent.ERROR_NETWORK_DATA) {
Toast.makeText(
AppContext.getInstance().getApplicationContext(),
"输入正确的检索条件!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onGetPermissionState(int iError) {
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
// 授权Key错误:
Toast.makeText(
AppContext.getInstance().getApplicationContext(),
"请在 AppContext.java文件输入正确的授权Key!", Toast.LENGTH_LONG)
.show();
AppContext.getInstance().m_bKeyRight = false;
}
}
}
}
/**
* 显示车辆分布
*
* @author Bert Guo 2013-2-27
*/
public class ShowCarActivity extends Activity {
static MapView mMapView = null; // 显示地图的View
private int k = 0;
private MapController mMapController = null; // 可用于控制和驱动平移和缩放
public MKMapViewListener mMapListener = null; // 监听地图显示事件
LocationClient mLocClient; // 定位客户
public MyLocationListenner myListener = new MyLocationListenner(); // 自定义定位监听器
Button testUpdateButton = null; // 测试Button
EditText indexText = null;
MyLocationOverlay myLocationOverlay = null; // 负责显示用户当前位置的Overlay
int index = 0; // 计数器,移动地图后不让地图自动跑到中心点
LocationData locData = null; // 我的位置信息, 具体信息可由百度定位SDK获取
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trick_showcar);
AppContext app = (AppContext) this.getApplication();
if (app.mBMapManager == null) {
app.mBMapManager = new BMapManager(this);
app.mBMapManager.init(AppContext.strKey,new AppContext.MyGeneralListener());
}
initMapView();
testUpdateButton = (Button) findViewById(R.id.button1);
testUpdateButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Drawable marker = getResources().getDrawable(
R.drawable.trick_car); // 得到需要标在地图上的资源
mMapView.getOverlays().add(
new OverItemT(marker, ShowCarActivity.this)); // 添加ItemizedOverlay实例到mMapView
mMapView.refresh();// 刷新地图
}
});
}
/**
* 初始化地图
*/
private void initMapView() {
mMapView = (MapView) findViewById(R.id.bmapView);
myLocationOverlay = new MyLocationOverlay(mMapView);
mLocClient = new LocationClient(this);
mMapController = mMapView.getController();
mMapView.setLongClickable(true); // 启用常按事件
mMapView.setTraffic(true); // 显示实时路况
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
mMapView.getController().setZoom(14);
mMapView.getController().enableClick(true);
mMapView.displayZoomControls(true);
mMapListener = new MKMapViewListener() {
@Override
public void onMapMoveFinish() {
// TODO Auto-generated method stub
}
@Override
public void onClickMapPoi(MapPoi mapPoiInfo) {
// TODO Auto-generated method stub
String title = "";
if (mapPoiInfo != null) {
title = mapPoiInfo.strText;
Toast.makeText(ShowCarActivity.this, title,
Toast.LENGTH_SHORT).show();
}
}
};
mMapView.regMapViewListener(AppContext.getInstance().mBMapManager,
mMapListener);
locData = new LocationData();
mLocClient.registerLocationListener(myListener); // 注册定位监听器,监听当前位置的改变
mMapView.getOverlays().add(myLocationOverlay);
}
/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
// 获取当前位置
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null) { // 如果获取不到位置
return;
}
locData.latitude = location.getLatitude();
locData.longitude = location.getLongitude();
locData.accuracy = location.getRadius(); // 定位精度
locData.direction = location.getDerect(); // GPS定位时方向角度
myLocationOverlay.setData(locData); // 设置位置数据
if (k < 3) { // 定位3次
mMapController.animateTo(new GeoPoint(
(int) (locData.latitude * 1e6),
(int) (locData.longitude * 1e6))); // 将地图中心移向当前位置
}
mMapView.refresh(); // 刷新地图 进行overlay增删操作后,需手动调用此方法以刷新地图显示。
k++;
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null) {
return;
}
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
}
}
trick_showcar
<?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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:hint="请输入目的地"
/>
<Button
android:id="@+id/button1"
android:text="查询"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:clickable="true"
/>
</LinearLayout>
/**
* 覆盖物
*
* @author Bert Guo 2013-3-1
*/
public class OverItemT extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> GeoList = new ArrayList<OverlayItem>();
private Context mContext;
private double mLat1 = 30.642423;// 39.9022; // point1纬度
private double mLon1 = 114.212945;// 116.3822; // point1经度
private double mLat2 = 30.642423;
private double mLon2 = 114.213945;
private double mLat3 = 30.642423;
private double mLon3 = 114.214945;
public OverItemT(Drawable marker, Context context) {
super(marker);
this.mContext = context; // 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
GeoPoint p1 = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));
GeoPoint p2 = new GeoPoint((int) (mLat2 * 1E6), (int) (mLon2 * 1E6));
GeoPoint p3 = new GeoPoint((int) (mLat3 * 1E6), (int) (mLon3 * 1E6));
GeoList.add(new OverlayItem(p1, "P1", "point1"));
GeoList.add(new OverlayItem(p2, "P2", "point2"));
GeoList.add(new OverlayItem(p3, "P3", "point3"));
populate(); // createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法 }
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection(); // 获取屏幕像素坐标和经纬度对之间的转换。
for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList
OverlayItem overLayItem = getItem(index); // 得到给定索引的item
String title = overLayItem.getTitle();
// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
Point point = projection.toPixels(overLayItem.getPoint(), null);
// 可在此处添加您的绘制代码
Paint paintText = new Paint();
paintText.setColor(Color.RED);
paintText.setTextSize(50);
paintText.setFakeBoldText(true);
canvas.drawText(title, point.x + 20, point.y - 35, paintText); // 绘制文本
}
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return GeoList.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return GeoList.size();
}
}
先写到这了,稍后再做修改.这几天的博客就围绕地图来写吧.呵呵
如果有其他相关问题都可以QQ 468903507我,看过的顶顶哦