You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 line
8.3 KiB

  1. local PluginBase = require("luaScript.Plugins.PluginBase")
  2. local PluginIosLocation = class("PluginIosLocation" , PluginBase)
  3. function PluginIosLocation:ctor()
  4. PluginIosLocation.super.ctor(self);
  5. self.x = 0; -- 坐标x(单位米)
  6. self.y = 0; -- 坐标y(单位米)
  7. self.latitude = 0; -- 维度
  8. self.longitude = 0; -- 经度
  9. self.addr = "" -- 地址
  10. self.desc = "" -- 描述
  11. self.gpsStatus = 0;
  12. self.lastGpsStatus = 0;
  13. self.timeLastUpdate = 0
  14. self.timeDelta = 600; -- 秒
  15. self.plugin = nil;
  16. -- 每次从后台切换回来都获取当期的GPS开启状态
  17. app:addEventListener("applicationWillEnterForeground", handler(self, self.checkLocationEnable));
  18. end
  19. --[[
  20. typedef NS_ENUM(int, CLAuthorizationStatus) {
  21. // 未处理
  22. kCLAuthorizationStatusNotDetermined = 0,
  23. // 受限制的
  24. kCLAuthorizationStatusRestricted = 1,
  25. // 已禁止
  26. kCLAuthorizationStatusDenied = 2,
  27. // 一直允许
  28. kCLAuthorizationStatusAuthorizedAlways = 3,
  29. // 使用时
  30. kCLAuthorizationStatusAuthorizedWhenInUse = 4,
  31. // 已经被废弃了
  32. kCLAuthorizationStatusAuthorized = 5
  33. };
  34. --]]
  35. function PluginIosLocation:start()
  36. if isReviewVersion() then
  37. return
  38. end
  39. self.plugin = cc.PluginManager:getInstance():createPlugin("PluginLocation" , "LocationManager");
  40. self:checkLocationEnable();
  41. end
  42. --[[
  43. GpsStatus =
  44. {
  45. close = 0, --// 未开启
  46. undeal = 1, --// 未处理
  47. enable = 2, --// 允许
  48. disEnable = 3, --// 拒绝的
  49. }
  50. --]]
  51. function PluginIosLocation:getGpsStatus(gpsEnable, gpsPermission)
  52. if gpsEnable then
  53. if gpsPermission == 0 then
  54. return GpsStatus.undeal
  55. elseif gpsPermission == 2 then
  56. return GpsStatus.disEnable
  57. else
  58. return GpsStatus.enable
  59. end
  60. else
  61. return GpsStatus.close
  62. end
  63. end
  64. -- 重置GPS数据
  65. function PluginIosLocation:resetLocationInfo()
  66. self.x = 0;
  67. self.y = 0;
  68. self.latitude = 0;
  69. self.longitude = 0
  70. self.gpsStatus = 0;
  71. self.lastGpsStatus = 0;
  72. end
  73. -- 获取GPS信息
  74. function PluginIosLocation:getLocationInfo()
  75. local tt =
  76. {
  77. x = self.x,
  78. y = self.y,
  79. gpsStatus = self.gpsStatus,
  80. addr = self.addr,
  81. }
  82. logD("PluginIosLocation:getLocationInfo() tt = ", table.tostring(tt))
  83. return tt;
  84. end
  85. --获取定位状态
  86. function PluginIosLocation:getLocationEnable()
  87. logD("PluginIosLocation:getLocationEnable()");
  88. if self.plugin then
  89. local locationEnable = self.plugin:callBool("getLocationServicesEnabled");
  90. logD("PluginIosLocation:getLocationEnable() locationEnable = ", locationEnable);
  91. return (locationEnable == "true");
  92. end
  93. return false
  94. end
  95. -- 最新的GPS权限状态
  96. function PluginIosLocation:getGpsPermission()
  97. logD("PluginIosLocation:getGpsPermission()");
  98. if self.plugin then
  99. local gpsPermission = self.plugin:callBool("getAppLocationPermission");
  100. logD("PluginIosLocation:getGpsPermission() gpsPermission = ", gpsPermission);
  101. return tonumber(gpsPermission);
  102. end
  103. return 0
  104. end
  105. -- 前往GPS设置界面
  106. function PluginIosLocation:gotoGpsSettingView()
  107. logD("PluginIosLocation:gotoGpsSettingView()");
  108. if self.plugin then
  109. local params =
  110. {
  111. title = "开启定位服务",
  112. message = "您的定位没有开启,请点击 设置 -> 隐私 -> 定位服务 -> 开启定位服务 -> 设置悠闲麻将欢乐允许访问位置信息",
  113. }
  114. logD("PluginIosLocation:gotoGpsSettingView()");
  115. self.plugin:callVoid("gotoGpsSettingView", params);
  116. end
  117. end
  118. -- 前往APP权限界面
  119. function PluginIosLocation:gotoAppPermissionsView()
  120. logD("PluginIosLocation:gotoAppPermissionsView()");
  121. if self.plugin then
  122. logD("PluginIosLocation:gotoAppPermissionsView()");
  123. self.plugin:callVoid("gotoAppPermissionsView");
  124. end
  125. end
  126. -- 前往打开GPS
  127. function PluginIosLocation:openGPS()
  128. local locationEnable = self:getLocationEnable()
  129. if locationEnable then
  130. self:gotoAppPermissionsView()
  131. else
  132. self:gotoGpsSettingView()
  133. end
  134. end
  135. -- 每次从后台切换回来都获取当期的GPS开启状态
  136. -- 如果本次的状态跟上一次不一样,则更新数据
  137. function PluginIosLocation:checkLocationEnable()
  138. logD("PluginIosLocation:checkLocationEnable()");
  139. local gpsEnable = self:getLocationEnable()
  140. local gpsPermission = self:getGpsPermission()
  141. logD("PluginIosLocation:checkLocationEnable(), gpsEnable = ", gpsEnable);
  142. logD("PluginIosLocation:checkLocationEnable(), gpsPermission = ", gpsPermission);
  143. self.gpsStatus = self:getGpsStatus(gpsEnable, gpsPermission);
  144. logD("PluginIosLocation:checkLocationEnable(), self.gpsStatus = ", self.gpsStatus);
  145. logD("PluginIosLocation:checkLocationEnable(), self.lastGpsStatus = ", self.lastGpsStatus);
  146. if self.gpsStatus == GpsStatus.enable then
  147. -- 当前是开启的
  148. if self.lastGpsStatus ~= self.gpsStatus then
  149. self:updateLocation()
  150. else
  151. local timeNow = os.time()
  152. if timeNow - self.timeLastUpdate > self.timeDelta then
  153. self:updateLocation()
  154. end
  155. end
  156. else
  157. if gpsPermission == 0 then
  158. -- 未处理的情况下,还是可以请求一下的
  159. self:updateLocation()
  160. else
  161. -- 重置数据
  162. self:resetLocationInfo()
  163. self:onGpsInoChanged()
  164. end
  165. end
  166. self.lastGpsStatus = self.gpsStatus;
  167. end
  168. function PluginIosLocation:updateLocation()
  169. if self.plugin then
  170. local params =
  171. {
  172. callback = handler(self, self.onResult); -- 回调函数
  173. };
  174. self.plugin:callVoid("start", params);
  175. end
  176. end
  177. function PluginIosLocation:stopLocation()
  178. if self.plugin then
  179. print("PluginIosLocation:stopLocation()");
  180. self.plugin:callVoid("stop");
  181. end
  182. end
  183. -- 回调函数
  184. function PluginIosLocation:onResult(result)
  185. logD("PluginIosLocation:onResult() ", tostring(result))
  186. local jsonRet = json.decode(result);
  187. if not jsonRet then
  188. logD("PluginIosLocation:onResult() jsonRet is nil ")
  189. self:onGpsInoChanged()
  190. return
  191. end
  192. if not jsonRet.code or tonumber(jsonRet.code) ~= 0 then
  193. logD("PluginIosLocation:onResult() jsonRet.code = ", jsonRet.code)
  194. self:onGpsInoChanged()
  195. return
  196. end
  197. local jsonData = json.decode(jsonRet.message or "")
  198. if not jsonData then
  199. logD("PluginIosLocation:onResult() jsonData is nil ")
  200. self:onGpsInoChanged()
  201. return
  202. end
  203. --[[
  204. message.put("latitude", latitude); // 纬度
  205. message.put("longitude", longitude); // 经度
  206. message.put("radius", radius); // 经度
  207. --]]
  208. local latitude = jsonData.latitude
  209. local longitude = jsonData.longitude
  210. local addr = jsonData.addr
  211. local desc = jsonData.desc
  212. -- 如果本地地位的数据和上一次一样,就没有必要进行转换了
  213. if latitude == self.latitude and longitude == self.longitude then
  214. self:onGpsInoChanged()
  215. return
  216. end
  217. self.x = latitude;
  218. self.y = longitude;
  219. self.addr = addr
  220. self.desc = desc
  221. self.latitude = latitude
  222. self.longitude = longitude
  223. -- 记录更新成功的时间
  224. self.timeLastUpdate = os.time()
  225. -- 发送事件
  226. self:onGpsInoChanged()
  227. -- local baiduApi = "http://api.map.baidu.com/geoconv/v1/";
  228. -- local params =
  229. -- {
  230. -- coords = tostring(longitude)..","..tostring(latitude),
  231. -- from = 1,
  232. -- to = 6,
  233. -- ak = "w7567NEaAFO4f5QDf6KphFVdeNUmAlWK",
  234. -- mcode = "7F:3E:A9:0C:CC:03:87:9B:26:DF:CC:BD:E6:A4:DD:C2:76:F6:2A:76;com.dingdinggame.huanle"
  235. -- }
  236. -- httpPost(baiduApi, params, function(status, response)
  237. -- --[[
  238. -- {"status":0,"result":[{"x":113.8627303666,"y":22.580960414847}]}
  239. -- ]]
  240. -- logD("PluginIosLocation:onResult() status = ", tostring(status), tostring(response))
  241. -- if status ~= "successed" then
  242. -- logD("PluginIosLocation:onResult() status = ", status)
  243. -- self:onGpsInoChanged()
  244. -- return
  245. -- end
  246. -- local ttRet = json.decode(response)
  247. -- logD("PluginIosLocation:onResult() ttRet = ", table.tostring(ttRet))
  248. -- if tonumber(ttRet.status) ~= 0 then
  249. -- logD("PluginIosLocation:onResult() 转换米制坐标失败")
  250. -- self:onGpsInoChanged()
  251. -- return
  252. -- end
  253. -- local x = ttRet.result[1].x;
  254. -- local y = ttRet.result[1].y;
  255. -- -- 转换成功之后记录数据
  256. -- self.x = x;
  257. -- self.y = y;
  258. -- self.addr = addr
  259. -- self.desc = desc
  260. -- self.latitude = latitude
  261. -- self.longitude = longitude
  262. -- -- 记录更新成功的时间
  263. -- self.timeLastUpdate = os.time()
  264. -- -- 发送事件
  265. -- self:onGpsInoChanged()
  266. -- end)
  267. end
  268. -- 发送通知 - 定位成功
  269. function PluginIosLocation:onGpsInoChanged()
  270. logD("PluginIosLocation:onGpsInoChanged()")
  271. local ttGpsInfo = self:getLocationInfo()
  272. app.user:updateGpsInfo(ttGpsInfo)
  273. app:dispatchEvent({name = "onGpsInoChanged"})
  274. end
  275. return PluginIosLocation