-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutil_location.lua
More file actions
117 lines (100 loc) · 3.6 KB
/
util_location.lua
File metadata and controls
117 lines (100 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
local util_location = {}
local TaskManager = require("task_manager")
-- 使用局部变量避免全局污染
local PRODUCT_KEY = "v32xEAKsGTIEQxtqgwCldp5aPlcnPs3K"
local lbsLoc = require("lbsLoc")
-- 任务名称常量
local CELLINFO_TASK_NAME = "util_location_cellinfo"
local REFRESH_TASK_NAME = "util_location_refresh"
local cache = { lbs_data = { 0, 0 } }
--- 格式化经纬度 (保留小数点后 6 位, 去除末尾的 0)
-- @param value 经纬度
-- @return 格式化后的经纬度
local function formatCoord(value)
local str = string.format("%.6f", tonumber(value) or 0)
str = str:gsub("%.?0+$", "")
return tonumber(str)
end
--- 生成地图链接
-- @param lat 纬度
-- @param lng 经度
-- @return 地图链接 or ""
local function getMapLink(lat, lng)
lat, lng = lat or 0, lng or 0
local map_link = ""
if lat ~= 0 and lng ~= 0 then map_link = "http://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng end
log.debug("util_location.getMapLink", map_link)
return map_link
end
--- lbsLoc.request 回调
local function getLocCb(result, lat, lng, addr, time, locType)
log.info("util_location.getLocCb", "result,lat,lng,time,locType:", result, lat, lng, time and time:toHex(), locType)
-- 获取经纬度成功, 坐标系WGS84
if result == 0 and lat and lng then
cache.lbs_data = { lat, lng }
end
end
--- 刷新基站信息
-- @param timeout 超时时间(单位: 秒)
local function refreshCellInfo(timeout)
log.info("util_location.refreshCellInfo", "start")
if cache.is_req_cell_info_running then
log.info("util_location.refreshCellInfo", "running, wait...")
else
cache.is_req_cell_info_running = true
mobile.reqCellInfo(timeout or 20) -- 单位: 秒
end
sys.waitUntil("CELL_INFO_UPDATE")
cache.is_req_cell_info_running = false
log.info("util_location.refreshCellInfo", "end")
end
--- 刷新基站定位信息
-- @param timeout 超时时间(单位: 毫秒)
function util_location.refresh(timeout)
timeout = type(timeout) == "number" and timeout or nil
-- 删除已存在的任务,避免重复创建
TaskManager.delete(REFRESH_TASK_NAME)
TaskManager.create(REFRESH_TASK_NAME, function()
refreshCellInfo()
lbsLoc.request(getLocCb, nil, timeout)
end, function(success, err)
if not success then
log.error("util_location", "refresh task failed:", err)
end
end)
end
--- 获取位置信息
-- @return lat
-- @return lng
-- @return map_link
function util_location.get()
local lat, lng = unpack(cache.lbs_data)
lat, lng = formatCoord(lat), formatCoord(lng)
return lat, lng, getMapLink(lat, lng)
end
-- 启动基站信息刷新任务(替代原来的无限循环任务)
function util_location.startCellInfoRefresh()
TaskManager.createLoop(CELLINFO_TASK_NAME, function()
refreshCellInfo()
end, 30000, function(success, err) -- 30秒刷新一次
if not success then
log.error("util_location", "cellinfo refresh task failed:", err)
end
end)
end
-- 停止基站信息刷新任务
function util_location.stopCellInfoRefresh()
TaskManager.stopLoop(CELLINFO_TASK_NAME)
end
-- 清理所有任务
function util_location.cleanup()
util_location.stopCellInfoRefresh()
TaskManager.delete(REFRESH_TASK_NAME)
end
sys.subscribe("CELL_INFO_UPDATE", function() log.debug("EVENT.CELL_INFO_UPDATE") end)
-- 模块加载时自动启动基站信息刷新
sys.taskInit(function()
sys.waitUntil("IP_READY", config.NETWORK_TIMEOUT_SHORT)
util_location.startCellInfoRefresh()
end)
return util_location