移动警务考勤打卡组合定位实现

一、业务逻辑

本次分享的是移动警务项目考勤模块的代码实现,该模块的主要业务逻辑是用户到达要考勤的目标点-》打开移动警务软件的考勤模块-》系统自动进行组合定位-》定位成功后,选择打卡方式-》最后提交考勤结果即可,具体步骤如下:

1、用户登录后在首页选择考勤管理,进入签到记录页面,点击右上角的+号,进入考勤签到模块;

 

2、进入考勤签到模块,若GPS已经开启,则会在20秒内尝试获取GPS数据,如果定位成功,则调用业务平台的POI查询接口,获取当前考勤点的名称,跳转到步骤5;

3、若GPS定位失败,则会在10秒内进行MapABC的网络定位,如果定位成功,则调用业务平台的POI查询接口,获取当前考勤点的名称, 跳转到步骤5;

4、若MapABC的网络定位,则会在10秒内调用辽宁基地的SDK进行定位,如果定位成功,则调用业务平台的POI查询接口,获取当前考勤点的名称, 跳转到步骤5,否则跳转到步骤7;

5、显示当前的考勤点信息;

6、选择考勤点,进行打开,程序跳转到上一页面面;

7、如果基地的SDK定位仍然失败,则本次定位失败!


二、流程图



三、代码实现

1、定义变量,用于保存超时计数和获得的经纬度

local g_retryCount    = 20     -- GPS定位超时计数
local net_retryCount  = 20     -- 网络定位超时计数
local latitude        = 0      -- 当前的纬度:字符串类型,小数点后精确6位
local longitude       = 0      -- 当前的经度:字符串类型,小数点后精确6位

 

2、组合定位的入口函数

-- @brief 组合定位方式获取当前的经纬度
function getCurrentLocation()
    -- 初始化MapABC定位地址
    g_address = nil 
    local locInfo = Sprite:findChild(rootSprite, 'kaoqinlocation')
    Sprite:setProperty(locInfo, 'text', '正在定位......')
    -- 当前无经纬度信息,检查当前gps状态
    local result = System:getGPSStatus()
    if result == -1 then
        Sprite:setProperty(locInfo, 'text', '当前GPS不可用, 尝试使用网络定位...')
        Log:write('GPS开启失败, 尝试使用网络定位...')
        getLocationByNetwork()
    elseif result == 0 then
        Sprite:setProperty(locInfo, 'text', 'GPS未开启,尝试使用网络定位...')
        getLocationByNetwork()
    elseif result == 1 then
        Sprite:setProperty(locInfo, 'text', 'GPS已经开启,正在使用GPS定位...')
        Timer:set(222, 1000, 'getLocationByGPS')
    end
end

 

3、利用GPS获取当前经纬度

-- @brief 利用GPS获取经纬度,需要多次进行请求
function getLocationByGPS()
    -- 获取当前的经纬度数据
    local locInfo = Sprite:findChild(rootSprite, 'kaoqinlocation')
    Sprite:setProperty(locInfo, "text", "GPS正在定位中,剩余"..g_retryCount.."秒")
    longitude,latitude = System:getGPSData()
    Log:write(string.format("当前经纬度: %s, %s", latitude, longitude))
    -- 对获得的数据进行处理
    if longitude ~= nil and longitude ~= 0 and latitude ~= nil and latitude ~= 0 
        and g_retryCount > 0 then
        Log:write("GPS数据有效!")
        g_retryCount = 20
        System:setGPSStatus(0)
        latitude, longitude = formatLocation(latitude, longitude)
        poiSearch(latitude, longitude, "0")
    elseif longitude == 0 and latitude == 0 and g_retryCount > 0 then
        -- 未超时需要继续请求
        Log:write("GPS数据无效!")
        g_retryCount = g_retryCount - 1
        Timer:set(222, 1000, 'getLocationByGPS')
    else
        -- 已经超时,进行网络定位
        System:setGPSStatus(0)
        g_retryCount = 20
        Log:write('GPS定位失败, 尝试使用网络定位...')
        Sprite:setProperty(locInfo, 'text', 'GPS定位失败,尝试使用网络定位...')
        getLocationByNetwork()
     end
end


4、网络定位入口函数

-- @brief 网络定位获取经纬度
function getLocationByNetwork()
    local locInfo = Sprite:findChild(rootSprite, 'kaoqinlocation')
    if net_retryCount > 10 then
        if net_retryCount == 20 then 
            getLocationByMapABC()
        end 
        Sprite:setProperty(locInfo, "text", "MapABC网络定位中,剩余"..(net_retryCount - 10).."秒")
        net_retryCount = net_retryCount - 1
    elseif net_retryCount > 0 then 
        if net_retryCount == 10 then 
            getLocationByJD()
        end
        Sprite:setProperty(locInfo, "text", "辽宁基地网络定位中,剩余"..net_retryCount.."秒")
        net_retryCount = net_retryCount - 1 
    else
        Sprite:setProperty(locInfo, "text", "定位失败,请稍候再试!")
        resetNetworkLocation()
        return
    end
    Timer:set(333, 1000, 'getLocationByNetwork')
