提交 0805d1c0 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

腾讯定位模块增加 监听位置变化,清除监听 方法

上级 84c979e8
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text">
1. 腾讯定位sdk需在腾讯地图官网申请key。https://lbs.qq.com/mobile/androidMapSDK/
developerGuide/getKey。
</view>
<view class="uni-hello-text">
2. 将key配置在uts插件uts-tencentgeolocation/utssdk/app-android/AndroidManifest.xml中。
</view>
</view>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="checkLocationPermission">请求定位权限</button>
<button type="primary" @tap="testGetlocation">腾讯地图获取定位(需自定义基座)</button>
</view>
</view>
</template>
<script>
import {requestPremission,getLocation} from "@/uni_modules/uts-tencentgeolocation";
export default {
data() {
return {
title: '腾讯定位SDK集成示例',
}
},
methods: {
checkLocationPermission:function(e){
requestPremission();
},
testGetlocation:function(e){
let startRet = getLocation({
geocode:true,
success:function(response){
console.log(response);
var addressDesc = response.name + '-' + response.address
uni.showToast({
title:'执行结果:' + addressDesc,
icon:'none'
});
},
fail:function(msg) {
uni.showToast({
title: msg,
icon:"none"
})
}
})
if(!startRet){
uni.showToast({
title:'定位启动失败,请检查配置',
icon:'none'
});
}
},
}
}
</script>
<style>
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text">
1. 腾讯定位sdk需在腾讯地图官网申请key。 https://lbs.qq.com/
</view>
<view class="uni-hello-text">
2. 按照readme文档配置apikey
</view>
<view class="uni-hello-text">
3. 需要制作自定义基座运行
</view>
</view>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="checkLocationPermission">请求定位权限</button>
<button type="primary" @tap="testGetlocation">获取设备位置信息</button>
<button type="primary" @tap="testWatchPosition">监听设备位置信息</button>
<button type="primary" @tap="testClearWatch">停止监听</button>
</view>
</view>
</template>
<script>
import {
requestPremission,
getLocation,
watchPosition,
clearWatch
} from "@/uni_modules/uts-tencentgeolocation";
export default {
data() {
return {
title: '腾讯定位SDK集成示例',
}
},
methods: {
checkLocationPermission: function(e) {
requestPremission();
},
testGetlocation: function(e) {
let startRet = getLocation({
geocode: true,
success: function(response) {
console.log(response);
var addressDesc = response.name + '-' + response.address
uni.showToast({
title: '执行结果:' + addressDesc,
icon: 'none'
});
},
fail: function(msg) {
uni.showToast({
title: msg,
icon: "none"
})
}
})
if (!startRet) {
uni.showToast({
title: '定位启动失败,请检查配置',
icon: 'none'
});
}
},
testWatchPosition() {
watchPosition({
geocode: true,
success: function(response) {
console.log(response);
var addressDesc = response.name + '-' + response.address
uni.showToast({
title: '执行结果:' + addressDesc,
icon: 'none'
});
},
fail: function(msg) {
uni.showToast({
title: msg,
icon: "none"
})
}
})
},
testClearWatch() {
clearWatch()
}
}
}
</script>
<style>
</style>
......@@ -20,17 +20,15 @@ class CaptureScreenTool {
// 捕获截屏回调的方法
// target-action 的方法前需要添加 @objc 前缀
@objc static userDidTakeScreenshot() {
const obj = new UTSJSONObject()
// 回调
this.listener?.(obj)
this.listener?.({})
}
// 移除监听事件
static removeListen(callback?: UTSCallback) {
this.listener = null
NotificationCenter.default.removeObserver(this)
const obj = new UTSJSONObject()
callback?.(obj)
callback?.({})
}
}
......
import { CLLocationManager, CLAuthorizationStatus } from "CoreLocation"
import { TencentLBSLocationManager, TencentLBSLocation, Error, TencentLBSRequestLevel} from "TencentLBS"
import { TencentLBSLocationManager, TencentLBSLocation, Error, TencentLBSRequestLevel, TencentLBSLocationManagerDelegate} from "TencentLBS"
import Bundle from "Foundation"
......@@ -25,13 +25,15 @@ type LocationResponse = {
/**
* 定位 LBSLocation 类,封装定位相关方法
*/
class LBSLocation {
class LBSLocation implements TencentLBSLocationManagerDelegate {
// 定义 locationManager 属性,类型为 TencentLBSLocationManager
static locationManager: TencentLBSLocationManager
locationManager: TencentLBSLocationManager
locationOptions?: LocationOptions
// 初始化 locationManager 方法
static configLocationManager(): boolean {
configLocationManager(): boolean {
if (this.locationManager == null) {
// 从 info.plist 中读取 apiKey
......@@ -48,13 +50,14 @@ class LBSLocation {
this.locationManager = new TencentLBSLocationManager()
// 设置 apiKey (因为 apiKey 是 any?类型,需要转成 string 类型赋值)
this.locationManager.apiKey = apiKey! as string;
this.locationManager.delegate = this
}
return true
}
// 请求定位权限
static requestPremission() {
requestPremission() {
if (this.configLocationManager()) {
const status = CLLocationManager.authorizationStatus()
// 如果未获取过定位权限,则发起权限请求
......@@ -64,8 +67,8 @@ class LBSLocation {
}
}
// 获取位置信息
static getLocation(locationOptions: LocationOptions): boolean {
// 获取单次位置信息
getLocation(locationOptions: LocationOptions): boolean {
// 初始化 locationManager
if (!this.configLocationManager()) {
......@@ -95,18 +98,74 @@ class LBSLocation {
return true
}
// 监听位置变化
watchPosition(locationOptions: LocationOptions) {
// 初始化 locationManager
if (!this.configLocationManager()) {
return
}
if (locationOptions.geocode) {
this.locationManager.requestLevel = TencentLBSRequestLevel.name
} else {
this.locationManager.requestLevel = TencentLBSRequestLevel.geo
}
this.locationOptions = locationOptions
this.locationManager.startUpdatingLocation()
}
// 清除监听
clearWatch() {
// 初始化 locationManager
if (!this.configLocationManager()) {
return
}
this.locationManager.stopUpdatingLocation()
}
// 实现定位出错的 delegate 方法
tencentLBSLocationManager(manager: TencentLBSLocationManager, error: Error) {
this.locationOptions?.fail(error.localizedDescription)
}
// 实现位置更新的 delegate 方法
tencentLBSLocationManager(manager: TencentLBSLocationManager, @argumentLabel("didUpdate") location: TencentLBSLocation) {
let response = new LocationResponse();
response.name = location.name;
response.address = location.address;
response.latitude = Number(location.location.coordinate.latitude);
response.longitude = Number(location.location.coordinate.longitude);
this.locationOptions?.success(response)
}
}
const LBSLocationTool: LBSLocation = new LBSLocation()
/**
* 导出请求权限方法
* 请求定位权限方法
*/
export function requestPremission() {
LBSLocation.requestPremission()
LBSLocationTool.requestPremission()
}
/*
* 导出请求位置信息方法
* 获取位置信息方法(单次定位)
*/
export function getLocation(locationOptions: LocationOptions): boolean {
return LBSLocation.getLocation(locationOptions)
return LBSLocationTool.getLocation(locationOptions)
}
/**
* 持续监听位置变化
*/
export function watchPosition(locationOptions: LocationOptions) {
LBSLocationTool.watchPosition(locationOptions)
}
/**
* 关闭监听位置变化
*/
export function clearWatch() {
LBSLocationTool.clearWatch()
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册