dingwei.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * 定位文件
  3. * 创建人:雨中磐石(rainrock)
  4. */
  5. //jssdk回调过来的
  6. js.jssdkcall = function(bo){
  7. js.dw.start();//开始定位
  8. }
  9. var openfrom = '';
  10. function initApp(){
  11. js.dw.start();
  12. }
  13. js.dw = {
  14. //开始定位
  15. init:function(isgzh){
  16. var dws = navigator.userAgent;
  17. this.ISAPP = dws.indexOf('XINHUOA')>0;
  18. if(dws.indexOf('REIMPLAT')>0 || this.ISAPP)return;
  19. if(openfrom=='nppandroid' || openfrom=='nppios')return;
  20. if(isgzh==1){
  21. js.jssdkwxgzh();
  22. }else{
  23. js.jssdkwixin();
  24. }
  25. },
  26. dwbool:false,
  27. dwtimeer:false,
  28. ondwcall:function(){},
  29. ondwstart:function(){},
  30. ondwerr:function(){},
  31. successbo:false,
  32. ondwwait:function(){return false},
  33. start:function(){
  34. if(this.dwbool)return;
  35. this.successbo = false;
  36. this.chaoshi();
  37. this.ondwstart(js.jssdkstate);
  38. if(js.jssdkstate != 1 || this.ISAPP){
  39. this.htmldingw(0);
  40. }else{
  41. this.wxdingw();
  42. }
  43. },
  44. //定位等待
  45. wait:function(msg){
  46. var bo = this.ondwwait(msg);
  47. if(!bo)js.msg('wait',msg);
  48. },
  49. chaoshi:function(){
  50. clearTimeout(this.dwtimeer);
  51. this.dwtimeer = setTimeout(function(){
  52. var msg = '定位超时,请重新定位';
  53. js.msg('msg', msg);
  54. js.dw.ondwerr(msg);
  55. js.jssdkstate = 2;
  56. js.dw.dwbool=false;
  57. },20*1000);
  58. },
  59. clearchao:function(){
  60. clearTimeout(this.dwtimeer);
  61. this.dwbool = false;
  62. },
  63. //html5定位
  64. htmldingw:function(lx){
  65. var msg;
  66. if(appobj1('startLocation','appbacklocation')){
  67. this.wait('原生app定位中...');
  68. this.dwbool = true;
  69. return;
  70. }
  71. if(window['api'] && api.startLocation){
  72. js.msg();
  73. this.dwbool = true;
  74. if(api.systemType=='ios'){
  75. this.wait(''+api.systemType+'APP定位中...');
  76. api.startLocation({},function(ret,err){
  77. js.dw.appLocationSuc(ret,err);
  78. });
  79. return;
  80. }else if(lx==0){ //这个是旧版的app里弃用了
  81. this.wait(''+api.systemType+'百度地图定位中...');
  82. if(!this.baiduLocation)this.baiduLocation = api.require('baiduLocation');
  83. if(this.baiduLocation){
  84. this.baiduLocation.startLocation({
  85. autoStop: false
  86. }, function(ret, err) {
  87. js.dw.baiduLocationSuc(ret,err);
  88. });
  89. }else{
  90. if(!this.bmLocation)this.bmLocation = api.require('bmLocation');
  91. if(this.bmLocation){
  92. this.bmLocation.configManager({
  93. coordinateType:'BMK09LL',accuracy:'hight_accuracy'
  94. });
  95. this.bmLocation.singleLocation({reGeocode:false},function(ret,err){
  96. var dtes = {};
  97. dtes.status = ret.status;
  98. if(ret.status){
  99. dtes.longitude = ret.location.longitude;
  100. dtes.latitude = ret.location.latitude;
  101. }
  102. js.dw.baiduLocationSuc(dtes,err);
  103. js.dw.bmLocation.stopLocation();
  104. });
  105. }
  106. }
  107. return;
  108. }
  109. }
  110. if(this.ISAPP)return;
  111. if(!navigator.geolocation){
  112. msg = '不支持浏览器定位';
  113. js.msg('msg',msg);
  114. this.clearchao();
  115. js.dw.ondwerr(msg);
  116. }else{
  117. this.liulqdw();
  118. }
  119. },
  120. liulqdw:function(){
  121. this.wait('浏览器定位中...');
  122. var dwrand = sessionStorage.getItem('dwrand');
  123. if(dwrand){
  124. sessionStorage.setItem('dwrand', '');
  125. this.wait('跳转定位获取中...');
  126. $.ajax({
  127. url:'api.php?m=kaoqin&a=dwget&dwrand='+dwrand+'',
  128. dataType:'json',
  129. success:function(ret){
  130. if(ret.success && ret.data){
  131. js.dw.showPosition({coords:ret.data});
  132. }else{
  133. js.dw.showcuowu('跳转定位错误:'+ret.msg);
  134. }
  135. },
  136. error:function(){
  137. js.dw.showcuowu('跳转定位错误');
  138. }
  139. });
  140. return;
  141. }
  142. if(HOST=='127.0.0.1'){this.showPosition({coords:{latitude:24.51036967,longitude:118.178837299,accuracy:100}});return;}//本地虚拟定位
  143. navigator.geolocation.getCurrentPosition(this.showPosition,this.showError,{
  144. enableHighAccuracy: true,
  145. timeout: 10000,
  146. maximumAge: 3000
  147. });
  148. },
  149. //微信定位
  150. wxdingw:function(){
  151. var msg = '微信定位中...';
  152. if(js.isqywx)msg='企业微信定位中...';
  153. this.wait(msg);
  154. wx.getLocation({
  155. type: 'gcj02',
  156. success: function (res,err){
  157. js.dw.dwsuccess(res,err);
  158. },
  159. error:function(){
  160. js.jssdkstate = 2;
  161. js.dw.dwbool=false;
  162. js.dw.start();
  163. }
  164. });
  165. },
  166. appLocationSuc:function(ret,err){
  167. if(ret.status){
  168. if(!ret.accuracy)ret.accuracy = 200;
  169. this.dwsuccess(ret);
  170. }else{
  171. this.dwshibai(err.msg);
  172. }
  173. },
  174. baiduLocationSuc:function(ret,err){
  175. if(ret.status && ret.latitude){
  176. this.wait('百度定位成功,获取位置信息...');
  177. if(!ret.accuracy)ret.accuracy = 200;
  178. this.translate(ret.latitude, ret.longitude, ret.accuracy, 3);
  179. }else{
  180. this.dwshibai('定位失败,检查是否给APP开定位权限');
  181. }
  182. },
  183. dwshibai:function(msg){
  184. this.clearchao();
  185. js.setmsg('');
  186. js.msg('msg', msg);
  187. this.ondwerr(msg);
  188. },
  189. dwsuccess:function(res){
  190. this.wait('定位成功,获取位置信息...');
  191. this.clearchao();
  192. var lat = parseFloat(res.latitude); // 纬度,浮点数,范围为90 ~ -90
  193. var lng = parseFloat(res.longitude); // 经度,浮点数,范围为180 ~ -180。
  194. var jid = parseFloat(res.accuracy); // 位置精度
  195. var address = res.address;
  196. if(address){
  197. js.msg('none');
  198. res.addressinfo = address+'(精确'+js.float(jid,1)+'米)';
  199. res.latitude = lat;
  200. res.longitude = lng;
  201. res.accuracy = jid;
  202. this.ondwcall(res);
  203. }else{
  204. this.geocoder(lat,lng, jid);
  205. }
  206. },
  207. showError:function (error){
  208. js.dw.clearchao();
  209. js.setmsg('');
  210. var msg='无法定位';
  211. switch(error.code){
  212. case error.PERMISSION_DENIED:
  213. msg="用户拒绝对获取地理位置的请求。"
  214. break;
  215. case error.POSITION_UNAVAILABLE:
  216. msg="位置信息是不可用的。"
  217. break;
  218. case error.TIMEOUT:
  219. msg="请求用户地理位置超时。"
  220. break;
  221. case error.UNKNOWN_ERROR:
  222. msg="未知错误。"
  223. break;
  224. }
  225. clearTimeout(js.dw.timeerrbo)
  226. if(NOWURL.substr(0,5)!='https')msg+='必须使用https访问';
  227. js.dw.timeerrbo = setTimeout(function(){
  228. if(!js.dw.successbo){
  229. js.dw.showErrorss(msg);
  230. }else{
  231. js.msg();
  232. }
  233. },1000);
  234. },
  235. showcuowu:function(msg){
  236. js.msg('msg', msg);
  237. js.dw.ondwerr(msg);
  238. },
  239. showErrorss:function(msg){
  240. js.confirm('点确定继续去定位('+msg+')', function(jg){
  241. if(jg=='yes'){
  242. setTimeout('js.dw.gotodingw()',100);
  243. }else{
  244. js.dw.showcuowu(msg);
  245. }
  246. });
  247. },
  248. gotodingw:function(){
  249. js.alert('跳转在定位中,可点确定读取定位内容。','', function(){
  250. js.reload();
  251. });
  252. var dwrand = js.getrand();
  253. $.ajax({
  254. url:'api.php?m=kaoqin&a=dwurl&dwrand='+dwrand+'',
  255. dataType:'json',
  256. success:function(ret){
  257. var da = ret.data;
  258. sessionStorage.setItem('dwrand', da.dwrand);
  259. js.location(da.url);
  260. }
  261. });
  262. },
  263. showPosition:function(position){
  264. js.dw.successbo = true;
  265. js.tanclose('confirm');
  266. clearTimeout(js.dw.timeerrbo);
  267. js.msg();
  268. var res = position.coords;
  269. var latitude = res.latitude;
  270. var longitude = res.longitude;
  271. var accuracy = parseFloat(res.accuracy);
  272. js.dw.translate(latitude,longitude, accuracy, 1);
  273. },
  274. //坐标转化type1原始
  275. translate:function(lat, lng,juli, type){
  276. $.ajax({
  277. url:'api.php?m=kaoqin&a=translate',
  278. data:{
  279. lat:lat,
  280. lng:lng,
  281. type:type
  282. },
  283. dataType:'json',
  284. success:function(ret){
  285. if(ret.status==0){
  286. js.dw.dwsuccess({
  287. latitude:ret.locations[0].lat,
  288. longitude:ret.locations[0].lng,
  289. accuracy:juli
  290. });
  291. }else{
  292. js.dw.dwshibai('无法转化坐标('+lat+','+lng+'),'+type+'<br>'+ret.status+','+ret.message+'');
  293. }
  294. },
  295. error:function(){
  296. js.dw.dwshibai('无法转化坐标'+type+'');
  297. }
  298. });
  299. },
  300. //搜索位置,2024-07-19改
  301. geocoder:function(lat,lng, jid){
  302. var errcan = {
  303. latitude:lat,
  304. longitude:lng,
  305. accuracy:jid,
  306. address:'未知位置',
  307. addressinfo:'定位成功未知位置',
  308. detail:'未知位置'
  309. }
  310. $.ajax({
  311. url:'api.php?m=kaoqin&a=gcoder',
  312. data:{
  313. lat:lat,
  314. lng:lng,
  315. },
  316. dataType:'json',
  317. success:function(ret){
  318. if(ret.status==0 && ret.result){
  319. var result = ret.result,addressinfo;
  320. var address= result.formatted_addresses.recommend;
  321. if(!address)address = result.address;
  322. addressinfo = ''+address;
  323. if(jid>0)addressinfo+='(精确'+js.float(jid,1)+'米)';
  324. js.msg();
  325. errcan.address = address;
  326. errcan.addressinfo = addressinfo;
  327. errcan.detail = result;
  328. js.dw.ondwcall(errcan);
  329. }else{
  330. if(ret.message)js.msg('msg', ret.status+':'+ret.message);
  331. js.dw.ondwcall(errcan);
  332. }
  333. },
  334. error:function(){
  335. js.dw.ondwcall(errcan);
  336. }
  337. });
  338. },
  339. //计算距离,old
  340. matrix:function(lat,lng, kqarr, funs){
  341. var fromstr = ''+lat+','+lng+'',tostr='';
  342. for(var i=0;i<kqarr.length;i++){
  343. if(i>0)tostr+=';';
  344. tostr +=''+kqarr[i].location_x+','+kqarr[i].location_y+'';
  345. }
  346. if(fromstr && tostr){
  347. $.ajax({
  348. url:'api.php?m=kaoqin&a=matrix',
  349. data:{
  350. fromstr:fromstr,
  351. tostr:tostr,
  352. },
  353. dataType:'json',
  354. success:function(ret){
  355. if(ret.status==0){
  356. var rows = ret.result.rows[0].elements;
  357. for(var j=0;j<rows.length;j++)kqarr[j].kqjuli = rows[j].distance;
  358. funs(kqarr);
  359. }else{
  360. alert('计算距离('+ret.status+'):'+ret.message);
  361. funs(kqarr);
  362. }
  363. },
  364. error:function(e){
  365. alert('接口出错无法计算距离');
  366. funs(kqarr);
  367. }
  368. });
  369. }else{
  370. funs(kqarr);
  371. }
  372. },
  373. //计算距离
  374. julisuan:function(lat,lng, kqarr, funs){
  375. var startPoint = new TMap.LatLng(lat, lng);
  376. for(var i=0;i<kqarr.length;i++){
  377. var path = [startPoint , new TMap.LatLng(parseFloat(kqarr[i].location_x), parseFloat(kqarr[i].location_y))];
  378. var distance = TMap.geometry.computeDistance(path);
  379. kqarr[i].kqjuli = parseFloat(distance);
  380. }
  381. funs(kqarr);
  382. }
  383. };
  384. //原生app定位中
  385. appbacklocation=function(res){
  386. var latitude = res.latitude;
  387. var longitude = res.longitude;
  388. var accuracy = parseFloat(res.accuracy);
  389. js.dw.dwsuccess({
  390. latitude:latitude,
  391. longitude:longitude,
  392. accuracy:accuracy,
  393. address:res.address
  394. });
  395. }
粤ICP备19079148号