local PluginBase = require("luaScript.Plugins.PluginBase") --[[ 创建插件时更新一次定位信息 每次从后台切换回来,检查GPS开启状态,如果跟之前的状态不一样,则重新获取定位信息 --]] -- 获取玩家定位信息 local PluginAndroidLocation = class("PluginAndroidLocation" , PluginBase) function PluginAndroidLocation:ctor() PluginAndroidLocation.super.ctor(self); self.x = 0; -- 坐标x(单位米) self.y = 0; -- 坐标y(单位米) self.latitude = 0; -- 维度 self.longitude = 0; -- 经度 self.addr = "" -- 地址 self.desc = "" -- 描述 self.gpsStatus = 0; -- 最新的GPS开启状态 self.lastGpsStatus = 0; -- 上一次记录的GPS开启状态 self.timeLastUpdate = 0 self.timeDelta = 600; -- 秒 self.plugin = nil; -- 每次从后台切换回来都获取当期的GPS开启状态 app:addEventListener("applicationWillEnterForeground", handler(self, self.checkLocationEnable)); end function PluginAndroidLocation:start() self.plugin = cc.PluginManager:getInstance():createPlugin("PluginLocation" , "com/ddgame/plugin/LocationManager"); self:checkLocationEnable(); end --[[ GpsStatus = { close = 0, --// 未开启 undeal = 1, --// 未处理 enable = 2, --// 允许 disEnable = 3, --// 拒绝的 } --]] function PluginAndroidLocation:getGpsStatus(gpsEnable) if gpsEnable then -- if self:isHavePermission() then return 2 -- else -- return 1 -- end else return 0 end end -- 重置GPS数据 function PluginAndroidLocation:resetLocationInfo() self.x = 0; self.y = 0; self.latitude = 0; self.longitude = 0 self.gpsStatus = GpsStatus.close self.lastGpsStatus = GpsStatus.close end -- 获取GPS信息 function PluginAndroidLocation:getLocationInfo() local tt = { x = self.x, y = self.y, gpsStatus = self.gpsStatus, addr = self.addr, } logD("PluginAndroidLocation:getLocationInfo() tt = ", table.tostring(tt)) return tt; end --获取定位服务开启状态 function PluginAndroidLocation:getLocationEnable() logD("PluginAndroidLocation:getLocationEnable()"); if self.plugin then local locationEnable = self.plugin:callBool("getLocationServicesEnabled"); logD("PluginAndroidLocation:getLocationEnable() locationEnable = ", locationEnable); return locationEnable; end return false end --是否开启定位权限 function PluginAndroidLocation:isHavePermission() if self.plugin then local locationEnable = self.plugin:callBool("isHavePermission"); logD("PluginAndroidLocation:isHavePermission = ", locationEnable); return locationEnable; end return false end -- 前往GPS设置界面 function PluginAndroidLocation:gotoGpsSettingView() logD("PluginAndroidLocation:gotoGpsSettingView()"); if self.plugin then logD("PluginAndroidLocation:gotoGpsSettingView()"); local state = self.plugin:callVoid("gotoGpsSettingView"); end end -- 前往APP权限界面 function PluginAndroidLocation:gotoAppPermissionsView() logD("PluginAndroidLocation:gotoAppPermissionsView()"); if self.plugin then logD("PluginAndroidLocation:gotoAppPermissionsView()"); local state = self.plugin:callVoid("gotoAppPermissionsView"); end end -- 前往打开GPS function PluginAndroidLocation:openGPS() local locationEnable = self:getLocationEnable() if locationEnable then self:gotoAppPermissionsView() else self:gotoGpsSettingView() end end -- 每次从后台切换回来都获取当期的GPS开启状态 -- 如果本次的状态跟上一次不一样,则更新数据 function PluginAndroidLocation:checkLocationEnable() logD("PluginAndroidLocation:checkLocationEnable()"); local gpsEnable = self:getLocationEnable() logD("PluginAndroidLocation:checkLocationEnable(), gpsEnable = ", gpsEnable) self.gpsStatus = self:getGpsStatus(gpsEnable) logD("PluginAndroidLocation:checkLocationEnable(), self.gpsStatus = ", self.gpsStatus) -- logD("PluginAndroidLocation:checkLocationEnable(), self.lastGpsStatus = ", self.lastGpsStatus); -- self:updateLocation() if self.gpsStatus == GpsStatus.enable then -- 当前是开启的 if self.lastGpsStatus ~= self.gpsStatus then self:updateLocation() else local timeNow = os.time() if timeNow - self.timeLastUpdate > self.timeDelta then self:updateLocation() end end else -- 重置数据 self:resetLocationInfo() self:onGpsInoChanged() end self.lastGpsStatus = self.gpsStatus end -- 更新 GPS 位置信息 function PluginAndroidLocation:updateLocation() -- local timeNow = os.time() -- if timeNow - self.timeLastUpdate < self.timeDelta then -- return -- end logD("PluginAndroidLocation:updateLocation()"); if self.plugin then local params = { callback = handler(self, self.onResult); -- 回调函数 }; self.plugin:callVoid("start", params); end end -- 回调函数 -- function PluginAndroidLocation:onResult(result) -- logD("PluginAndroidLocation:onResult() result = ", tostring(result)) -- local jsonRet = json.decode(result); -- if not jsonRet then -- logD("PluginAndroidLocation:onResult() jsonRet is nil ") -- self:resetLocationInfo() -- self:onGpsInoChanged() -- return -- end -- if not jsonRet.code or jsonRet.code ~= 0 then -- logD("PluginAndroidLocation:onResult() jsonRet.code = ", jsonRet.code) -- self:resetLocationInfo() -- self:onGpsInoChanged() -- return -- end -- local jsonData = json.decode(jsonRet.message or "") -- if not jsonData then -- logD("PluginAndroidLocation:onResult() jsonData is nil ") -- self:resetLocationInfo() -- self:onGpsInoChanged() -- return -- end -- --[[ -- message.put("locType", locType); // 定位结果类型 -- message.put("latitude", latitude); // 纬度 -- message.put("longitude", longitude); // 经度 -- message.put("radius", radius); // 经度 -- message.put("addr", addr); // 地址 -- message.put("desc", desc); // 地址描述 -- --]] -- local locType = jsonData.locType; -- if locType ~= 61 and locType ~= 66 and locType ~= 161 then -- logD("PluginAndroidLocation:onResult() 定位失败 locType = ", locType) -- self:resetLocationInfo() -- self:onGpsInoChanged() -- return -- end -- local latitude = jsonData.latitude -- local longitude = jsonData.longitude -- local addr = jsonData.addr -- local desc = jsonData.desc -- -- 如果本地地位的数据和上一次一样,就没有必要进行转换了 -- if latitude == self.latitude and longitude == self.longitude then -- logD("PluginAndroidLocation:onResult() 定位数据和上次的一样,不需要转换") -- self:onGpsInoChanged() -- return -- end -- -- 转换坐标 -- local baiduApi = "http://api.map.baidu.com/geoconv/v1/"; -- local params = -- { -- coords = tostring(longitude)..","..tostring(latitude), -- from = 5, -- to = 6, -- ak = "w7567NEaAFO4f5QDf6KphFVdeNUmAlWK", -- mcode = "7F:3E:A9:0C:CC:03:87:9B:26:DF:CC:BD:E6:A4:DD:C2:76:F6:2A:76;com.dingdinggame.huanle" -- } -- httpPost(baiduApi, params, function(status, response) -- --[[ -- {"status":0,"result":[{"x":113.8627303666,"y":22.580960414847}]} -- ]] -- logD("PluginAndroidLocation:onHttpResponse() status = ", tostring(status), tostring(response)) -- if status ~= "successed" then -- logD("PluginAndroidLocation:onHttpResponse() status = ", status) -- self:onGpsInoChanged() -- return -- end -- local ttRet = json.decode(response) -- logD("PluginAndroidLocation:onHttpResponse() ttRet = ", table.tostring(ttRet)) -- if tonumber(ttRet.status) ~= 0 then -- logD("PluginAndroidLocation:onHttpResponse() 转换米制坐标失败") -- self:onGpsInoChanged() -- return -- end -- local x = ttRet.result[1].x; -- local y = ttRet.result[1].y; -- -- 转换成功之后记录数据 -- self.x = x; -- self.y = y; -- self.addr = addr -- self.desc = desc -- self.latitude = latitude -- self.longitude = longitude -- -- 记录更新成功的时间 -- self.timeLastUpdate = os.time() -- -- 发送事件 -- self:onGpsInoChanged() -- end) -- end function PluginAndroidLocation:onResult(result) logD("PluginAndroidLocation:onResult() result = ", tostring(result)) local jsonRet = json.decode(result); if not jsonRet then logD("PluginAndroidLocation:onResult() jsonRet is nil ") self:resetLocationInfo() self:onGpsInoChanged() return end if not jsonRet.code or jsonRet.code ~= 0 then logD("PluginAndroidLocation:onResult() jsonRet.code = ", jsonRet.code) self:resetLocationInfo() self:onGpsInoChanged() return end local jsonData = json.decode(jsonRet.message or "") if not jsonData then logD("PluginAndroidLocation:onResult() jsonData is nil ") self:resetLocationInfo() self:onGpsInoChanged() return end --[[ message.put("locType", locType); // 定位结果类型 message.put("latitude", latitude); // 纬度 message.put("longitude", longitude); // 经度 message.put("radius", radius); // 经度 message.put("addr", addr); // 地址 message.put("desc", desc); // 地址描述 --]] local locType = jsonData.locType; if locType ~= 0 then--61 and locType ~= 66 and locType ~= 161 then logD("PluginAndroidLocation:onResult() 定位失败 locType = ", locType) self:resetLocationInfo() if locType == 12 then self.gpsStatus = GpsStatus.disEnable else self.gpsStatus = GpsStatus.close end self:onGpsInoChanged() return end -- 记录更新成功的时间 self.timeLastUpdate = os.time() local latitude = jsonData.latitude local longitude = jsonData.longitude local addr = jsonData.addr local desc = jsonData.desc -- 如果本地地位的数据和上一次一样,就没有必要进行转换了 if latitude == self.latitude and longitude == self.longitude then logD("PluginAndroidLocation:onResult() 定位数据和上次的一样,不需要转换") self:onGpsInoChanged() return end self.x = latitude; self.y = longitude; self.addr = addr self.desc = desc self.latitude = latitude self.longitude = longitude -- 发送事件 self:onGpsInoChanged() -- 转换坐标 -- local baiduApi = "http://api.map.baidu.com/geoconv/v1/"; -- local params = -- { -- coords = tostring(longitude)..","..tostring(latitude), -- from = 5, -- to = 6, -- ak = "0ucQuyttiZfCGqwHjtj87kLwkcUI81Fz", -- mcode = "D4:63:2B:28:DE:42:63:2E:99:6D:6D:5E:23:C3:51:C0:BB:50:AA:C4;com.dingdinggame.bohu" -- } -- httpPost(baiduApi, params, function(status, response) -- --[[ -- {"status":0,"result":[{"x":113.8627303666,"y":22.580960414847}]} -- ]] -- logD("PluginAndroidLocation:onHttpResponse() status = ", tostring(status), tostring(response)) -- if status ~= "successed" then -- logD("PluginAndroidLocation:onHttpResponse() status = ", status) -- self:onGpsInoChanged() -- return -- end -- local ttRet = json.decode(response) -- logD("PluginAndroidLocation:onHttpResponse() ttRet = ", table.tostring(ttRet)) -- if tonumber(ttRet.status) ~= 0 then -- logD("PluginAndroidLocation:onHttpResponse() 转换米制坐标失败") -- self:onGpsInoChanged() -- return -- end -- local x = ttRet.result[1].x; -- local y = ttRet.result[1].y; -- -- 转换成功之后记录数据 -- self.x = x; -- self.y = y; -- self.addr = addr -- self.desc = desc -- self.latitude = latitude -- self.longitude = longitude -- -- 记录更新成功的时间 -- self.timeLastUpdate = os.time() -- -- 发送事件 -- self:onGpsInoChanged() -- end) end -- 发送通知 - 定位完成 function PluginAndroidLocation:onGpsInoChanged() logD("PluginAndroidLocation:onGpsInoChanged()") local ttGpsInfo = self:getLocationInfo() app.user:updateGpsInfo(ttGpsInfo) app:dispatchEvent({name = "onGpsInoChanged"}) end return PluginAndroidLocation