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.

416 lines
12 KiB

  1. local PluginBase = require("luaScript.Plugins.PluginBase")
  2. --[[
  3. 创建插件时更新一次定位信息
  4. 每次从后台切换回来,检查GPS开启状态,如果跟之前的状态不一样,则重新获取定位信息
  5. --]]
  6. -- 获取玩家定位信息
  7. local PluginAndroidLocation = class("PluginAndroidLocation" , PluginBase)
  8. function PluginAndroidLocation:ctor()
  9. PluginAndroidLocation.super.ctor(self);
  10. self.x = 0; -- 坐标x(单位米)
  11. self.y = 0; -- 坐标y(单位米)
  12. self.latitude = 0; -- 维度
  13. self.longitude = 0; -- 经度
  14. self.addr = "" -- 地址
  15. self.desc = "" -- 描述
  16. self.gpsStatus = 0; -- 最新的GPS开启状态
  17. self.lastGpsStatus = 0; -- 上一次记录的GPS开启状态
  18. self.timeLastUpdate = 0
  19. self.timeDelta = 600; -- 秒
  20. self.plugin = nil;
  21. -- 每次从后台切换回来都获取当期的GPS开启状态
  22. app:addEventListener("applicationWillEnterForeground", handler(self, self.checkLocationEnable));
  23. end
  24. function PluginAndroidLocation:start()
  25. self.plugin = cc.PluginManager:getInstance():createPlugin("PluginLocation" , "com/ddgame/plugin/LocationManager");
  26. self:checkLocationEnable();
  27. end
  28. --[[
  29. GpsStatus =
  30. {
  31. close = 0, --// 未开启
  32. undeal = 1, --// 未处理
  33. enable = 2, --// 允许
  34. disEnable = 3, --// 拒绝的
  35. }
  36. --]]
  37. function PluginAndroidLocation:getGpsStatus(gpsEnable)
  38. if gpsEnable then
  39. -- if self:isHavePermission() then
  40. return 2
  41. -- else
  42. -- return 1
  43. -- end
  44. else
  45. return 0
  46. end
  47. end
  48. -- 重置GPS数据
  49. function PluginAndroidLocation:resetLocationInfo()
  50. self.x = 0;
  51. self.y = 0;
  52. self.latitude = 0;
  53. self.longitude = 0
  54. self.gpsStatus = GpsStatus.close
  55. self.lastGpsStatus = GpsStatus.close
  56. end
  57. -- 获取GPS信息
  58. function PluginAndroidLocation:getLocationInfo()
  59. local tt =
  60. {
  61. x = self.x,
  62. y = self.y,
  63. gpsStatus = self.gpsStatus,
  64. addr = self.addr,
  65. }
  66. logD("PluginAndroidLocation:getLocationInfo() tt = ", table.tostring(tt))
  67. return tt;
  68. end
  69. --获取定位服务开启状态
  70. function PluginAndroidLocation:getLocationEnable()
  71. logD("PluginAndroidLocation:getLocationEnable()");
  72. if self.plugin then
  73. local locationEnable = self.plugin:callBool("getLocationServicesEnabled");
  74. logD("PluginAndroidLocation:getLocationEnable() locationEnable = ", locationEnable);
  75. return locationEnable;
  76. end
  77. return false
  78. end
  79. --是否开启定位权限
  80. function PluginAndroidLocation:isHavePermission()
  81. if self.plugin then
  82. local locationEnable = self.plugin:callBool("isHavePermission");
  83. logD("PluginAndroidLocation:isHavePermission = ", locationEnable);
  84. return locationEnable;
  85. end
  86. return false
  87. end
  88. -- 前往GPS设置界面
  89. function PluginAndroidLocation:gotoGpsSettingView()
  90. logD("PluginAndroidLocation:gotoGpsSettingView()");
  91. if self.plugin then
  92. logD("PluginAndroidLocation:gotoGpsSettingView()");
  93. local state = self.plugin:callVoid("gotoGpsSettingView");
  94. end
  95. end
  96. -- 前往APP权限界面
  97. function PluginAndroidLocation:gotoAppPermissionsView()
  98. logD("PluginAndroidLocation:gotoAppPermissionsView()");
  99. if self.plugin then
  100. logD("PluginAndroidLocation:gotoAppPermissionsView()");
  101. local state = self.plugin:callVoid("gotoAppPermissionsView");
  102. end
  103. end
  104. -- 前往打开GPS
  105. function PluginAndroidLocation:openGPS()
  106. local locationEnable = self:getLocationEnable()
  107. if locationEnable then
  108. self:gotoAppPermissionsView()
  109. else
  110. self:gotoGpsSettingView()
  111. end
  112. end
  113. -- 每次从后台切换回来都获取当期的GPS开启状态
  114. -- 如果本次的状态跟上一次不一样,则更新数据
  115. function PluginAndroidLocation:checkLocationEnable()
  116. logD("PluginAndroidLocation:checkLocationEnable()");
  117. local gpsEnable = self:getLocationEnable()
  118. logD("PluginAndroidLocation:checkLocationEnable(), gpsEnable = ", gpsEnable)
  119. self.gpsStatus = self:getGpsStatus(gpsEnable)
  120. logD("PluginAndroidLocation:checkLocationEnable(), self.gpsStatus = ", self.gpsStatus)
  121. -- logD("PluginAndroidLocation:checkLocationEnable(), self.lastGpsStatus = ", self.lastGpsStatus);
  122. -- self:updateLocation()
  123. if self.gpsStatus == GpsStatus.enable then
  124. -- 当前是开启的
  125. if self.lastGpsStatus ~= self.gpsStatus then
  126. self:updateLocation()
  127. else
  128. local timeNow = os.time()
  129. if timeNow - self.timeLastUpdate > self.timeDelta then
  130. self:updateLocation()
  131. end
  132. end
  133. else
  134. -- 重置数据
  135. self:resetLocationInfo()
  136. self:onGpsInoChanged()
  137. end
  138. self.lastGpsStatus = self.gpsStatus
  139. end
  140. -- 更新 GPS 位置信息
  141. function PluginAndroidLocation:updateLocation()
  142. -- local timeNow = os.time()
  143. -- if timeNow - self.timeLastUpdate < self.timeDelta then
  144. -- return
  145. -- end
  146. logD("PluginAndroidLocation:updateLocation()");
  147. if self.plugin then
  148. local params =
  149. {
  150. callback = handler(self, self.onResult); -- 回调函数
  151. };
  152. self.plugin:callVoid("start", params);
  153. end
  154. end
  155. -- 回调函数
  156. -- function PluginAndroidLocation:onResult(result)
  157. -- logD("PluginAndroidLocation:onResult() result = ", tostring(result))
  158. -- local jsonRet = json.decode(result);
  159. -- if not jsonRet then
  160. -- logD("PluginAndroidLocation:onResult() jsonRet is nil ")
  161. -- self:resetLocationInfo()
  162. -- self:onGpsInoChanged()
  163. -- return
  164. -- end
  165. -- if not jsonRet.code or jsonRet.code ~= 0 then
  166. -- logD("PluginAndroidLocation:onResult() jsonRet.code = ", jsonRet.code)
  167. -- self:resetLocationInfo()
  168. -- self:onGpsInoChanged()
  169. -- return
  170. -- end
  171. -- local jsonData = json.decode(jsonRet.message or "")
  172. -- if not jsonData then
  173. -- logD("PluginAndroidLocation:onResult() jsonData is nil ")
  174. -- self:resetLocationInfo()
  175. -- self:onGpsInoChanged()
  176. -- return
  177. -- end
  178. -- --[[
  179. -- message.put("locType", locType); // 定位结果类型
  180. -- message.put("latitude", latitude); // 纬度
  181. -- message.put("longitude", longitude); // 经度
  182. -- message.put("radius", radius); // 经度
  183. -- message.put("addr", addr); // 地址
  184. -- message.put("desc", desc); // 地址描述
  185. -- --]]
  186. -- local locType = jsonData.locType;
  187. -- if locType ~= 61 and locType ~= 66 and locType ~= 161 then
  188. -- logD("PluginAndroidLocation:onResult() 定位失败 locType = ", locType)
  189. -- self:resetLocationInfo()
  190. -- self:onGpsInoChanged()
  191. -- return
  192. -- end
  193. -- local latitude = jsonData.latitude
  194. -- local longitude = jsonData.longitude
  195. -- local addr = jsonData.addr
  196. -- local desc = jsonData.desc
  197. -- -- 如果本地地位的数据和上一次一样,就没有必要进行转换了
  198. -- if latitude == self.latitude and longitude == self.longitude then
  199. -- logD("PluginAndroidLocation:onResult() 定位数据和上次的一样,不需要转换")
  200. -- self:onGpsInoChanged()
  201. -- return
  202. -- end
  203. -- -- 转换坐标
  204. -- local baiduApi = "http://api.map.baidu.com/geoconv/v1/";
  205. -- local params =
  206. -- {
  207. -- coords = tostring(longitude)..","..tostring(latitude),
  208. -- from = 5,
  209. -- to = 6,
  210. -- ak = "w7567NEaAFO4f5QDf6KphFVdeNUmAlWK",
  211. -- mcode = "7F:3E:A9:0C:CC:03:87:9B:26:DF:CC:BD:E6:A4:DD:C2:76:F6:2A:76;com.dingdinggame.huanle"
  212. -- }
  213. -- httpPost(baiduApi, params, function(status, response)
  214. -- --[[
  215. -- {"status":0,"result":[{"x":113.8627303666,"y":22.580960414847}]}
  216. -- ]]
  217. -- logD("PluginAndroidLocation:onHttpResponse() status = ", tostring(status), tostring(response))
  218. -- if status ~= "successed" then
  219. -- logD("PluginAndroidLocation:onHttpResponse() status = ", status)
  220. -- self:onGpsInoChanged()
  221. -- return
  222. -- end
  223. -- local ttRet = json.decode(response)
  224. -- logD("PluginAndroidLocation:onHttpResponse() ttRet = ", table.tostring(ttRet))
  225. -- if tonumber(ttRet.status) ~= 0 then
  226. -- logD("PluginAndroidLocation:onHttpResponse() 转换米制坐标失败")
  227. -- self:onGpsInoChanged()
  228. -- return
  229. -- end
  230. -- local x = ttRet.result[1].x;
  231. -- local y = ttRet.result[1].y;
  232. -- -- 转换成功之后记录数据
  233. -- self.x = x;
  234. -- self.y = y;
  235. -- self.addr = addr
  236. -- self.desc = desc
  237. -- self.latitude = latitude
  238. -- self.longitude = longitude
  239. -- -- 记录更新成功的时间
  240. -- self.timeLastUpdate = os.time()
  241. -- -- 发送事件
  242. -- self:onGpsInoChanged()
  243. -- end)
  244. -- end
  245. function PluginAndroidLocation:onResult(result)
  246. logD("PluginAndroidLocation:onResult() result = ", tostring(result))
  247. local jsonRet = json.decode(result);
  248. if not jsonRet then
  249. logD("PluginAndroidLocation:onResult() jsonRet is nil ")
  250. self:resetLocationInfo()
  251. self:onGpsInoChanged()
  252. return
  253. end
  254. if not jsonRet.code or jsonRet.code ~= 0 then
  255. logD("PluginAndroidLocation:onResult() jsonRet.code = ", jsonRet.code)
  256. self:resetLocationInfo()
  257. self:onGpsInoChanged()
  258. return
  259. end
  260. local jsonData = json.decode(jsonRet.message or "")
  261. if not jsonData then
  262. logD("PluginAndroidLocation:onResult() jsonData is nil ")
  263. self:resetLocationInfo()
  264. self:onGpsInoChanged()
  265. return
  266. end
  267. --[[
  268. message.put("locType", locType); // 定位结果类型
  269. message.put("latitude", latitude); // 纬度
  270. message.put("longitude", longitude); // 经度
  271. message.put("radius", radius); // 经度
  272. message.put("addr", addr); // 地址
  273. message.put("desc", desc); // 地址描述
  274. --]]
  275. local locType = jsonData.locType;
  276. if locType ~= 0 then--61 and locType ~= 66 and locType ~= 161 then
  277. logD("PluginAndroidLocation:onResult() 定位失败 locType = ", locType)
  278. self:resetLocationInfo()
  279. if locType == 12 then
  280. self.gpsStatus = GpsStatus.disEnable
  281. else
  282. self.gpsStatus = GpsStatus.close
  283. end
  284. self:onGpsInoChanged()
  285. return
  286. end
  287. -- 记录更新成功的时间
  288. self.timeLastUpdate = os.time()
  289. local latitude = jsonData.latitude
  290. local longitude = jsonData.longitude
  291. local addr = jsonData.addr
  292. local desc = jsonData.desc
  293. -- 如果本地地位的数据和上一次一样,就没有必要进行转换了
  294. if latitude == self.latitude and longitude == self.longitude then
  295. logD("PluginAndroidLocation:onResult() 定位数据和上次的一样,不需要转换")
  296. self:onGpsInoChanged()
  297. return
  298. end
  299. self.x = latitude;
  300. self.y = longitude;
  301. self.addr = addr
  302. self.desc = desc
  303. self.latitude = latitude
  304. self.longitude = longitude
  305. -- 发送事件
  306. self:onGpsInoChanged()
  307. -- 转换坐标
  308. -- local baiduApi = "http://api.map.baidu.com/geoconv/v1/";
  309. -- local params =
  310. -- {
  311. -- coords = tostring(longitude)..","..tostring(latitude),
  312. -- from = 5,
  313. -- to = 6,
  314. -- ak = "0ucQuyttiZfCGqwHjtj87kLwkcUI81Fz",
  315. -- mcode = "D4:63:2B:28:DE:42:63:2E:99:6D:6D:5E:23:C3:51:C0:BB:50:AA:C4;com.dingdinggame.bohu"
  316. -- }
  317. -- httpPost(baiduApi, params, function(status, response)
  318. -- --[[
  319. -- {"status":0,"result":[{"x":113.8627303666,"y":22.580960414847}]}
  320. -- ]]
  321. -- logD("PluginAndroidLocation:onHttpResponse() status = ", tostring(status), tostring(response))
  322. -- if status ~= "successed" then
  323. -- logD("PluginAndroidLocation:onHttpResponse() status = ", status)
  324. -- self:onGpsInoChanged()
  325. -- return
  326. -- end
  327. -- local ttRet = json.decode(response)
  328. -- logD("PluginAndroidLocation:onHttpResponse() ttRet = ", table.tostring(ttRet))
  329. -- if tonumber(ttRet.status) ~= 0 then
  330. -- logD("PluginAndroidLocation:onHttpResponse() 转换米制坐标失败")
  331. -- self:onGpsInoChanged()
  332. -- return
  333. -- end
  334. -- local x = ttRet.result[1].x;
  335. -- local y = ttRet.result[1].y;
  336. -- -- 转换成功之后记录数据
  337. -- self.x = x;
  338. -- self.y = y;
  339. -- self.addr = addr
  340. -- self.desc = desc
  341. -- self.latitude = latitude
  342. -- self.longitude = longitude
  343. -- -- 记录更新成功的时间
  344. -- self.timeLastUpdate = os.time()
  345. -- -- 发送事件
  346. -- self:onGpsInoChanged()
  347. -- end)
  348. end
  349. -- 发送通知 - 定位完成
  350. function PluginAndroidLocation:onGpsInoChanged()
  351. logD("PluginAndroidLocation:onGpsInoChanged()")
  352. local ttGpsInfo = self:getLocationInfo()
  353. app.user:updateGpsInfo(ttGpsInfo)
  354. app:dispatchEvent({name = "onGpsInoChanged"})
  355. end
  356. return PluginAndroidLocation