navigation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. (function () {
  2. var page = {
  3. init: function ($page, param) {
  4. renlian.layer.loading(true, "加载数据中");
  5. //地图初始化
  6. var map = new AMap.Map("navigation-map", {
  7. resizeEnable: true,
  8. center: [113.955439, 22.542956],//地图中心点
  9. zoom: 12 //地图显示的缩放级别
  10. });
  11. console.log('aa');
  12. map.on('complete', function(){
  13. renlian.layer.loading(false);
  14. });
  15. var riding = null;
  16. var userMarker = new AMap.Marker({icon: "./images/location.png", offset:new AMap.Pixel(-16, -16)});
  17. //骑行导航
  18. if(!riding){
  19. var riding = new AMap.Riding({
  20. map: map,
  21. autoFitView: true
  22. });
  23. }
  24. //根据起终点坐标规划骑行路线
  25. riding.search(param.start, param.end, function(status, result) {
  26. // result即是对应的骑行路线数据信息,相关数据结构文档请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_RidingResult
  27. if (status === 'complete') {
  28. // renlian.layer.toast('绘制骑行路线完成:');
  29. } else {
  30. renlian.layer.toast('骑行路线数据查询失败:' + result);
  31. }
  32. });
  33. var presentLnglat = null;
  34. function get_mobile_gps_location(onSuccess,onError){
  35. var onSuccess = function(position) {
  36. // alert('Latitude: ' + position.coords.latitude + '\n' +
  37. // 'Longitude: ' + position.coords.longitude + '\n' +
  38. // 'Altitude: ' + position.coords.altitude + '\n' +
  39. // 'Accuracy: ' + position.coords.accuracy + '\n' +
  40. // 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
  41. // 'Heading: ' + position.coords.heading + '\n' +
  42. // 'Speed: ' + position.coords.speed + '\n' +
  43. // 'Timestamp: ' + position.timestamp + '\n');
  44. var gps = [position.coords.longitude, position.coords.latitude];
  45. AMap.convertFrom(gps, 'gps', function (status, result) {
  46. if (result.info === 'ok') {
  47. var lnglats = result.locations; // Array.<LngLat>
  48. }
  49. presentLnglat = new AMap.LngLat(lnglats[0].lng, lnglats[0].lat);
  50. userMarker.setPosition(presentLnglat);
  51. userMarker.setMap(map);
  52. });
  53. // presentLnglat = new AMap.LngLat(position.coords.longitude, position.coords.latitude);
  54. // userMarker.setPosition(presentLnglat);
  55. // userMarker.setMap(map);
  56. };
  57. // onError Callback receives a PositionError object
  58. //
  59. function onError(error) {
  60. console.log('code: ' + error.code + '\n' +
  61. 'message: ' + error.message + '\n');
  62. }
  63. navigator.geolocation.getCurrentPosition(onSuccess, onError);
  64. }
  65. var geocoder = null;
  66. var locationAuto = setInterval(function(){
  67. if($('.f-page').length < 2){
  68. clearTimeout(locationAuto);
  69. }
  70. if(window.lrmui.isreal){
  71. get_mobile_gps_location();
  72. var lnglat = presentLnglat;
  73. // var lnglat = [113.951104,22.542902];
  74. if(!geocoder){
  75. geocoder = new AMap.Geocoder({
  76. radius: 50
  77. });
  78. }
  79. console.log('dd');
  80. geocoder.getAddress(lnglat, function(status, result) {
  81. if (status === 'complete' && result.regeocode) {
  82. address = result.regeocode.formattedAddress;
  83. $('#navg-address').text(address);
  84. }else{console.log(JSON.stringify(result))}
  85. });
  86. }
  87. }, 2000);
  88. },
  89. reload:function(pageinfo){
  90. console.log('testsfadfasd')
  91. }
  92. };
  93. return page;
  94. })();