原文:
Android学习-- 基于位置的服务 LBS(基于百度地图Android SDK)--定位SDK
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37590688/article/details/76072563
前两天学习了一下Android的特色--LBS(基于百度地图Android SDK)。有些坑还是得填下,避免以后用到时忘记了。
一 .申请API_KEY
1)http://lbsyun.baidu.com/ :在这里注册并申请。注册之后出现应用列表
2)选择创建应用:下图所示
PS:SHA1找了我好久
按照书上面的,资料上看了,左侧选择项目目录类型project,打开右侧Gradle–>app–>android–>signingReport,发现,诶,怎么没有。。。。(黑人问号脸??),突然发现左下角有个转换的键(左下标红的地方),一按。。。,答案出来了。
3)完成
创建完成,获得API_KEY。
二 .下载百度定位SDK
下载解压后得到libs文件夹
BaiduLBS_android.jar放在project目录下的app–>libs目录下,其他目录放在app–>main–>新建文件夹比如:jniLibs下
三 .使用百度地图
1) AndroidManifest.xml
添加如下权限
<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<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_EXTERNAL_STORAGE"></uses-permission>
<!-- 访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
在Application标签中声明SERVICE组件,每个APP拥有自己单独的定位SERVICE
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote">
</service>
Android定位SDK4.2及之后版本需要在Mainfest.xml正确设置Accesskey(AK),如果设置错误将会导致定位和地理围栏服务无法正常使用。设置AK,在Application标签中加入
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="AK" /> //key:开发者申请的Key
2) 新建activity: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);
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);
List<String> 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) {
if(bdLocation.getLocType()==BDLocation.TypeGpsLocation||bdLocation.getLocType()==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);
}
}
效果图: