dingwei.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.wait('浏览器定位中...');
  115. //本地虚拟定位
  116. if(HOST=='127.0.0.1'){this.showPosition({coords:{latitude:24.51036967,longitude:118.178837299,accuracy:100}});return;}
  117. navigator.geolocation.getCurrentPosition(this.showPosition,this.showError,{
  118. enableHighAccuracy: true,
  119. timeout: 19000,
  120. maximumAge: 3000
  121. });
  122. }
  123. },
  124. //微信定位
  125. wxdingw:function(){
  126. var msg = '微信定位中...';
  127. if(js.isqywx)msg='企业微信定位中...';
  128. this.wait(msg);
  129. wx.getLocation({
  130. type: 'gcj02',
  131. success: function (res,err){
  132. js.dw.dwsuccess(res,err);
  133. },
  134. error:function(){
  135. js.jssdkstate = 2;
  136. js.dw.dwbool=false;
  137. js.dw.start();
  138. }
  139. });
  140. },
  141. appLocationSuc:function(ret,err){
  142. if(ret.status){
  143. if(!ret.accuracy)ret.accuracy = 200;
  144. this.dwsuccess(ret);
  145. }else{
  146. this.dwshibai(err.msg);
  147. }
  148. },
  149. baiduLocationSuc:function(ret,err){
  150. if(ret.status && ret.latitude){
  151. this.wait('百度定位成功,获取位置信息...');
  152. if(!ret.accuracy)ret.accuracy = 200;
  153. this.translate(ret.latitude, ret.longitude, ret.accuracy, 3);
  154. }else{
  155. this.dwshibai('定位失败,检查是否给APP开定位权限');
  156. }
  157. },
  158. dwshibai:function(msg){
  159. this.clearchao();
  160. js.setmsg('');
  161. js.msg('msg', msg);
  162. this.ondwerr(msg);
  163. },
  164. dwsuccess:function(res){
  165. this.wait('定位成功,获取位置信息...');
  166. this.clearchao();
  167. var lat = parseFloat(res.latitude); // 纬度,浮点数,范围为90 ~ -90
  168. var lng = parseFloat(res.longitude); // 经度,浮点数,范围为180 ~ -180。
  169. var jid = parseFloat(res.accuracy); // 位置精度
  170. this.geocoder(lat,lng, jid);
  171. },
  172. showError:function (error){
  173. js.dw.clearchao();
  174. js.setmsg('');
  175. var msg='无法定位';
  176. switch(error.code){
  177. case error.PERMISSION_DENIED:
  178. msg="用户拒绝对获取地理位置的请求。"
  179. break;
  180. case error.POSITION_UNAVAILABLE:
  181. msg="位置信息是不可用的。"
  182. break;
  183. case error.TIMEOUT:
  184. msg="请求用户地理位置超时。"
  185. break;
  186. case error.UNKNOWN_ERROR:
  187. msg="未知错误。"
  188. break;
  189. }
  190. if(NOWURL.substr(0,5)!='https')msg+='必须使用https访问';
  191. js.dw.timeerrbo = setTimeout(function(){
  192. if(!js.dw.successbo){
  193. js.msg('msg', msg);
  194. js.dw.ondwerr(msg);
  195. }else{
  196. js.msg();
  197. }
  198. },1000);
  199. },
  200. showPosition:function(position){
  201. js.dw.successbo = true;
  202. clearTimeout(js.dw.timeerrbo);
  203. js.msg();
  204. var res = position.coords;
  205. var latitude = res.latitude;
  206. var longitude = res.longitude;
  207. var accuracy = parseFloat(res.accuracy);
  208. js.dw.translate(latitude,longitude, accuracy, 1);
  209. },
  210. //坐标转化type1原始
  211. translate:function(lat, lng,juli, type){
  212. $.ajax({
  213. url:'api.php?m=kaoqin&a=translate',
  214. data:{
  215. lat:lat,
  216. lng:lng,
  217. type:type
  218. },
  219. dataType:'json',
  220. success:function(ret){
  221. if(ret.status==0){
  222. js.dw.dwsuccess({
  223. latitude:ret.locations[0].lat,
  224. longitude:ret.locations[0].lng,
  225. accuracy:juli
  226. });
  227. }else{
  228. js.dw.dwshibai('无法转化坐标('+lat+','+lng+'),'+type+'<br>'+ret.status+','+ret.message+'');
  229. }
  230. },
  231. error:function(){
  232. js.dw.dwshibai('无法转化坐标'+type+'');
  233. }
  234. });
  235. },
  236. //搜索位置,2024-07-19改
  237. geocoder:function(lat,lng, jid){
  238. var errcan = {
  239. latitude:lat,
  240. longitude:lng,
  241. accuracy:jid,
  242. address:'未知位置',
  243. addressinfo:'定位成功未知位置',
  244. detail:'未知位置'
  245. }
  246. $.ajax({
  247. url:'api.php?m=kaoqin&a=gcoder',
  248. data:{
  249. lat:lat,
  250. lng:lng,
  251. },
  252. dataType:'json',
  253. success:function(ret){
  254. if(ret.status==0 && ret.result){
  255. var result = ret.result,addressinfo;
  256. var address= result.formatted_addresses.recommend;
  257. if(!address)address = result.address;
  258. addressinfo = ''+address;
  259. if(jid>0)addressinfo+='(精确'+js.float(jid,1)+'米)';
  260. js.msg();
  261. errcan.address = address;
  262. errcan.addressinfo = addressinfo;
  263. errcan.detail = result;
  264. js.dw.ondwcall(errcan);
  265. }else{
  266. if(ret.message)js.msg('msg', ret.status+':'+ret.message);
  267. js.dw.ondwcall(errcan);
  268. }
  269. },
  270. error:function(){
  271. js.dw.ondwcall(errcan);
  272. }
  273. });
  274. },
  275. //计算距离,old
  276. matrix:function(lat,lng, kqarr, funs){
  277. var fromstr = ''+lat+','+lng+'',tostr='';
  278. for(var i=0;i<kqarr.length;i++){
  279. if(i>0)tostr+=';';
  280. tostr +=''+kqarr[i].location_x+','+kqarr[i].location_y+'';
  281. }
  282. if(fromstr && tostr){
  283. $.ajax({
  284. url:'api.php?m=kaoqin&a=matrix',
  285. data:{
  286. fromstr:fromstr,
  287. tostr:tostr,
  288. },
  289. dataType:'json',
  290. success:function(ret){
  291. if(ret.status==0){
  292. var rows = ret.result.rows[0].elements;
  293. for(var j=0;j<rows.length;j++)kqarr[j].kqjuli = rows[j].distance;
  294. funs(kqarr);
  295. }else{
  296. alert('计算距离('+ret.status+'):'+ret.message);
  297. funs(kqarr);
  298. }
  299. },
  300. error:function(e){
  301. alert('接口出错无法计算距离');
  302. funs(kqarr);
  303. }
  304. });
  305. }else{
  306. funs(kqarr);
  307. }
  308. },
  309. //计算距离
  310. julisuan:function(lat,lng, kqarr, funs){
  311. var startPoint = new TMap.LatLng(lat, lng);
  312. for(var i=0;i<kqarr.length;i++){
  313. var path = [startPoint , new TMap.LatLng(parseFloat(kqarr[i].location_x), parseFloat(kqarr[i].location_y))];
  314. var distance = TMap.geometry.computeDistance(path);
  315. kqarr[i].kqjuli = parseFloat(distance);
  316. }
  317. funs(kqarr);
  318. }
  319. };
  320. //原生app定位中
  321. appbacklocation=function(res){
  322. var latitude = res.latitude;
  323. var longitude = res.longitude;
  324. var accuracy = parseFloat(res.accuracy);
  325. js.dw.dwsuccess({
  326. latitude:latitude,
  327. longitude:longitude,
  328. accuracy:accuracy
  329. });
  330. }
粤ICP备19079148号