navigation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. (function () {
  2. var page = {
  3. init: function ($page, param) {
  4. console.log('~~~~~~~~~~~~');
  5. console.log(param);
  6. var geocoder = null;
  7. var originName='';//北四环东路 望和路
  8. var origin = new Array();//origin[0]为纬度,origin[1]为经度
  9. if(param.startEnd.start){
  10. var start = param.startEnd.start;
  11. origin = new Array(start.lat, start.lng)
  12. if(!geocoder){
  13. geocoder = new AMap.Geocoder({
  14. radius: 50
  15. });
  16. }
  17. geocoder.getAddress(start, function(status, result) {
  18. if (status === 'complete' && result.regeocode) {
  19. var address = result.regeocode.formattedAddress;
  20. originName = address;
  21. }else{console.log(JSON.stringify(result))}
  22. });
  23. }
  24. var destinationName = param.endAddress;
  25. var destination=new Array(param.startEnd.end.lat, param.startEnd.end.lng);//目的地纬度、经度。先纬度后经度
  26. renlian.layer.loading(true, "加载数据中");
  27. //地图初始化
  28. var map = new AMap.Map("navigation-map", {
  29. resizeEnable: true,
  30. center: [113.955439, 22.542956],//地图中心点
  31. zoom: 12 //地图显示的缩放级别
  32. });
  33. var mapList = [
  34. {
  35. value: "com.baidu.BaiduMap",
  36. text: "百度地图",
  37. },
  38. {
  39. value: "com.autonavi.minimap",
  40. text: "高德地图",
  41. },
  42. {
  43. value: "com.tencent.map",
  44. text: "腾讯地图",
  45. },
  46. {
  47. value: "com.google.android.apps.maps",
  48. text: "谷歌地图",
  49. }
  50. ];
  51. map.on('complete', function(){
  52. renlian.layer.loading(false);
  53. });
  54. if(!param.startEnd.start){
  55. renlian.layer.toast('无法获取定位点');
  56. }
  57. var riding = null;
  58. var userMarker = new AMap.Marker({icon: "./images/location.png", offset:new AMap.Pixel(-16, -16)});
  59. //骑行导航
  60. if(!riding){
  61. var riding = new AMap.Riding({
  62. map: map,
  63. autoFitView: true
  64. });
  65. }
  66. //根据起终点坐标规划骑行路线
  67. riding.search(param.startEnd.start, param.startEnd.end, function(status, result) {
  68. if (status === 'complete') {
  69. } else {
  70. renlian.layer.toast('骑行路线数据查询失败:' + result);
  71. }
  72. });
  73. var presentLnglat = null;
  74. function get_mobile_gps_location(onSuccess,onError){
  75. var onSuccess = function(position) {
  76. // alert('Latitude: ' + position.coords.latitude + '\n' +
  77. // 'Longitude: ' + position.coords.longitude + '\n' +
  78. // 'Altitude: ' + position.coords.altitude + '\n' +
  79. // 'Accuracy: ' + position.coords.accuracy + '\n' +
  80. // 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
  81. // 'Heading: ' + position.coords.heading + '\n' +
  82. // 'Speed: ' + position.coords.speed + '\n' +
  83. // 'Timestamp: ' + position.timestamp + '\n');
  84. var gps = [position.coords.longitude, position.coords.latitude];
  85. AMap.convertFrom(gps, 'gps', function (status, result) {
  86. if (result.info === 'ok') {
  87. var lnglats = result.locations; // Array.<LngLat>
  88. }
  89. presentLnglat = new AMap.LngLat(lnglats[0].lng, lnglats[0].lat);
  90. userMarker.setPosition(presentLnglat);
  91. userMarker.setMap(map);
  92. });
  93. };
  94. function onError(error) {
  95. console.log('code: ' + error.code + '\n' +
  96. 'message: ' + error.message + '\n');
  97. }
  98. navigator.geolocation.getCurrentPosition(onSuccess, onError);
  99. }
  100. get_mobile_gps_location();
  101. var locationAuto = setInterval(function(){
  102. if($('.f-page').length < 2){
  103. clearTimeout(locationAuto);
  104. }
  105. if(!window.lrmui.isreal){
  106. get_mobile_gps_location();
  107. var lnglat = presentLnglat;
  108. if(!geocoder){
  109. geocoder = new AMap.Geocoder({
  110. radius: 50
  111. });
  112. }
  113. origin = new Array(lnglat.lat, lnglat.lng);
  114. geocoder.getAddress(lnglat, function(status, result) {
  115. if (status === 'complete' && result.regeocode) {
  116. var address = result.regeocode.formattedAddress;
  117. originName = address;
  118. $('#navg-address').text(address);
  119. }else{console.log(JSON.stringify(result))}
  120. });
  121. }
  122. }, 6000);
  123. function haveInstalledApp(){//通过包名判断app是否安装
  124. var mapList = document.getElementsByClassName('mapLi');
  125. var baiduPackageName = "com.baidu.BaiduMap";
  126. var gaodePackageName = "com.autonavi.minimap";
  127. var tengxunPackageName = "com.tencent.map";
  128. var gugePackageName = "com.google.android.apps.maps";
  129. appAvailability.check(baiduPackageName,function () {//success callback
  130. mapList.item(0).style.display = 'block';
  131. },function () {//
  132. mapList.item(0).style.display = 'none';
  133. });
  134. appAvailability.check(gaodePackageName,function () {//success callback
  135. mapList.item(1).style.display = 'block';
  136. },function () {//
  137. mapList.item(1).style.display = 'none';
  138. });
  139. appAvailability.check(tengxunPackageName,function () {//success callback
  140. mapList.item(2).style.display = 'block';
  141. },function () {//
  142. mapList.item(2).style.display = 'none';
  143. });
  144. appAvailability.check(gugePackageName,function () {//success callback
  145. mapList.item(3).style.display = 'block';
  146. // mapList.item(4).style.display = 'block';
  147. },function () {
  148. mapList.item(3).style.display = 'none';
  149. });
  150. }
  151. $('#daohang-button').rlpicker({
  152. data: mapList
  153. },function(map){
  154. appAvailability.check(map,function () {//success callback
  155. if(!origin){
  156. renlian.layer.toast('无法获取定位点');
  157. get_mobile_gps_location();
  158. return;
  159. }
  160. if(map == 'com.baidu.BaiduMap'){
  161. checkBaidu();
  162. }
  163. if(map == 'com.autonavi.minimap'){
  164. checkGaode();
  165. }
  166. if(map == 'com.tencent.map'){
  167. checkTengxun();
  168. }
  169. if(map == 'com.google.android.apps.maps'){
  170. checkGuge();
  171. }
  172. },function () {//
  173. renlian.layer.warning('未检测到安装了该地图应用', function () { }, '提示', '确定');
  174. });
  175. });
  176. //
  177. function checkBaidu(){
  178. var sApp = startApp.set({ /* params */
  179. "action":"ACTION_VIEW",
  180. "category":"CATEGORY_DEFAULT",
  181. "type":"text/css",
  182. "package":"com.baidu.BaiduMap",
  183. "uri":"baidumap://map/direction?origin=name:"+originName+"|latlng:"+origin[0]+","+origin[1]+"&destination=name:"+destinationName+"|latlng:"+destination[0]+","+destination[1]+"&mode=driving",
  184. "flags":["FLAG_ACTIVITY_CLEAR_TOP","FLAG_ACTIVITY_CLEAR_TASK"],
  185. "intentstart":"startActivity"
  186. });
  187. sApp.start(function() { /* success */
  188. //alert("OK");
  189. }, function(error) { /* fail */
  190. alert(error);
  191. });
  192. }
  193. function checkGaode(){
  194. var sApp = startApp.set({ /* params */
  195. "action":"ACTION_VIEW",
  196. "category":"CATEGORY_DEFAULT",
  197. "type":"text/css",
  198. "package":"com.autonavi.minimap",
  199. "uri":"amapuri://route/plan/?slat="+origin[0]+"&slon="+origin[1]+"&sname="+originName+"&dlat="+destination[0]+"&dlon="+destination[1]+"&dname="+destinationName+"&dev=0&t=0",
  200. "flags":["FLAG_ACTIVITY_CLEAR_TOP","FLAG_ACTIVITY_CLEAR_TASK"],
  201. "intentstart":"startActivity"
  202. });
  203. sApp.start(function() { /* success */
  204. //alert("OK");
  205. }, function(error) { /* fail */
  206. alert(error);
  207. });
  208. }
  209. function checkTengxun(){
  210. var sApp = startApp.set({ /* params */
  211. "action":"ACTION_VIEW",
  212. "category":"CATEGORY_DEFAULT",
  213. "type":"text/css",
  214. "package":"com.tencent.map",
  215. "uri":"qqmap://map/routeplan?type=drive&from="+originName+"&fromcoord="+origin[0]+","+origin[1]+"&to="+destinationName+"&tocoord="+destination[0]+","+destination[1]+"&coord_type=1&policy=0",
  216. "flags":["FLAG_ACTIVITY_CLEAR_TOP","FLAG_ACTIVITY_CLEAR_TASK"],
  217. "intentstart":"startActivity"
  218. });
  219. sApp.start(function() { /* success */
  220. //alert("OK");
  221. }, function(error) { /* fail */
  222. alert(error);
  223. });
  224. }
  225. function checkGuge(){//指定终点经纬度,默认当前位置
  226. var sApp = startApp.set({ /* params */
  227. "action":"ACTION_VIEW",
  228. "category":"CATEGORY_DEFAULT",
  229. "type":"text/css",
  230. "package":"com.google.android.apps.maps",
  231. "uri":"google.navigation:q="+destination[0]+","+destination[1],
  232. "flags":["FLAG_ACTIVITY_CLEAR_TOP","FLAG_ACTIVITY_CLEAR_TASK"],
  233. "intentstart":"startActivity"
  234. });
  235. sApp.start(function() { /* success */
  236. //alert("OK");
  237. }, function(error) { /* fail */
  238. alert(error);
  239. });
  240. }
  241. },
  242. reload:function(pageinfo){
  243. console.log('testsfadfasd')
  244. }
  245. };
  246. return page;
  247. })();