dingwei.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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(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. });
  100. }
  101. }
  102. return;
  103. }
  104. }
  105. if(!navigator.geolocation){
  106. msg = '不支持浏览器定位';
  107. js.msg('msg',msg);
  108. this.clearchao();
  109. js.dw.ondwerr(msg);
  110. }else{
  111. this.wait('浏览器定位中...');
  112. navigator.geolocation.getCurrentPosition(this.showPosition,this.showError,{
  113. enableHighAccuracy: true,
  114. timeout: 19000,
  115. maximumAge: 3000
  116. });
  117. }
  118. },
  119. //微信定位
  120. wxdingw:function(){
  121. var msg = '微信定位中...';
  122. if(js.isqywx)msg='企业微信定位中...';
  123. this.wait(msg);
  124. wx.getLocation({
  125. type: 'gcj02',
  126. success: function (res,err){
  127. js.dw.dwsuccess(res,err);
  128. },
  129. error:function(){
  130. js.jssdkstate = 2;
  131. js.dw.dwbool=false;
  132. js.dw.start();
  133. }
  134. });
  135. },
  136. appLocationSuc:function(ret,err){
  137. if(ret.status){
  138. if(!ret.accuracy)ret.accuracy = 200;
  139. this.dwsuccess(ret);
  140. }else{
  141. this.dwshibai(err.msg);
  142. }
  143. },
  144. baiduLocationSuc:function(ret,err){
  145. if(ret.status && ret.latitude){
  146. this.wait('定位成功,获取位置信息...');
  147. if(!ret.accuracy)ret.accuracy = 200;
  148. var center = new qq.maps.LatLng(ret.latitude,ret.longitude);
  149. this.translate(center, ret.accuracy, 3);
  150. }else{
  151. this.dwshibai('定位失败,检查是否给APP开定位权限');
  152. }
  153. },
  154. dwshibai:function(msg){
  155. this.clearchao();
  156. js.setmsg('');
  157. js.msg('msg', msg);
  158. this.ondwerr(msg);
  159. },
  160. dwsuccess:function(res){
  161. this.wait('定位成功,获取位置信息...');
  162. this.clearchao();
  163. var lat = parseFloat(res.latitude); // 纬度,浮点数,范围为90 ~ -90
  164. var lng = parseFloat(res.longitude); // 经度,浮点数,范围为180 ~ -180。
  165. var jid = parseFloat(res.accuracy); // 位置精度
  166. this.geocoder(lat,lng, jid);
  167. },
  168. showError:function (error){
  169. js.dw.clearchao();
  170. js.setmsg('');
  171. var msg='无法定位';
  172. switch(error.code){
  173. case error.PERMISSION_DENIED:
  174. msg="用户拒绝对获取地理位置的请求。"
  175. break;
  176. case error.POSITION_UNAVAILABLE:
  177. msg="位置信息是不可用的。"
  178. break;
  179. case error.TIMEOUT:
  180. msg="请求用户地理位置超时。"
  181. break;
  182. case error.UNKNOWN_ERROR:
  183. msg="未知错误。"
  184. break;
  185. }
  186. if(NOWURL.substr(0,5)!='https')msg+='必须使用https访问';
  187. js.msg('msg', msg);
  188. js.dw.ondwerr(msg);
  189. },
  190. showPosition:function(position){
  191. var res = position.coords;
  192. var latitude = res.latitude;
  193. var longitude = res.longitude;
  194. var accuracy = parseFloat(res.accuracy);
  195. var center = new qq.maps.LatLng(parseFloat(latitude), parseFloat(longitude));
  196. js.dw.translate(center, accuracy, 1);
  197. },
  198. //坐标转化type1原始
  199. translate:function(center, accuracy, type){
  200. qq.maps.convertor.translate(center,type,function(res){
  201. var latitude = res[0].lat;
  202. var longitude = res[0].lng;
  203. if(latitude==0 || latitude==0){
  204. js.dw.dwshibai('无法获取位置信息失败');
  205. }else{
  206. js.dw.dwsuccess({
  207. latitude:latitude,
  208. longitude:longitude,
  209. accuracy:accuracy
  210. });
  211. }
  212. });
  213. },
  214. //搜索位置
  215. geocoder:function(lat,lng, jid){
  216. if(!this.geocoderObj)this.geocoderObj = new qq.maps.Geocoder();
  217. var center = new qq.maps.LatLng(lat, lng);
  218. this.geocoderObj.getAddress(center);
  219. this.geocoderObj.setComplete(function(result){
  220. var address = result.detail.address;
  221. var dzarr = result.detail.addressComponents;
  222. //address = ''+dzarr.province+''+dzarr.city+''+dzarr.district+''+dzarr.street+'';
  223. //if(dzarr.streetnumber)address+=dzarr.streetnumber;
  224. //范围内地址
  225. var near = result.detail.nearPois,dist = 500,naddress,addressinfo;
  226. for(var i=0;i<near.length;i++){
  227. if(near[i].dist<dist){
  228. dist = near[i].dist;
  229. naddress = ''+near[i].name+'('+near[i].address+')';
  230. }
  231. }
  232. if(dist<500)address = naddress;
  233. addressinfo = ''+address;
  234. if(jid>0)addressinfo+='(精确'+jid+'米)';
  235. js.msg();
  236. js.dw.ondwcall({
  237. latitude:lat,
  238. longitude:lng,
  239. accuracy:jid,
  240. address:address,
  241. addressinfo:addressinfo,
  242. detail:result.detail,
  243. center:center
  244. });
  245. });
  246. this.geocoderObj.setError(function() {
  247. //var msg = '无法获取位置';js.msg('msg', msg);js.dw.ondwerr(msg);
  248. js.msg();
  249. js.dw.ondwcall({
  250. latitude:lat,
  251. longitude:lng,
  252. accuracy:jid,
  253. address:'未知位置',
  254. addressinfo:'定位成功未知位置',
  255. detail:'未知位置',
  256. center:center
  257. });
  258. });
  259. }
  260. };
  261. //原生app定位中
  262. appbacklocation=function(res){
  263. var latitude = res.latitude;
  264. var longitude = res.longitude;
  265. var accuracy = parseFloat(res.accuracy);
  266. js.dw.dwsuccess({
  267. latitude:latitude,
  268. longitude:longitude,
  269. accuracy:accuracy
  270. });
  271. }