概述
官方文档:http://lbsyun.baidu.com/index.php?title=android-locsdk
1.获取AK
发布版:
keytool -v -list -keystore key.jks
SHA1:A9:7D:70:70:4C:67:83:71:AC:FD:18:6E:EF:9B:08:FF:CF:07:A6:BD
开发版:
keytool -v -list -keystore debug.keystore
SHA1:39:FC:60:2A:FA:CB:BF:6C:79:CA:98:58:C6:2F:F8:58:FE:56:24:2F
2.创建定位测试项目
1)AndroidManifest.xml
增加权限,声明service和ak鉴权
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_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_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" >
</uses-permission>
<uses-permission android:name="android.permission.VIBRATE" >
</uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS" >
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- 声明service组件 -->
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
<!-- AK鉴权 -->
<!-- meta-data需要写在application中 -->
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="V57HWvyoHwUYEK15Nws2kAZYvSwWtC2q" />
2)导入项目所需的jar包和so
BaiduLBS_Android.jar
libBaiduMapSDK_base_v4_5_2.so
libBaiduMapSDK_map_v4_5_2.so
liblocSDK7b.so
在app的build.gradle android{}中说明so为该libs路径
android {
...
sourceSets {
main {
jniLibs.srcDir 'libs'
}
}
}
3.定位流程
1)初始化LocationClient类
LocationClient类最好在主线程中声明,需要Context类型的参数。
Context需要时全进程有效的Context,推荐用getApplicationConext获取全进程有效的Context
mLocClient = new LocationClient(getApplicationContext());
mLocClient.registerLocationListener(listener);
2)配置SDK参数
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);//是否要地址
option.setOpenGps(true);
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);//可选,默认0,即仅定位一次,设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
mLocClient.setLocOption(option);
mLocClient.start();
3)实现BDAbstractLocationListener接口
Android定位SDK自v7.2版本起,对外提供了Abstract类型的监听接口BDAbstractLocationListener,用于实现定位监听。原有BDLocationListener暂时保留,推荐开发者升级到Abstract类型的新监听接口使用,该接口会异步获取定位结果
@Override
public void onReceiveLocation(BDLocation bdLocation) {
Log.i("bdmap","定位类型:"+bdLocation.getLocTypeDescription()+"n"
+"纬度:"+bdLocation.getLatitude()+"n"
+"经度:"+bdLocation.getLongitude()+"n"
+"详细地址:"+bdLocation.getAddrStr()+"n"
+"卫星数目"+bdLocation.getSatelliteNumber());
}
输出:
I/bdmap: 定位类型:GPS location successful!
纬度:30.555669
经度:104.075068
详细地址:中国四川省成都市武侯区G213(xxxxxx)
卫星数目4
4.增加Mark
LatLng ll = new LatLng(bdLocation.getLatitude(),
bdLocation.getLongitude());
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.icon_openmap_mark);
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(ll) //mark出现的位置
.icon(bitmap) //mark图标
.draggable(true);//mark可拖动
//在地图上添加Marker并显示
mBaiduMap.addOverlay(option);
//对Marker的点击事件
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//获取marker中的数据
double lat = marker.getPosition().latitude;
double log = marker.getPosition().longitude;
Toast.makeText(getApplication(),
"经度"+log+" 纬度"+lat,Toast.LENGTH_LONG)
.show();
return false;
}
});
5.Demo
public class MainActivity extends AppCompatActivity {
private MapView mMapView = null;
private BaiduMap mBaiduMap = null;
boolean isFirstLoc = true; // 是否首次定位
LocationClient mLocClient;
private MyLocationData locData; //定位数据包
private Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mMapView = (MapView)findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
//设置类型
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(17));
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
mLocClient = new LocationClient(getApplicationContext());
mLocClient.registerLocationListener(listener);
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);//是否要地址
option.setOpenGps(true);
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);//可选,默认0,即仅定位一次,设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
mLocClient.setLocOption(option);
mLocClient.start();
//对Marker的点击事件
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//获取marker中的数据
double lat = marker.getPosition().latitude;
double log = marker.getPosition().longitude;
Toast.makeText(getApplication(),
"经度"+log+" 纬度"+lat,Toast.LENGTH_LONG)
.show();
return false;
}
});
//Map点击事件,点击地图移除marker
mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
marker.remove();//移除marker
}
@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
return false;
}
});
}
/***
* 定位结果回调,在此方法中处理定位结果
*/
BDAbstractLocationListener listener = new BDAbstractLocationListener() {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
Log.i("bdmap","定位类型:"+bdLocation.getLocTypeDescription()+"n"
+"纬度:"+bdLocation.getLatitude()+"n"
+"经度:"+bdLocation.getLongitude()+"n"
+"详细地址:"+bdLocation.getAddrStr()+"n"
+"卫星数目"+bdLocation.getSatelliteNumber());
//构造定位数据
locData = new MyLocationData.Builder()
.accuracy(bdLocation.getRadius())
.latitude(bdLocation.getLatitude())
.longitude(bdLocation.getLongitude())
.build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(bdLocation.getLatitude(),
bdLocation.getLongitude());
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.icon_openmap_mark);
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(ll) //mark出现的位置
.icon(bitmap) //mark图标
.draggable(true);//mark可拖动
//在地图上添加Marker并显示
marker = (Marker) mBaiduMap.addOverlay(option);
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(17.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
}
};
@Override
protected void onDestroy() {
//退出时销毁定位
mLocClient.stop();
//关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
}
最后
以上就是健壮诺言为你收集整理的Android 百度定位SDK的全部内容,希望文章能够帮你解决Android 百度定位SDK所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复