Guest User

Untitled

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. var initialLocations = [{
  2. name: 'Golden Gate',
  3. lat: 37.819929,
  4. long: -122.478255
  5. }, {
  6. name: 'AT&T Park',
  7. lat: 37.7749,
  8. long: -122.4194
  9.  
  10. }, {
  11. name: 'Twin Peaks',
  12. lat: 37.7544,
  13. long: -122.4477
  14. },
  15. ];
  16.  
  17. var clientID = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
  18. var clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx";
  19.  
  20. var Location = function(data) {
  21. var self = this;
  22.  
  23. this.name = data.name;
  24. this.lat = data.lat;
  25. this.long = data.long;
  26. this.URL = "";
  27. this.street = "";
  28. this.city = "";
  29. this.phone = "";
  30.  
  31. this.visible = ko.observable(true);
  32.  
  33. var foursquareURL = 'https://api.foursquare.com/v2/venues/search?ll=' + this.lat + ',' + this.long + '&client_id=' + clientID + '&client_secret=' + clientSecret + '&v=20160118' + '&query=' + this.name;
  34.  
  35. $.getJSON(foursquareURL).done(function(data) {
  36. var results = data.response.venues[0];
  37. self.URL = results.url || 'No url found';
  38. self.street = results.location.formattedAddress[0];
  39. self.city = results.location.formattedAddress[1];
  40. self.phone = results.contact.phone || 'No phone found';;
  41.  
  42. }).fail(function() {
  43. alert("Error found. Please refresh your page.");
  44. });
  45.  
  46. // Info window
  47.  
  48.  
  49. this.infoWindow = new google.maps.InfoWindow({
  50.  
  51.  
  52. });
  53.  
  54.  
  55. // Marker
  56.  
  57. this.marker = new google.maps.Marker({
  58. position: new google.maps.LatLng(data.lat, data.long),
  59. map: map,
  60. title: data.name
  61. });
  62.  
  63.  
  64. this.marker.addListener('click', function() {
  65.  
  66. self.contentString = '<div class="info-window-content"><div class="title"><b>' + data.name + "</b></div>" +
  67. '<div class="content"><a href="' + self.URL + '">' + self.URL + "</a></div>" +
  68. '<div class="content">' + self.street + "</div>" +
  69. '<div class="content">' + self.city + "</div>" +
  70. '<div class="content"><a href="tel:' + self.phone + '">' + self.phone + "</a></div></div>";
  71.  
  72. self.infoWindow.setContent(self.contentString);
  73.  
  74. self.infoWindow.open(map, this);
  75.  
  76. self.marker.setAnimation(google.maps.Animation.BOUNCE);
  77. setTimeout(function() {
  78. self.marker.setAnimation(null);
  79. }, 2100);
  80. });
  81.  
  82. this.bounce = function(place) {
  83. google.maps.event.trigger(self.marker, 'click');
  84. };
  85. };
  86.  
  87.  
  88. //-ViewModel
  89. function AppViewModel() {
  90. var self = this;
  91.  
  92. // create array of places
  93.  
  94. this.locationList = ko.observableArray([]);
  95. this.searchLocation = ko.observable('');
  96.  
  97. map = new google.maps.Map(document.getElementById('map'), {
  98. zoom: 12,
  99. center: {
  100. lat: 37.77986,
  101. lng: -122.429
  102. }
  103. });
  104.  
  105. initialLocations.forEach(function(locationItem) {
  106. self.locationList.push(new Location(locationItem));
  107. });
  108.  
  109. this.filteredList = ko.computed(function() {
  110. var filter = self.searchLocation().toLowerCase();
  111. if (!filter) {
  112. self.locationList().forEach(function(locationItem) {
  113. locationItem.visible(true);
  114. });
  115. return self.locationList();
  116. } else {
  117. return ko.utils.arrayFilter(self.locationList(), function(locationItem) {
  118. var string = locationItem.name.toLowerCase();
  119. var result = (string.search(filter) >= 0);
  120. locationItem.visible(result);
  121. return result;
  122. });
  123. }
  124. }, self);
  125.  
  126. this.mapElem = document.getElementById('map');
  127. this.mapElem.style.height = window.innerHeight - 50;
  128. }
  129. // Bind the VeiewModel to the view using knockout
  130. function startApp() {
  131. ko.applyBindings(new AppViewModel());
  132. }
  133.  
  134. function errorHandling() {
  135. alert("Error loading page, try refresh in page.");
  136. }
Add Comment
Please, Sign In to add comment