end


5、高德定位获取当前经纬度

-- @brief 高德网络定位获取当前经纬度
function getLocationByMapABC()
    Log:write("尝试使用高德网络定位获取当前经纬度...")
    observer = Plugin:getObserver()
    Map:getCurPosition(observer, 1001)
end
-- @brief 获取接口返回时的反馈处理
function bodyOnPluginEvent(msg, param)
    if msg == 1001 then  -- MapABC定位返回
        local postDataString = Param:getString(param, 0)
        local postData = Json:loadString2Table(postDataString)
        Log:write('MapABC网络定位结果:', postData)
        if postData.longitude ~= nil and postData.latitude ~= nil then
            resetNetworkLocation()
            latitude, longitude = formatLocation(postData.latitude, postData.longitude)
            g_address = postData.desc
            poiSearch(latitude, longitude, "1")
        end   

 

6、辽宁基地获取当前经纬度

-- @brief 辽宁基地网络定位获取当前经纬度
function getLocationByJD()
    Log:write("尝试使用辽宁基地网络定位获取当前经纬度...")
    local code = LuaToJavaExec('Clutter','GetLocationByJD', "[]", 1, "GetLocationByJDCallback");
    Log:write("LuaToJavaExec函数返回:", code)
end

-- @brief 辽宁基地网络定位回调函数
function GetLocationByJDCallback(strMsg)
    Log:write("辽宁基地网络定位返回字符串:"..strMsg)
    local startPos = string.find(strMsg, "\"")
    local endPos = lastIndexof(strMsg, "\"")
    local locationStr = string.sub(strMsg, startPos + 1, endPos - 1)
    local locationArray = Split(locationStr, " ")
    latitude = locationArray[1]
    longitude = locationArray[2]
    Log:write("解析后的经纬度为:"..latitude..", "..longitude)
    latitude,longitude = formatLocation(latitude, longitude)
    resetNetworkLocation()
    poiSearch(latitude, longitude, "0")
end


7、格式化经纬度函数

-- @brief 格式化经纬度
function formatLocation(lat, lon)
    lat = tonumber(lat)
    lon = tonumber(lon)
    while lat > 90 do 
        lat = lat / 10
    end 
    while lon > 180 do
        lon = lon / 10
    end 
    lat = string.format("%.6f", lat)
    lon = string.format("%.6f", lon)
    Log:write(string.format("格式化后的经纬度为:%s, %s", lat, lon))
    return lat, lon
end


8、POI查询接口函数

-- @brief 由经纬度进行POI查询
-- localType : 0 - 真实经纬度, 1 - MapABC经纬度
function poiSearch(lat, lon, locateType)
    Log:write("进行POI查询,latitude:"..lat..", longitude:"..lon..", locateType:"..locateType)
    local isShifting  = locateType
    local requestUrl = string.format("http://120.209.131.154:8080/mobileSale/locateUserType/poi?mobile=%s&longtitude=%s&latitude=%s&radius=%s&isShifting=%s&locateType=%s&productCode=%s",
        Config:get("username"), lon, lat, "1", 
        isShifting, locateType, Config:get("productKey"))
    Log:write("自定义POI查询接口地址:"..requestUrl)
    Http:request('pois', requestUrl, 1003, {useCache = false})
    Loading:show()
end
elseif msg == 1003 then -- 自定义POI查询接口返回
        Loading:close()
        local json = Http:jsonDecode('pois')
        Log:write("自定义POI查询接口返回:", json)
        if json.success == "true" and json.list ~= nil 
            and getJsonArrayCount(json.list) > 0 then
            g_addressList = json.list
            -- 加载地址选择列表
            local len = getJsonArrayCount(g_addressList)
            local addrList = Sprite:findChild(rootSprite, "addrList")
            local addrSelectItem = Sprite:findChild(rootSprite, "addrSelectItem")
            ListView:loadItem(addrList, addrSelectItem, len, 'loadListItem')
            ListView:adjust(addrList) 
            -- 显示取消列表按钮
            local addrListNode = Sprite:findChild(rootSprite, "addrListNode")
            Sprite:setProperty(addrListNode, "visible", "true")
            Sprite:setProperty(addrListNode, "enable", "true")
        else
            Log:write("自定义POI查询失败或为空!")
            if g_address ~= nil then 
                Log:write("使用高德定位返回的地址")
                local locInfo = Sprite:findChild(rootSprite, 'kaoqinlocation')
                Sprite:setProperty(locInfo, "text", g_address)
                isLocateSuccess = true
            else
                Log:write("调用高德地址反解接口...")
                observer = Plugin:getObserver()
                latitude = tonumber(latitude) * 1000000
                longitude = tonumber(longitude) * 1000000
                Map:getLocation(observer, 1004, latitude, longitude)
            end 
        end 



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值