app-user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var addressesInitialised = false;
  2. var locationsInitialised = false;
  3. var addressMarkers = [];
  4. var locationMarkers = [];
  5. $(function() {
  6. $("[data-toggle=tab]").on("shown.bs.tab", function(event) {
  7. if(event.target.id == "addresses-tab" && addressesInitialised == false) {
  8. initialiseAddresses();
  9. }
  10. if(event.target.id == "nearby-locations-tab" && locationsInitialised == false) {
  11. initialiseLocations();
  12. }
  13. });
  14. $("#address-id").change(function() {
  15. initialiseLocations();
  16. });
  17. });
  18. function initialiseAddresses() {
  19. let map = maps["map"];
  20. $.get("/api/users/" + userId + "/get-addresses", {
  21. }, function(response) {
  22. if(!response.success) return false;
  23. if(!response.addresses) return false;
  24. let bounds = new google.maps.LatLngBounds;
  25. for(let i in response.addresses) {
  26. let address = response.addresses[i];
  27. let position = {
  28. lat: address.latitude,
  29. lng: address.longitude
  30. };
  31. let options = {
  32. map: map,
  33. position: position,
  34. title: address.name,
  35. icon: "/img/markers/address.png"
  36. }
  37. bounds.extend(position);
  38. let marker = new google.maps.Marker(options);
  39. }
  40. map.fitBounds(bounds);
  41. });
  42. }
  43. function initialiseLocations() {
  44. for(var i in locationMarkers) {
  45. let marker = locationMarkers[i];
  46. marker.setMap(null);
  47. marker = null;
  48. }
  49. locationMarkers = [];
  50. $("#nearby-locations-list-group").empty();
  51. let map = maps["nearby-locations-map"];
  52. $.get("/api/users/" + userId + "/get-locations", {
  53. "address_id": $("#address-id").val()
  54. }, function(response) {
  55. if(!response.success) return false;
  56. if(!response.locations) return false;
  57. let bounds = new google.maps.LatLngBounds;
  58. // User Address
  59. let position = {
  60. lat: response.latitude,
  61. lng: response.longitude
  62. };
  63. let options = {
  64. map: map,
  65. position: position,
  66. title: response.name,
  67. icon: "/img/markers/address.png"
  68. }
  69. bounds.extend(position);
  70. let marker = new google.maps.Marker(options);
  71. locationMarkers.push(marker);
  72. // Locations
  73. for(let i in response.locations) {
  74. let location = response.locations[i];
  75. let position = {
  76. lat: location.latitude,
  77. lng: location.longitude
  78. };
  79. let options = {
  80. map: map,
  81. position: position,
  82. title: location.name
  83. }
  84. if(location.icon) options.icon = "/img/markers/" + location.icon;
  85. else options.icon = "/img/markers/building-red.png";
  86. bounds.extend(position);
  87. let marker = new google.maps.Marker(options);
  88. locationMarkers.push(marker);
  89. if(location.internalId == 0) {
  90. location.internalId = "N/A";
  91. }
  92. // Add to list group
  93. let listGroupItem = $("div[data-nearby-location-list-group-item-template]").clone();
  94. listGroupItem.removeAttr("data-nearby-location-list-group-item-template");
  95. listGroupItem.find("a[data-location-name]").text(location.name);
  96. listGroupItem.find("span[data-location-distance]").text(location.distance + " miles");
  97. listGroupItem.find("span[data-location-id]").text(location.id);
  98. listGroupItem.find("span[data-location-internal-id]").text(location.internalId);
  99. listGroupItem.find("small[data-location-address]").text(location.address);
  100. listGroupItem.appendTo("#nearby-locations-list-group").show();
  101. }
  102. map.fitBounds(bounds);
  103. });
  104. }