最近需要做一个 SDK 定位的项目,由于 APP 需在安卓应用商店上架,高德需付费,在网上搜到一家做互联网全域定位的维智科技,他们也提供设备定位 SDK,文档、支持还比较完善,个人在集成过程中遇到一些问题,分享给大家,避免走弯路。
1.在AndroidManifest.xml 添加相前权限
<!--外部存储读取权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<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.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<!--允许程序发现和配对蓝牙设备 -->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />
2.添加 Service
<!-- 必选-->
<service android:name="com.wayz.location.WzService"></service>
3.先声明
MapsInitializer.updatePrivacyShow(this, true, false)
MapsInitializer.updatePrivacyAgree(this, true)
4.初始化定位模式
private WzLocationClientOption option;
private fun initLocationClient() {
option = WzLocationClientOption()
// 回调间隔
option.isFastLocation = false
option.interval = 5000
option.isLockScreenLocation = true
option.isUsingSensor = true
option.locationMode = WzLocationMode.HIGHT_ACCURACY
option.setReportHost("")
option.setOnceLocate(false)
client = WzLocationClient(this@BasicLocContinuousActivity, option)
}
5.实现定位监听
class BasicLocContinuousActivity : AppCompatActivity(), WzLocationListener{
override fun onLocationReceived(location: WzLocation) {
// 成功
}
override fun onLocationError(exception: WzException) {
// 失败
}
}
6.整体代码
/*
* Copyright (C) 2017 GlobalEarth, Inc. All Rights Reserved.
*/
package com.example.sample
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.wayz.location.*
import kotlinx.android.synthetic.main.activity_main.*
import pub.devrel.easypermissions.EasyPermissions
class BasicLocActivity : AppCompatActivity(), WzLocationListener, EasyPermissions.PermissionCallbacks {
private lateinit var client: WzLocationClient
private lateinit var option: WzLocationClientOption
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
location_btn.setOnClickListener {
if(checkPermisson()) {
tv.text = "正在定位..."
tv.visibility = View.VISIBLE
client?.startLocation(this@BasicLocActivity)
} else {
tv.text = "缺少权限..."
tv.visibility = View.VISIBLE
}
}
if(checkPermisson()) {
initLocationClient()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
}
private fun checkPermisson() : Boolean {
if (!EasyPermissions.hasPermissions(this, *MyApp.permissions)) {
EasyPermissions.requestPermissions(this, "需要申请如下权限", 1, *MyApp.permissions)
return false
} else {
return true
}
}
private fun initLocationClient() {
option = WzLocationClientOption()
// 回调间隔
option.interval = 5000
option.isFastLocation = false
option.setOnceLocate(true)
client = WzLocationClient(this@BasicLocActivity, option)
}
override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
}
override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
initLocationClient()
}
override fun onLocationReceived(location: WzLocation) {
if (location != null) {
val stringBuilder = StringBuilder()
.append("Address:")
.append(location.address)
.append("\n").append("Accuracy:")
.append(location.accuracy)
.append("\n").append("Confidence:")
.append(location.confidence)
.append("\n").append("Longitude:")
.append(location.longitude)
.append("\n").append("Latitude:")
.append(location.latitude)
.append("\n").append("Altitude:")
.append(location.altitude)
.append("\n").append("Source:")
.append(location.source)
.append("\n")
if (location.place != null) {
stringBuilder.append("\nPlace:")
stringBuilder.append(location.place.name)
}
val nearbyPlaces = location.nearbyPlaces
if (nearbyPlaces != null && nearbyPlaces.size > 0) {
stringBuilder.append("\n\nNearby:")
for (nearbyPlace in nearbyPlaces) {
stringBuilder.append("\n${nearbyPlace.name}")
}
}
tv.text = stringBuilder.toString()
} else {
tv.text = "数据为空"
}
}
override fun onLocationError(exception: WzException) {
tv.text = StringBuilder().append("数据异常:").append(exception.errorMessage).toString()
}
override fun onDestroy() {
client?.onDestroy()
super.onDestroy()
}
}
注意事项:
1. 打包混淆,如 果 您 打 包 时 开 启 了 混 淆 建 议您在混淆规则中保留SDK在proguard-rules.pro 添加:
-keep class com.wayz.location.** {*;}
-dontwarn com.wayz.location.**
2. 如果您的应用 targetSdkVersion >= 23,则需要在调用SDK之前申请动态权限,否则在 Android 6.0 以上手机上运行可能会崩溃。
3. SDK 应用需要收集 WIFI 信息,如果运行在Android 6.0 以上手机上扫描 WIFI 信息需要打开 GPS 开关(位置服务选为高精度),如果您的应用不是系统应用,建议引导用户打开此开关,便于收集定位信息。
4. 在集成过程中,如果定位失败,首先检查是否添加相关权限,以及手机定位权限是否开启,还有手机系统日期时间是否正常,定位权限
网友评论