1
0

dingwei.js 9.7 KB

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