Development environment:
android studio
table of Contents
One: Prepare the environment
1. First, you must have a Tencent development account.
Portal: https://lbs.qq.com/
and register after entering.
2. Obtain APPKey:
3. Configure APPKey under AndroidManifest.xml file:
<application>
...
<meta-data android:name="TencentMapSDK" android:value="您申请的Key" />
</application>
4.
There are actually two ways to import AndroidStudio , one is to import the jar package directly, and the second is to add dependencies. Because the second type is much more convenient, I chose the second one. From this point of view, Tencent's positioning is indeed much better than Baidu. He used Baidu for the last semester.
1. Modify the configuration in build.gradle and
add the following configuration:
llprojects {
repositories {
jcenter()
google()
mavenCentral()
}
}
2. Add dependencies to dependencies in the second build.gradle file
implementation 'com.tencent.map.geolocation:TencentLocationSdk-openplatform:7.2.6'
5. Add permissions in the AndroidManifest.xml file:
<!-- 通过GPS得到精确位置 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 通过网络得到粗略位置 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 访问网络. 某些位置信息需要从网络服务器获取 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 访问WiFi状态. 需要WiFi信息用于网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 修改WiFi状态. 发起WiFi扫描, 需要WiFi信息用于网络定位 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!-- 访问网络状态, 检测网络的可用性. 需要网络运营商相关信息用于网络定位 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 访问网络的变化, 需要某些信息用于网络定位 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<!-- 访问手机当前状态, 需要device id用于网络定位 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 支持A-GPS辅助定位 -->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<!-- 用于 log 日志 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The previous preparations are done and then start the code part
Two: coding part
1. Obtain an instance of TencentLocationManager
TencentLocationManager mLocationManager = TencentLocationManager.getInstance(this);
2. Create a location listener
public class MyActivity extends Activity implements TencentLocationListener {
...
@Override
public void onLocationChanged(TencentLocation location, int error, String reason) {
// 用于接收定位结果
}
@Override
public void onStatusUpdate(String name, int status, String desc) {
//用于接收GPS、WiFi、Cell状态码,
}
}
3. Construct TencentLocationRequest
TencentLocationRequest request = TencentLocationRequest.create()
//1.用户可以自定义定位间隔,事件单位为毫秒,不得小于1000毫秒
request.setInterval(1000);
//2.设置请求级别
request.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_NAME);
//3.是否允许使用GPS(建议用户开启,在室外场景可以显著提升定位精度);
request.setAllowGPS(true);
//4.是否需要获取传感器方向
request. setAllowDirection(true);
//5.是否需要开启室内定位
request.setIndoorLocationMode(true);
The request level inside can be customized, the reference table is as follows:
4. Initiate contact location request (start location)
mLocationManager.requestLocationUpdates(request,this);
5. Stop positioning
// mLocationManager.removeUpdates(this);
6. Positioning results:
Three: Comparison of Baidu and Tencent
Personally, I feel that Tencent’s SDK is much simpler and more convenient to use than Baidu. Baidu’s project package name must be consistent with the local one, while Tencent’s location service does not have this restriction, and it can even be named in Chinese. SDK Tencent only needs to import dependencies. Functional aspects need further testing.