1
0

app-user.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. }
  36. bounds.extend(position);
  37. let marker = new google.maps.Marker(options);
  38. }
  39. map.fitBounds(bounds);
  40. });
  41. }
  42. function initialiseLocations() {
  43. for(var i in locationMarkers) {
  44. let marker = locationMarkers[i];
  45. marker.setMap(null);
  46. marker = null;
  47. }
  48. locationMarkers = [];
  49. $("#nearby-locations-list-group").empty();
  50. let map = maps["nearby-locations-map"];
  51. $.get("/api/users/" + userId + "/get-locations", {
  52. "address_id": $("#address-id").val()
  53. }, function(response) {
  54. if(!response.success) return false;
  55. if(!response.locations) return false;
  56. let bounds = new google.maps.LatLngBounds;
  57. // User Address
  58. let position = {
  59. lat: response.latitude,
  60. lng: response.longitude
  61. };
  62. let options = {
  63. map: map,
  64. position: position,
  65. title: response.name
  66. }
  67. bounds.extend(position);
  68. let marker = new google.maps.Marker(options);
  69. locationMarkers.push(marker);
  70. // Locations
  71. for(let i in response.locations) {
  72. let location = response.locations[i];
  73. let position = {
  74. lat: location.latitude,
  75. lng: location.longitude
  76. };
  77. let options = {
  78. map: map,
  79. position: position,
  80. title: location.name
  81. }
  82. if(location.icon) options.icon = "/img/markers/" + location.icon;
  83. bounds.extend(position);
  84. let marker = new google.maps.Marker(options);
  85. locationMarkers.push(marker);
  86. if(location.internalId == 0) {
  87. location.internalId = "N/A";
  88. }
  89. // Add to list group
  90. let listGroupItem = $("div[data-nearby-location-list-group-item-template]").clone();
  91. listGroupItem.removeAttr("data-nearby-location-list-group-item-template");
  92. listGroupItem.find("a[data-location-name]").text(location.name);
  93. listGroupItem.find("span[data-location-distance]").text(location.distance + " miles");
  94. listGroupItem.find("span[data-location-id]").text(location.id);
  95. listGroupItem.find("span[data-location-internal-id]").text(location.internalId);
  96. listGroupItem.find("small[data-location-address]").text(location.address);
  97. listGroupItem.appendTo("#nearby-locations-list-group").show();
  98. }
  99. map.fitBounds(bounds);
  100. });
  101. }