app.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. class AddressMap {
  2. constructor(map) {
  3. this.element = map;
  4. this.$latitude = $("input[name=latitude]");
  5. this.$longitude = $("input[name=longitude]");
  6. this.map = new google.maps.Map(this.element, {
  7. center: this.latLng,
  8. zoom: 17
  9. });
  10. this.marker = new google.maps.Marker({
  11. map: this.map,
  12. position: this.latLng,
  13. draggable: true
  14. });
  15. this.marker.addListener("dragend", () => {
  16. const latLng = this.marker.getPosition();
  17. this.$latitude.val(latLng.lat());
  18. this.$longitude.val(latLng.lng());
  19. });
  20. this.geocoder = new google.maps.Geocoder();
  21. $("#plot-button").on("click", () => this.plot());
  22. }
  23. get latLng() {
  24. const latitude = this.$latitude.val() * 1;
  25. const longitude = this.$longitude.val() * 1;
  26. return {
  27. lat: latitude,
  28. lng: longitude
  29. };
  30. }
  31. plot() {
  32. const addressParts = [
  33. $("#address-1").val(),
  34. $("#address-2").val(),
  35. $("#city").val(),
  36. $("#county").val(),
  37. $("#postal-code").val(),
  38. $("#country").val(),
  39. ];
  40. const address = addressParts.filter(value => value.length > 0).join(", ");
  41. this.geocoder.geocode({
  42. address
  43. }, (results, status) => {
  44. if(status === "OK") {
  45. const { location } = results[0].geometry;
  46. this.map.setCenter(location);
  47. this.marker.setPosition(location);
  48. this.$latitude.val(location.lat);
  49. this.$longitude.val(location.lng);
  50. }
  51. else {
  52. alert("Could not plot for address: " + address);
  53. }
  54. });
  55. }
  56. }
  57. /* Map Overview */
  58. class GoogleMap {
  59. constructor(element) {
  60. this.element = element;
  61. this.markers = [];
  62. this.map = new google.maps.Map(this.element, {
  63. center: this.latLng,
  64. zoom: 5
  65. });
  66. this.$customerList = $("#customer-list");
  67. this.$customerList.on("click", ".list-group-item", event => {
  68. const $item = $(event.currentTarget);
  69. const customerId = $item.attr("data-id");
  70. const selected = $item.attr("data-selected");
  71. if(selected === "false") {
  72. // Add markers
  73. $.post("/api/get-markers", {
  74. customer_id: customerId
  75. }).then(response => {
  76. if(typeof this.markers[customerId] === "undefined") {
  77. this.markers[customerId] = [];
  78. }
  79. response.markers.forEach(markerData => {
  80. const marker = new google.maps.Marker({
  81. map: this.map,
  82. position: markerData.latLng,
  83. });
  84. marker.addListener("click", () => {
  85. window.location = markerData.url + "/update";
  86. });
  87. this.markers[customerId].push(marker);
  88. });
  89. $item.find("i").attr("class", "fa fa-fw fa-check text-success");
  90. $item.attr("data-selected", "true");
  91. });
  92. }
  93. else {
  94. // Remove markers
  95. this.markers[customerId].forEach(marker => {
  96. marker.setMap(null);
  97. });
  98. this.markers[customerId] = [];
  99. $item.find("i").attr("class", "fa fa-fw");
  100. $item.attr("data-selected", "false");
  101. }
  102. });
  103. }
  104. get latLng() {
  105. return {
  106. lat: 54,
  107. lng: -4
  108. };
  109. }
  110. }
  111. $(() => {
  112. const addressMap = document.querySelector("#address-map");
  113. if(addressMap) {
  114. new AddressMap(addressMap);
  115. }
  116. const map = document.getElementById("map");
  117. if(map) {
  118. new GoogleMap(map);
  119. }
  120. });