app.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. let scene = document.querySelector('a-scene'),
  2. camera = document.querySelector('a-camera'),
  3. yRotation = document.querySelector('#y-rotation'),
  4. start = {lat: 22.364563, lon: 113.600967},
  5. end = {lat: 22.363379, lon: 113.598615},
  6. route = [],
  7. path = {},
  8. pathRecord = [],
  9. pathCreated = false;
  10. window.addEventListener('update-heading', function(data) {
  11. yRotation.innerText = Math.floor(data.detail) + "°";
  12. });
  13. // if this is not accurate we need to hook it onto camera.tick
  14. window.addEventListener('gps-camera-update-position', function(data) {
  15. // console.log('gps-camera-update-position', data);
  16. let cameraPosition = [data.detail.position.latitude, data.detail.position.longitude];
  17. // if(route.length > 1) {
  18. // route[0] = cameraPosition;
  19. // }
  20. if(pathCreated) {
  21. // update path
  22. } else if(!pathCreated && route.length > 1) {
  23. // put camera position at route[0]
  24. route[0] = cameraPosition;
  25. path = createPath(route);
  26. }
  27. });
  28. window.addEventListener('gps-position-update', function(position) {
  29. // console.log(position);
  30. });
  31. // function createPathRoot(position) {
  32. // let root = document.createElement('a-curve-point');
  33. // root.setAttribute('gps-entity-place', `latitude: ${lat}; longitude: ${lon};`);
  34. // path.appendChild(curvePoint);
  35. // }
  36. function getRoute(start, end) {
  37. axios.get('https://dev.virtualearth.net/REST/v1/Routes/Walking', {
  38. params: {
  39. 'wayPoint.1': `${start.lat},${start.lon}`,
  40. 'wayPoint.2': `${end.lat},${end.lon}`,
  41. 'routeAttributes': 'routePath',
  42. 'key': 'Ahwqh1mUNte20qOAphNKU-hSGsmEnavPTOU-749M7MJl7MYIdsTmL4XFoSwS4QvP'
  43. }
  44. }).then(response => {
  45. if(response.status === 200) {
  46. route = response.data.resourceSets[0].resources[0].routePath.line.coordinates;
  47. // drawPath(route);
  48. } else {
  49. throw new Error(response.status);
  50. }
  51. })
  52. }
  53. function createBoxes(route) {
  54. if(route.length) {
  55. console.log(route);
  56. route.forEach(coordinate => {
  57. const lat = coordinate[0];
  58. const lon = coordinate[1];
  59. const placeText = document.createElement('a-box');
  60. placeText.setAttribute('gps-entity-place', `latitude: ${lat}; longitude: ${lon};`);
  61. // placeText.setAttribute('title', 'PATH');
  62. placeText.setAttribute('scale', '5 5 5');
  63. scene.appendChild(placeText);
  64. });
  65. }
  66. }
  67. function createPath(route) {
  68. let path = {}; // null
  69. if(route.length > 1) {
  70. const pathElement = document.createElement('a-curve');
  71. pathElement.id = 'path';
  72. scene.appendChild(pathElement);
  73. path.el = pathElement;
  74. path.points = [];
  75. route.forEach(coordinate => {
  76. const lat = coordinate[0];
  77. const lon = coordinate[1];
  78. let pointElement = document.createElement('a-curve-point');
  79. pointElement.setAttribute('gps-entity-place', `latitude: ${lat}; longitude: ${lon};`);
  80. pathElement.appendChild(pointElement);
  81. path.points.push({el: pointElement, lat: lat, lon: lon});
  82. });
  83. // const drawPath = document.createElement('a-draw-curve');
  84. // drawPath.setAttribute('curveref', '#path');
  85. // drawPath.setAttribute('material', 'shader: line; color: green;');
  86. // scene.appendChild(drawPath);
  87. const renderElement = document.createElement('a-entity');
  88. renderElement.setAttribute('clone-along-curve', 'curve: #path; spacing: 2; scale: 1 1 1; rotation: 90 0 0;');
  89. renderElement.setAttribute('geometry', 'primitive:triangle;');
  90. renderElement.setAttribute('material', 'side: double; color: green;');
  91. scene.appendChild(renderElement);
  92. path.renderEl = renderElement;
  93. pathCreated = true;
  94. }
  95. return path;
  96. }
  97. function showPosition() {
  98. let lat = document.getElementById('latitude');
  99. let lon = document.getElementById('longitude');
  100. let options = {
  101. enableHighAccuracy: true,
  102. timeout: 30000,
  103. maximumAge: 0
  104. };
  105. function success(pos) {
  106. let crd = pos.coords;
  107. lat.innerHTML = `${crd.latitude}`;
  108. lon.innerHTML = `${crd.longitude}`;
  109. }
  110. function error(err) {
  111. console.warn(`ERROR(${err.code}): ${err.message}`);
  112. }
  113. navigator.geolocation.watchPosition(success, error, options);
  114. }
  115. showPosition();
  116. // console.log(coordtransform.wgs84togcj02(113.59573843475094, 22.36707700088935));
  117. getRoute(start, end);
  118. // wgs84 gps position: 22.36707700088935, 113.59573843475094
  119. // gcj02 gps position: 22.370123789469808, 113.5907111095581