navigation.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. map.on('complete', function(){
  12. renlian.layer.loading(false);
  13. });
  14. var userMarker = new AMap.Marker({icon: "./images/location.png", offset:new AMap.Pixel(-16, -16)});
  15. //骑行导航
  16. var riding = new AMap.Riding({
  17. map: map,
  18. autoFitView: true
  19. });
  20. //根据起终点坐标规划骑行路线
  21. riding.search(param.start, param.end, function(status, result) {
  22. // result即是对应的骑行路线数据信息,相关数据结构文档请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_RidingResult
  23. if (status === 'complete') {
  24. renlian.layer.toast('绘制骑行路线完成:');
  25. } else {
  26. renlian.layer.toast('骑行路线数据查询失败:' + result);
  27. }
  28. });
  29. var presentLnglat = null;
  30. function get_mobile_gps_location(onSuccess,onError){
  31. var onSuccess = function(position) {
  32. // alert('Latitude: ' + position.coords.latitude + '\n' +
  33. // 'Longitude: ' + position.coords.longitude + '\n' +
  34. // 'Altitude: ' + position.coords.altitude + '\n' +
  35. // 'Accuracy: ' + position.coords.accuracy + '\n' +
  36. // 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
  37. // 'Heading: ' + position.coords.heading + '\n' +
  38. // 'Speed: ' + position.coords.speed + '\n' +
  39. // 'Timestamp: ' + position.timestamp + '\n');
  40. presentLnglat = new AMap.LngLat(position.coords.longitude, position.coords.latitude);
  41. userMarker.setPosition(presentLnglat);
  42. userMarker.setMap(map);
  43. };
  44. // onError Callback receives a PositionError object
  45. //
  46. function onError(error) {
  47. alert('code: ' + error.code + '\n' +
  48. 'message: ' + error.message + '\n');
  49. }
  50. navigator.geolocation.getCurrentPosition(onSuccess, onError);
  51. }
  52. var geocoder = null;
  53. var locationAuto = setInterval(function(){
  54. if($('.f-page').length < 2){
  55. clearTimeout(locationAuto);
  56. }
  57. if(!window.lrmui.isreal){
  58. get_mobile_gps_location();
  59. var lnglat = presentLnglat;
  60. // var lnglat = [113.951104,22.542902];
  61. if(!geocoder){
  62. geocoder = new AMap.Geocoder({
  63. radius: 50
  64. });
  65. }
  66. console.log('dd');
  67. geocoder.getAddress(lnglat, function(status, result) {
  68. if (status === 'complete' && result.regeocode) {
  69. address = result.regeocode.formattedAddress;
  70. $('#navg-address').text(address);
  71. }else{console.log(JSON.stringify(result))}
  72. });
  73. }
  74. }, 2000);
  75. }
  76. };
  77. return page;
  78. })();