一、 申请API_KEY
1. http://lbsyun.baidu.com/ :在这里注册并申请。注册之后出现应用列表,选择 创建应用 :
2. 创建应用后如下图所示:
- 应用类型选择的话,我们选择 Android SDK 就可以了。
PS:SHA1
找了我好久...
- SHA1寻找之路:
左侧选择项目目录类型 project ,打开右侧 Gradle > app > android > signingReport ,点击发现空白,突然发现左下角有个转换的键(左下标红的地方),答案出来了。
3. 获取基本参数
- 创建完成,获得
API_KEY
。
二、 下载百度定位SDK
- 如无特殊要求,选择基础功能即可:
- 下载开发包解压后得到 libs 文件夹。
- BaiduLBS_android.jar 放在 project 目录下的 app > libs 目录下,其他so库放在 app > main > 新建文件夹jniLibs 下,或者都可以放jniLibs下。
三、 使用百度地图
1. 在 AndroidManifest.xml 中添加如下权限:
- 在
Application
标签中声明SERVICE
组件,每个APP拥有自己单独的定位SERVICE
:
- 百度定位SDK在4.2版本之后需要在 AndroidMainfest.xml 中正确设置
Accesskey(AK)
,如果设置错误将会导致定位和地理围栏服务无法正常使用。设置AK
,在Application
标签中加入:
//AK:开发者申请的Key
2. 新建LBSwithBaidu
,LBSwithBaidu
代码如下:
public class LBSwithBaidu extends AppCompatActivity {
private TextView text;
private LocationClient client;
private StringBuilder connrentPosition;
private MapView map;
private BaiduMap baidumap;
private boolean ismylocation=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SDK的初始化
SDKInitializer.initialize(getApplicationContext());
//注册客户端定位的监听
client = new LocationClient(getApplicationContext());
client.registerLocationListener(new MylocationListener());
setContentView(R.layout.activity_lbswith_baidu);
text= (TextView) findViewById(R.id.textView_location);
//获取地图控件的引用
map= (MapView) findViewById(R.id.mapwithbaidu);
//获取地图实例
baidumap=map.getMap();
//设置是否在地图上显示位置
baidumap.setMyLocationEnabled(true);
//6.0以上动态权限的获取
List permission = new ArrayList<>();
if (ContextCompat.checkSelfPermission(LBSwithBaidu.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permission.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(LBSwithBaidu.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
permission.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(LBSwithBaidu.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permission.isEmpty()) {
String[] permissions = permission.toArray(new String[permission.size()]);
ActivityCompat.requestPermissions(LBSwithBaidu.this, permissions, 1);
} else {
requestion();
}
}
private void requestion() {
initlocation();
client.start();
}
private void initlocation() {
LocationClientOption option=new LocationClientOption();
option.setIsNeedAddress(true);
option.setScanSpan(5000);
client.setLocOption(option);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] 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;
}
}
requestion();
}else {
Toast.makeText(this, "发生错误", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
public void nagativato(BDLocation bdLocation) {
if (ismylocation) {
LatLng ll = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
Log.d("pipa",bdLocation.getLatitude()+"::"+ bdLocation.getLongitude());
MapStatusUpdate updata = MapStatusUpdateFactory.newLatLng(ll);
baidumap.animateMapStatus(updata);
updata = MapStatusUpdateFactory.zoomTo(16f);
baidumap.animateMapStatus(updata);
ismylocation = false;
}
MyLocationData.Builder loBuilder=new MyLocationData.Builder().latitude(bdLocation.getLatitude())
.longitude(bdLocation.getLongitude());
MyLocationData locationData=loBuilder.build();
baidumap.setMyLocationData(locationData);
}
public class MylocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
String name = bdLocation.getLocType();
if(name==BDLocation.TypeGpsLocation||name==BDLocation.TypeNetWorkLocation){
nagativato(bdLocation);
}
connrentPosition = new StringBuilder();
connrentPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n");
connrentPosition.append("经度:").append(bdLocation.getLongitude()).append("\n");
connrentPosition.append("国家:").append(bdLocation.getCountry()).append("\n");
connrentPosition.append("省:").append(bdLocation.getProvince()).append("\n");
connrentPosition.append("市:").append(bdLocation.getCity()).append("\n");
connrentPosition.append("县:").append(bdLocation.getDistrict()).append("\n");
connrentPosition.append("街道:").append(bdLocation.getStreet()).append("\n");
connrentPosition.append("定位方式:");
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
connrentPosition.append("GPS");
} else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
connrentPosition.append("网络定位");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
text.setText(connrentPosition);
}
});
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
@Override
protected void onResume() {
super.onResume();
//地图生命周期需实现
map.onResume();
}
@Override
protected void onPause() {
super.onPause();
//地图生命周期需实现
map.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
client.stop();
//地图生命周期需实现
map.onDestroy();
baidumap.setMyLocationEnabled(false);
}
}
3. 最终效果图:
如有不对之处,欢迎指正,谢谢~