1.申请密钥
cd .android
keytool -list -v -keystore debug.keystore
复制开发版SHA1值 42:28:A5:A3:30:54:7D:71:FA:31:4D:58:4C:8C:5A:45:0F:94:89:89
keytool -list -v -keystore 发布版SHA1路径(需要打包生成)
输入密钥库口令: 生成APK的第一个密码2E:F8:95:77:E2:4B:CB:A2:26:8B:28:A8:86:EF:DE:B1:FD:67:A6:5A
---->XA5dNxGrhB006qGiM4TCNKDv39I8G1Wi
2.下载SDK
解压到libs目录下
依赖jar文件
在build.gradle的android闭包中添加
sourceSets{
main{
jniLibs.srcDirs = ['libs']
}
}
3.搭建基础地图
1.在application节点中中添加开发密钥
<application>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="第一步申请到的 key" />
</application>
2.添加所需权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
3.第三步,在布局xml文件中添加地图控件;
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
4.在MyApp初始化SDK,并引用AppName
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
SDKInitializer.initialize(this);
}
}
5.创建地图Activity,管理地图生命周期;与Activity同步
public class Baidu extends AppCompatActivity {
MapView mapView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mv);
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
4. 设置地图
//地图控制类
BaiduMap baiduMap = mapView.getMap()
//空白地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE)
//普通地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL)
//卫星地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE)
//交通地图
mBaiduMap.setTrafficEnabled(true)
//热力图
mBaiduMap.setBaiduHeatMapEnabled(true)
//Marker坐标点
public static final LatLng xykj = new LatLng(22.536294,113.95137)
//Marker图标
private static final BitmapDesceriptor xykjBitmap = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_round)
//创建一组Marker图标
ArrayList<BitmapDescriptor> list = new ArrayList<>()
for (int i = 0
try {
int id = R.drawable.class.getField("icon_mark" + (char)('a'+i)).getInt(null)
list.add(BitmapDescriptorFactory.fromResource(id))
} catch (Exception e) {
e.printStackTrace()
}
}
//添加Marker标记
OverlayOptions option = new MarkerOptions().position(xykj)
.icon(xykjBitmap)
.icons(list)
.animateType(MarkerOptions.MarkerAnimateType.drop)
baiduMap.addOverlay(option)
//添加文本标记
OverlayOptions overlayOptions = new TextOptions().position(xykj).text("武汉大学")
.fontSize(18).bgColor(Color.BLUE)
.fontColor(Color.RED)
Text text = (Text) baiduMap.addOverlay(overlayOptions)
//添加一个圆到地图
OverlayOptions overlayOptions1 = new CircleOptions().center(latLng).fillColor(Color.argb(0x60, 0xff, 0x00, 0xff))
.radius(2000)
.stroke(new Stroke(Color.GRAY, 10))
baiduMap.addOverlay(overlayOptions1)
//添加一个折线
List<Integer> colorList = new ArrayList<>()
List<LatLng> lineList = new LinkedList<>()
lineList.add(new LatLng(22, 113))
lineList.add(new LatLng(22.536294, 113.95137))
lineList.add(new LatLng(23, 114))
colorList.add(Color.RED)
colorList.add(Color.GREEN)
PolylineOptions points = new PolylineOptions().colorsValues(colorList).points(lineList)
baiduMap.addOverlay(points)
//设置百度地图的当前位置
baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngZoom(xykj,18))
//mipmap和drawable不区分图片大小
// MapFragment 和 MapView 的区别
5.POI检索
1. 创建POI检索实例
PoiSearch poiSearch = PoiSearch.newInstance();
2. 设置POI检索监听,监听请求后的结果
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
@Override
public void onGetPoiResult(PoiResult poiResult) {
List<PoiInfo> allPoi = poiResult.getAllPoi();
}
@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
}
else {
}
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
}
};
poiSearch.setOnGetPoiSearchResultListener(poiListener);
3. 发送POI请求
poiSearch.searchInBound(new PoiBoundSearchOption()
.bound(new LatLngBounds.Builder().include(xykj).include(zhongshan).build())
.keyword("超市"));
poiSearch.searchNearby(new PoiNearbySearchOption()
.location(zhongshan)
.keyword("公园")
.radius(20000)
.pageNum(2));
poiSearch.searchInCity(new PoiCitySearchOption()
.city("纽约")
.keyword("超市"));
poiSearch.searchPoiDetail(new PoiDetailSearchOption().poiUid(poiInfo.uid));
poiSearch.destroy();
6.路线规划
1. 初始化路线规划实例
RoutePlanSearch routePlanSearch = RoutePlanSearch.newInstance()
2. 设置规划请求后的监听,共有6个方案
routePlanSearch.setOnGetRoutePlanResultListener(this)
// 驾车、步行、公交、骑行、跨城综合、室内路线搜索
3. 发出规划请求
//跨城
mSearch.masstransitSearch(new MassTransitRoutePlanOption()
.from(PlanNode.withCityNameAndPlaceName("北京", "天安门"))
.to(PlanNode.withCityNameAndPlaceName("上海", "东方明珠")))
//驾车
mSearch.drivingSearch((new DrivingRoutePlanOption())
.from(stNode).to(enNode))
//公交
mSearch.transitSearch((new TransitRoutePlanOption())
.from(stNode).city("北京").to(enNode))
//步行
mSearch.walkingSearch((new WalkingRoutePlanOption())
.from(stNode).to(enNode))
//骑行
mSearch.bikingSearch((new BikingRoutePlanOption())
.from(stNode).to(enNode))
4. 销毁
mSearch.destroy()
7.定位
1. 使用SourceSets