按照下面的流程走,带你轻松学会百度地图的基本开发。
1.申请API Key
打开下面的网址
http://lbsyun.baidu.com/apiconsole/key
点击创建应用
填写应用名称为你的Android软件名称;
选择应用类型为Android SDK
SHA1为 打开你的Android studio
点击右侧的Gradle 然后再点击列表中的signingReport
运行结果:
将SHA1复制进去
包名为:在build.gradle(Module:app)中可以找到
点击提交
2.下载百度LBS开发平台的SDK并导入到你的项目中
下载地址
http://lbsyun.baidu.com/index.php?title=androidsdk/sdkandev-download
点击自定义下载,下载开发包
下载列表为:
1)将下面两个jar文件复制到项目文件的lib包里:
然后进行导入操作:
2)在app目录下新建文件夹jniLibs将上面下载的五个文件夹复制到你新进的jniLibs文件夹中
3.接下来就是写代码工作了
1)首先是添加权限
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!-- 用于读取手机当前的状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
2)然后是在
中添加:
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="API Key值" />
//此value的值是你在百度地图上的 访问应用(API Key)
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote"></service>
注:其中value值是你刚刚申请的API Key
3)界面代码(xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Space
android:layout_width="20dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/commomMap"
android:layout_weight="1"
android:text="普通地图"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Space
android:layout_width="80dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/satelliteMap"
android:layout_weight="1"
android:text="卫星地图"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="492dp"
android:gravity="bottom"
android:paddingLeft="3dp"
android:paddingTop="450dp">
<Button
android:id="@+id/destination"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="0dp"
android:text="◉"
android:textColor="#000000"
android:textSize="30dp" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
4)java代码
package com.example.testmap;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
public LocationClient mLocationClient;
private MapView mapView;
private BaiduMap baiduMap;
private boolean isFirstLocate = true;
private RadioButton commomMap,satelliteMap;
private Button destination;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加在setContentView(R.layout.activity_main);之前的代码
mLocationClient = new LocationClient(getApplicationContext());
//监听器
mLocationClient.registerLocationListener(new MyLocationListener());
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.bmapView);
//获取地图
baiduMap = mapView.getMap();
baiduMap.setMyLocationEnabled(true);//开启定位图标此功能
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
destination = findViewById(R.id.destination);
destination.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isFirstLocate = true;
}
});
//隐藏百度地图图标
View child = mapView.getChildAt(1);
if (child != null && (child instanceof ImageView || child instanceof ZoomControls)){
child.setVisibility(View.INVISIBLE);
}
commomMap = findViewById(R.id.commomMap);
commomMap.setChecked(true);
commomMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);//普通地图
// baiduMap.setIndoorEnable(true);
}
});
satelliteMap = findViewById(R.id.satelliteMap);
satelliteMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);//卫星地图
}
});
//权限的设置
List<String> permissionList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()){
String[] permissions = permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else{
requestLocation();
}
}
/*
获取当前的位置信息,并且让地图显示在自己的位置
*/
private void navigateTo(BDLocation location){
if (isFirstLocate){
//移动到我的位置
LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
update = MapStatusUpdateFactory.zoomTo(16f);//缩放级别
baiduMap.animateMapStatus(update);
isFirstLocate = false;
}
//让我在地图上显示出来
MyLocationData locationBuilder = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
// locationBuilder.latitude(location.getLatitude());
// locationBuilder.latitude(location.getLongitude());
// MyLocationData locationData = locationBuilder.build();
baiduMap.setMyLocationData(locationBuilder);//设置图标在地图上的位置
}
private void requestLocation(){
initLocation();
mLocationClient.start();
}
//改善start()方法只能定位一次,在快速移动中,可以实时更新当前的位置
private void initLocation(){
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");//设置bd09ll定位更精确,返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(1000);
option.setOpenGps(true);// 打开gps
// option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
mapView.onDestroy();
baiduMap.setMyLocationEnabled(false);//在程序退出时,关闭定位图标此功能
}
/////////
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case 1:
if(grantResults.length>0){
for (int result:grantResults){
if (result!=PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,"必须同意所有权限才能使用本程序",Toast.LENGTH_SHORT).show();
finish();
return;
}
}
requestLocation();
}else{
Toast.makeText(this,"发生未知错误",Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(final BDLocation bdLocation) {
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation
|| bdLocation.getLocType() == BDLocation.TypeNetWorkLocation){
navigateTo(bdLocation);
}
}
}
}