dingwei.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. ondwwait:function(){return false},
  31. start:function(){
  32. if(this.dwbool)return;
  33. this.dwbool = true;
  34. this.chaoshi();
  35. this.ondwstart(js.jssdkstate);
  36. if(js.jssdkstate != 1){
  37. this.htmldingw(0);
  38. }else{
  39. this.wxdingw();
  40. }
  41. },
  42. //定位等待
  43. wait:function(msg){
  44. var bo = this.ondwwait(msg);
  45. if(!bo)js.msg('wait',msg);
  46. },
  47. chaoshi:function(){
  48. clearTimeout(this.dwtimeer);
  49. this.dwtimeer = setTimeout(function(){
  50. var msg = '定位超时,请重新定位';
  51. js.msg('msg', msg);
  52. js.dw.ondwerr(msg);
  53. js.jssdkstate = 2;
  54. js.dw.dwbool=false;
  55. },20*1000);
  56. },
  57. clearchao:function(){
  58. clearTimeout(this.dwtimeer);
  59. this.dwbool = false;
  60. },
  61. //html5定位
  62. htmldingw:function(lx){
  63. var msg;
  64. if(appobj1('startLocation','appbacklocation')){
  65. this.wait('原生app定位中...');
  66. return;
  67. }
  68. if(window['api'] && api.startLocation){
  69. js.msg();
  70. if(api.systemType=='ios'){
  71. this.wait(''+api.systemType+'APP定位中...');
  72. api.startLocation({},function(ret,err){
  73. js.dw.appLocationSuc(ret,err);
  74. });
  75. return;
  76. }else if(lx==0){
  77. this.wait(''+api.systemType+'百度地图定位中...');
  78. if(!this.baiduLocation)this.baiduLocation = api.require('baiduLocation');
  79. if(this.baiduLocation){
  80. this.baiduLocation.startLocation({
  81. autoStop: false
  82. }, function(ret, err) {
  83. js.dw.baiduLocationSuc(ret,err);
  84. });
  85. }else{
  86. if(!this.bmLocation)this.bmLocation = api.require('bmLocation');
  87. if(this.bmLocation){
  88. this.bmLocation.configManager({
  89. coordinateType:'BMK09LL',accuracy:'hight_accuracy'
  90. });
  91. this.bmLocation.singleLocation({reGeocode:false},function(ret,err){
  92. var dtes = {};
  93. dtes.status = ret.status;
  94. if(ret.status){
  95. dtes.longitude = ret.location.longitude;
  96. dtes.latitude = ret.location.latitude;
  97. }
  98. js.dw.baiduLocationSuc(dtes,err);
  99. js.dw.bmLocation.stopLocation();
  100. });
  101. }
  102. }
  103. return;
  104. }
  105. }
  106. if(!navigator.geolocation){
  107. msg = '不支持浏览器定位';
  108. js.msg('msg',msg);
  109. this.clearchao();
  110. js.dw.ondwerr(msg);
  111. }else{
  112. this.wait('浏览器定位中...');
  113. //本地虚拟定位
  114. if(HOST=='127.0.0.1'){this.showPosition({coords:{latitude:24.51036967,longitude:118.178837299,accuracy:100}});return;}
  115. navigator.geolocation.getCurrentPosition(this.showPosition,this.showError,{
  116. enableHighAccuracy: true,
  117. timeout: 19000,
  118. maximumAge: 3000
  119. });
  120. }
  121. },
  122. //微信定位
  123. wxdingw:function(){
  124. var msg = '微信定位中...';
  125. if(js.isqywx)msg='企业微信定位中...';
  126. this.wait(msg);
  127. wx.getLocation({
  128. type: 'gcj02',
  129. success: function (res,err){
  130. js.dw.dwsuccess(res,err);
  131. },
  132. error:function(){
  133. js.jssdkstate = 2;
  134. js.dw.dwbool=false;
  135. js.dw.start();
  136. }
  137. });
  138. },
  139. appLocationSuc:function(ret,err){
  140. if(ret.status){
  141. if(!ret.accuracy)ret.accuracy = 200;
  142. this.dwsuccess(ret);
  143. }else{
  144. this.dwshibai(err.msg);
  145. }
  146. },
  147. baiduLocationSuc:function(ret,err){
  148. if(ret.status && ret.latitude){
  149. this.wait('定位成功,获取位置信息...');
  150. if(!ret.accuracy)ret.accuracy = 200;
  151. var center = new qq.maps.LatLng(ret.latitude,ret.longitude);
  152. this.translate(center, ret.accuracy, 3);
  153. }else{
  154. this.dwshibai('定位失败,检查是否给APP开定位权限');
  155. }
  156. },
  157. dwshibai:function(msg){
  158. this.clearchao();
  159. js.setmsg('');
  160. js.msg('msg', msg);
  161. this.ondwerr(msg);
  162. },
  163. dwsuccess:function(res){
  164. this.wait('定位成功,获取位置信息...');
  165. this.clearchao();
  166. var lat = parseFloat(res.latitude); // 纬度,浮点数,范围为90 ~ -90
  167. var lng = parseFloat(res.longitude); // 经度,浮点数,范围为180 ~ -180。
  168. var jid = parseFloat(res.accuracy); // 位置精度
  169. this.geocoder(lat,lng, jid);
  170. },
  171. showError:function (error){
  172. js.dw.clearchao();
  173. js.setmsg('');
  174. var msg='无法定位';
  175. switch(error.code){
  176. case error.PERMISSION_DENIED:
  177. msg="用户拒绝对获取地理位置的请求。"
  178. break;
  179. case error.POSITION_UNAVAILABLE:
  180. msg="位置信息是不可用的。"
  181. break;
  182. case error.TIMEOUT:
  183. msg="请求用户地理位置超时。"
  184. break;
  185. case error.UNKNOWN_ERROR:
  186. msg="未知错误。"
  187. break;
  188. }
  189. if(NOWURL.substr(0,5)!='https')msg+='必须使用https访问';
  190. js.msg('msg', msg);
  191. js.dw.ondwerr(msg);
  192. },
  193. showPosition:function(position){
  194. var res = position.coords;
  195. var latitude = res.latitude;
  196. var longitude = res.longitude;
  197. var accuracy = parseFloat(res.accuracy);
  198. var center = new qq.maps.LatLng(parseFloat(latitude), parseFloat(longitude));
  199. js.dw.translate(center, accuracy, 1);
  200. },
  201. //坐标转化type1原始
  202. translate:function(center, accuracy, type){
  203. qq.maps.convertor.translate(center,type,function(res){
  204. var latitude = res[0].lat;
  205. var longitude = res[0].lng;
  206. if(latitude==0 || latitude==0){
  207. js.dw.dwshibai('无法获取位置信息失败');
  208. }else{
  209. js.dw.dwsuccess({
  210. latitude:latitude,
  211. longitude:longitude,
  212. accuracy:accuracy
  213. });
  214. }
  215. });
  216. },
  217. //搜索位置
  218. geocoder:function(lat,lng, jid){
  219. if(!this.geocoderObj)this.geocoderObj = new qq.maps.Geocoder();
  220. var center = new qq.maps.LatLng(lat, lng);
  221. this.geocoderObj.getAddress(center);
  222. this.geocoderObj.setComplete(function(result){
  223. var address = result.detail.address;
  224. var dzarr = result.detail.addressComponents;
  225. //address = ''+dzarr.province+''+dzarr.city+''+dzarr.district+''+dzarr.street+'';
  226. //if(dzarr.streetnumber)address+=dzarr.streetnumber;
  227. //范围内地址
  228. var near = result.detail.nearPois,dist = 500,naddress,addressinfo;
  229. for(var i=0;i<near.length;i++){
  230. if(near[i].dist<dist){
  231. dist = near[i].dist;
  232. naddress = ''+near[i].name+'('+near[i].address+')';
  233. }
  234. }
  235. if(dist<500)address = naddress;
  236. addressinfo = ''+address;
  237. if(jid>0)addressinfo+='(精确'+jid+'米)';
  238. js.msg();
  239. js.dw.ondwcall({
  240. latitude:lat,
  241. longitude:lng,
  242. accuracy:jid,
  243. address:address,
  244. addressinfo:addressinfo,
  245. detail:result.detail,
  246. center:center
  247. });
  248. });
  249. this.geocoderObj.setError(function() {
  250. //var msg = '无法获取位置';js.msg('msg', msg);js.dw.ondwerr(msg);
  251. js.msg();
  252. js.dw.ondwcall({
  253. latitude:lat,
  254. longitude:lng,
  255. accuracy:jid,
  256. address:'未知位置',
  257. addressinfo:'定位成功未知位置',
  258. detail:'未知位置',
  259. center:center
  260. });
  261. });
  262. }
  263. };
  264. //原生app定位中
  265. appbacklocation=function(res){
  266. var latitude = res.latitude;
  267. var longitude = res.longitude;
  268. var accuracy = parseFloat(res.accuracy);
  269. js.dw.dwsuccess({
  270. latitude:latitude,
  271. longitude:longitude,
  272. accuracy:accuracy
  273. });
  274. }