local PluginBase = require("luaScript.Plugins.PluginBase") local PluginIosLocation = class("PluginIosLocation" , PluginBase) function PluginIosLocation:ctor() PluginIosLocation.super.ctor(self); self.x = 0; -- 坐标x(单位米) self.y = 0; -- 坐标y(单位米) self.latitude = 0; -- 维度 self.longitude = 0; -- 经度 self.addr = "" -- 地址 self.desc = "" -- 描述 self.gpsStatus = 0; self.lastGpsStatus = 0; self.timeLastUpdate = 0 self.timeDelta = 600; -- 秒 self.plugin = nil; -- 每次从后台切换回来都获取当期的GPS开启状态 app:addEventListener("applicationWillEnterForeground", handler(self, self.checkLocationEnable)); end --[[ typedef NS_ENUM(int, CLAuthorizationStatus) { // 未处理 kCLAuthorizationStatusNotDetermined = 0, // 受限制的 kCLAuthorizationStatusRestricted = 1, // 已禁止 kCLAuthorizationStatusDenied = 2, // 一直允许 kCLAuthorizationStatusAuthorizedAlways = 3, // 使用时 kCLAuthorizationStatusAuthorizedWhenInUse = 4, // 已经被废弃了 kCLAuthorizationStatusAuthorized = 5 }; --]] function PluginIosLocation:start() if isReviewVersion() then return end self.plugin = cc.PluginManager:getInstance():createPlugin("PluginLocation" , "LocationManager"); self:checkLocationEnable(); end --[[ GpsStatus = { close = 0, --// 未开启 undeal = 1, --// 未处理 enable = 2, --// 允许 disEnable = 3, --// 拒绝的 } --]] function PluginIosLocation:getGpsStatus(gpsEnable, gpsPermission) if gpsEnable then if gpsPermission == 0 then return GpsStatus.undeal elseif gpsPermission == 2 then return GpsStatus.disEnable else return GpsStatus.enable end else return GpsStatus.close end end -- 重置GPS数据 function PluginIosLocation:resetLocationInfo() self.x = 0; self.y = 0; self.latitude = 0; self.longitude = 0 self.gpsStatus = 0; self.lastGpsStatus = 0; end -- 获取GPS信息 function PluginIosLocation:getLocationInfo() local tt = { x = self.x, y = self.y, gpsStatus = self.gpsStatus, addr = self.addr, } logD("PluginIosLocation:getLocationInfo() tt = ", table.tostring(tt)) return tt; end --获取定位状态 function PluginIosLocation:getLocationEnable() logD("PluginIosLocation:getLocationEnable()"); if self.plugin then local locationEnable = self.plugin:callBool("getLocationServicesEnabled"); logD("PluginIosLocation:getLocationEnable() locationEnable = ", locationEnable); return (locationEnable == "true"); end return false end -- 最新的GPS权限状态 function PluginIosLocation:getGpsPermission() logD("PluginIosLocation:getGpsPermission()"); if self.plugin then local gpsPermission = self.plugin:callBool("getAppLocationPermission"); logD("PluginIosLocation:getGpsPermission() gpsPermission = ", gpsPermission); return tonumber(gpsPermission); end return 0 end -- 前往GPS设置界面 function PluginIosLocation:gotoGpsSettingView() logD("PluginIosLocation:gotoGpsSettingView()"); if self.plugin then local params = { title = "开启定位服务", message = "您的定位没有开启,请点击 设置 -> 隐私 -> 定位服务 -> 开启定位服务 -> 设置悠闲麻将欢乐允许访问位置信息", } logD("PluginIosLocation:gotoGpsSettingView()"); self.plugin:callVoid("gotoGpsSettingView", params); end end -- 前往APP权限界面 function PluginIosLocation:gotoAppPermissionsView() logD("PluginIosLocation:gotoAppPermissionsView()"); if self.plugin then logD("PluginIosLocation:gotoAppPermissionsView()"); self.plugin:callVoid("gotoAppPermissionsView"); end end -- 前往打开GPS function PluginIosLocation:openGPS() local locationEnable = self:getLocationEnable() if locationEnable then self:gotoAppPermissionsView() else self:gotoGpsSettingView() end end -- 每次从后台切换回来都获取当期的GPS开启状态 -- 如果本次的状态跟上一次不一样,则更新数据 function PluginIosLocation:checkLocationEnable() logD("PluginIosLocation:checkLocationEnable()"); local gpsEnable = self:getLocationEnable() local gpsPermission = self:getGpsPermission() logD("PluginIosLocation:checkLocationEnable(), gpsEnable = ", gpsEnable); logD("PluginIosLocation:checkLocationEnable(), gpsPermission = ", gpsPermission); self.gpsStatus = self:getGpsStatus(gpsEnable, gpsPermission); logD("PluginIosLocation:checkLocationEnable(), self.gpsStatus = ", self.gpsStatus); logD("PluginIosLocation:checkLocationEnable(), self.lastGpsStatus = ", self.lastGpsStatus); 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 if gpsPermission == 0 then -- 未处理的情况下,还是可以请求一下的 self:updateLocation() else -- 重置数据 self:resetLocationInfo() self:onGpsInoChanged() end end self.lastGpsStatus = self.gpsStatus; end function PluginIosLocation:updateLocation() if self.plugin then local params = { callback = handler(self, self.onResult); -- 回调函数 }; self.plugin:callVoid("start", params); end end function PluginIosLocation:stopLocation() if self.plugin then print("PluginIosLocation:stopLocation()"); self.plugin:callVoid("stop"); end end -- 回调函数 function PluginIosLocation:onResult(result) logD("PluginIosLocation:onResult() ", tostring(result)) local jsonRet = json.decode(result); if not jsonRet then logD("PluginIosLocation:onResult() jsonRet is nil ") self:onGpsInoChanged() return end if not jsonRet.code or tonumber(jsonRet.code) ~= 0 then logD("PluginIosLocation:onResult() jsonRet.code = ", jsonRet.code) self:onGpsInoChanged() return end local jsonData = json.decode(jsonRet.message or "") if not jsonData then logD("PluginIosLocation:onResult() jsonData is nil ") self:onGpsInoChanged() return end --[[ message.put("latitude", latitude); // 纬度 message.put("longitude", longitude); // 经度 message.put("radius", radius); // 经度 --]] 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 self:onGpsInoChanged() return end self.x = latitude; self.y = longitude; self.addr = addr self.desc = desc self.latitude = latitude self.longitude = longitude -- 记录更新成功的时间 self.timeLastUpdate = os.time() -- 发送事件 self:onGpsInoChanged() -- local baiduApi = "http://api.map.baidu.com/geoconv/v1/"; -- local params = -- { -- coords = tostring(longitude)..","..tostring(latitude), -- from = 1, -- 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("PluginIosLocation:onResult() status = ", tostring(status), tostring(response)) -- if status ~= "successed" then -- logD("PluginIosLocation:onResult() status = ", status) -- self:onGpsInoChanged() -- return -- end -- local ttRet = json.decode(response) -- logD("PluginIosLocation:onResult() ttRet = ", table.tostring(ttRet)) -- if tonumber(ttRet.status) ~= 0 then -- logD("PluginIosLocation:onResult() 转换米制坐标失败") -- 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 PluginIosLocation:onGpsInoChanged() logD("PluginIosLocation:onGpsInoChanged()") local ttGpsInfo = self:getLocationInfo() app.user:updateGpsInfo(ttGpsInfo) app:dispatchEvent({name = "onGpsInoChanged"}) end return PluginIosLocation