添加地图显示
一、在百度官网下载相关的SDK
(网址:http://developer.baidu.com/map/sdkandev-download.htm)
解压下载好的BaiduMap_AndroidSDK_v2.1.3_Sample文件
把BaiduMap_AndroidSDK_v2.1.3_Sample中的 libs 中的文件全部复制到 Project 中的 libs 目录中。
二、UI界面
打开res/layout /main.xml
<?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="百度地图Demo" /> <com.baidu.mapapi.map.MapView android:id="@+id/bmapsView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</LinearLayout>
三、创建地图引擎、地图显示器
File: src/com.jiangge.mymap/MainActivity.java
package com.jiangge.mymap; import android.app.Activity;
import android.os.Bundle; import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint; public class MainActivity extends Activity { private BMapManager mMapMan = null;
private MapView mMapView = null;
private MapController mMapContrl = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMapMan = new BMapManager(this);
mMapMan.init("A7f35d1cfa104f23574932c9d1b1b934", new MKGeneralListener() {
@Override
public void onGetPermissionState(int arg0) {}
@Override
public void onGetNetworkState(int arg0) {}
}); setContentView(R.layout.activity_main); mMapView = (MapView)findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true); //(允许缩放功能)
mMapContrl = mMapView.getController(); //初始化控制器(关联mMapView)
mMapContrl.enableClick(true); //(允许触发点击事件)
mMapContrl.setZoom(12); //(允许缩放级别)
GeoPoint poit = new GeoPoint((int)(39.915* 1E6),(int)(116.404* 1E6));//(经度,纬度) } }
四、添加相关权限
File: AndroidManifest.xml
<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.WRITE_SETTINGS"></uses-permission>
五、运行