百度地图的基本使用
项目中需要获取用户的位置信息,以方便后期对客户所在地域进行分类统计,为运营人员提供数据支撑.而强大的百度SDK足以满足应用.
对于第三方SDK,官方开发指南是第一手资料.所以使用前请参考百度地图官方网站.Android开发模块中,Android地图SDK和Android定位SDK是两个不同的功能.开发前需要详细区分,因为后期的文件配置有所不同,否者无法获取正确的地理位置.
现在官网上Android定位的SDK为 7.1,本文基于此版本,对百度SDK使用时的文件配置和基本用法进行简单介绍.
首先是app中build.gradle的配置.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.lbstest1"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
android中通过sourceSets将JNI的目录指向libs.其中后缀为so的文件,封装的是C语言代码.因为Anroid底层是Java语言,具有跨平台特性,但是当与以C语言编写的本地操作系统交互时,操作系统就很难兼顾跨平台特性.使用so文件可以顺利解决Java和操作系统交互时的问题.
dependencies中指定依赖库文件为libs目录中以jar结尾的文件.点击同步Sync Project with Gradle Files,启动配置的gradle文件.
使用地图之前,需要向用户申请权限和配置启动百度时使用的服务.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lbstest1">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<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">
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="BVUH1RNr1nfIIO99oQppSXbtj3GGZoDs"/>
<activity android:name="com.example.lbstest.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.baidu.location.f" android:exported="true"
android:process=":remote" android:enabled="true"/>
</application>
</manifest>
然后在使用定位的地方,初始化地图管理类LocationClient.
public LocationClient mLocationClient;
private TextView positionText;
private MapView mapView;
private BaiduMap baiduMap;
private boolean isFirstLocate = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = (MapView)findViewById(R.id.bmapView);
positionText = (TextView)findViewById(R.id.textView);
baiduMap = mapView.getMap();
baiduMap.setMyLocationEnabled(true);
}
在用户允许授权后,就可以进行地图的初始化和调用.
private void initLocation(){
LocationClientOption option = new LocationClientOption();
option.setScanSpan(3000);
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
mLocationClient.start();
}
最后在监听接口中,获取请求到的位置信息.根据具体位置,就可以进行获取详细地址,定位,地图详情,周边搜索等操作.
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuilder currentPosition = new StringBuilder();
currentPosition.append("维度: ").append(bdLocation.getLatitude()).append("\n");
currentPosition.append("经度: ").append(bdLocation.getLongitude()).append("\n");
currentPosition.append("国家: ").append(bdLocation.getCountry()).append("\n");
currentPosition.append("省: ").append(bdLocation.getProvince()).append("\n");
currentPosition.append("城市: ").append(bdLocation.getCity()).append("\n");
currentPosition.append("区: ").append(bdLocation.getDistrict()).append("\n");
currentPosition.append("街道: ").append(bdLocation.getStreet()).append("\n");
currentPosition.append("定位方式: ");
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation){
currentPosition.append("GPS");
}else if(bdLocation.getLocType() == BDLocation.TypeNetWorkLocation){
currentPosition.append("网络");
}else if (bdLocation.getLocType() == BDLocation.TypeOffLineLocation){
currentPosition.append("Offline");
}else if (bdLocation.getLocType() == BDLocation.TypeCacheLocation){
currentPosition.append("Cache");
}else {
currentPosition.append("xxxx" + bdLocation.getLocType());
}
final String text = currentPosition.toString();
runOnUiThread(new Runnable() {
@Override
public void run() {
positionText.setText(text);
}
});
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation || bdLocation.getLocType() == BDLocation
.TypeNetWorkLocation){
navigateTo(bdLocation);
}
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
详细请参考Demo
总结
Android中第三方SDK的使用,基本遵循上述流程:首先配置gradle,引入支持的jar包或者so文件.然后在需要使用的地方实例化,对实例进行相应的配置.最后开启功能后,在暴露的接口方法中处理第三方提供的数据.
喜欢和关注都是对我的鼓励和支持~
上一篇: easyui