| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- var addressesInitialised = false;
- var locationsInitialised = false;
- var addressMarkers = [];
- var locationMarkers = [];
- $(function() {
- $("[data-toggle=tab]").on("shown.bs.tab", function(event) {
- if(event.target.id == "addresses-tab" && addressesInitialised == false) {
- initialiseAddresses();
- }
- if(event.target.id == "nearby-locations-tab" && locationsInitialised == false) {
- initialiseLocations();
- }
- });
- $("#address-id").change(function() {
- initialiseLocations();
- });
- });
- function initialiseAddresses() {
- let map = maps["map"];
- $.get("/api/users/" + userId + "/get-addresses", {
- }, function(response) {
- if(!response.success) return false;
- if(!response.addresses) return false;
- let bounds = new google.maps.LatLngBounds;
- for(let i in response.addresses) {
- let address = response.addresses[i];
- let position = {
- lat: address.latitude,
- lng: address.longitude
- };
- let options = {
- map: map,
- position: position,
- title: address.name
- }
- bounds.extend(position);
- let marker = new google.maps.Marker(options);
- }
- map.fitBounds(bounds);
- });
- }
- function initialiseLocations() {
- for(var i in locationMarkers) {
- let marker = locationMarkers[i];
- marker.setMap(null);
- marker = null;
- }
- locationMarkers = [];
- $("#nearby-locations-list-group").empty();
- let map = maps["nearby-locations-map"];
- $.get("/api/users/" + userId + "/get-locations", {
- "address_id": $("#address-id").val()
- }, function(response) {
- if(!response.success) return false;
- if(!response.locations) return false;
- let bounds = new google.maps.LatLngBounds;
- // User Address
- let position = {
- lat: response.latitude,
- lng: response.longitude
- };
- let options = {
- map: map,
- position: position,
- title: response.name
- }
- bounds.extend(position);
- let marker = new google.maps.Marker(options);
- locationMarkers.push(marker);
- // Locations
- for(let i in response.locations) {
- let location = response.locations[i];
- let position = {
- lat: location.latitude,
- lng: location.longitude
- };
- let options = {
- map: map,
- position: position,
- title: location.name
- }
- if(location.icon) options.icon = "/img/markers/" + location.icon;
- bounds.extend(position);
- let marker = new google.maps.Marker(options);
- locationMarkers.push(marker);
- if(location.internalId == 0) {
- location.internalId = "N/A";
- }
- // Add to list group
- let listGroupItem = $("div[data-nearby-location-list-group-item-template]").clone();
- listGroupItem.removeAttr("data-nearby-location-list-group-item-template");
- listGroupItem.find("a[data-location-name]").text(location.name);
- listGroupItem.find("span[data-location-distance]").text(location.distance + " miles");
- listGroupItem.find("span[data-location-id]").text(location.id);
- listGroupItem.find("span[data-location-internal-id]").text(location.internalId);
- listGroupItem.find("small[data-location-address]").text(location.address);
- listGroupItem.appendTo("#nearby-locations-list-group").show();
- }
- map.fitBounds(bounds);
- });
- }
|