Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.75 KB | None | 0 0
  1. /* globals define, document */
  2. define([
  3. 'app',
  4. 'services/user'
  5. ], function (app) {
  6. //'use strict';
  7.  
  8. app.directive('googleMap', [
  9. '$state',
  10. '$window',
  11. 'userService',
  12.  
  13.  
  14.  
  15.  
  16. function ($state, $window, userService) {
  17. return {
  18. scope: {
  19. events: '=',
  20. apiKey: '@',
  21. searching: '='
  22.  
  23. },
  24. restrict: 'A',
  25.  
  26. link: function (scope, element) {
  27. var counter = 0,
  28. map,
  29. mapsMarker,
  30. gmarkers = [],
  31. object,
  32. object2,
  33. mylat,
  34. mylong,
  35. eventsReady = false,
  36. searchedItem;
  37.  
  38. function addClick(marker, id) {
  39. $window.google.maps.event.addListener(marker, 'click', function () {
  40. $state.go('detail', {
  41. id: id
  42. });
  43. });
  44. }
  45.  
  46.  
  47.  
  48. function makeMarkers() {
  49. /**
  50.  
  51.  
  52. eventsReady = true;
  53.  
  54. var i = 0;
  55.  
  56. var query = new Parse.Query('Listings');
  57. query.include('parent');
  58. query.find({
  59. success: function (results) {
  60. //window.alert("Successfully retrieved " + results.length + " scores.");
  61.  
  62. //console.log(userService.username);
  63. // Do something with the returned Parse.Object values
  64. for (var i = 0; i < results.length; i++) {
  65. object = results[i];
  66. object2 = object.get('parent');
  67. // code below draws marker for all users in DB except for logged in user.
  68. //alert(object.get('title') + " " + searchedItem);
  69. alert(results.length);
  70. if (Parse.User.current().id != object2.id && object.get('title') == searchedItem ) {
  71. alert("adding marker");
  72. console.log("marker added");
  73. var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
  74. mapsMarker = new $window.google.maps.Marker({
  75. position: new $window.google.maps.LatLng(object2.get('lat'), object2.get('long')),
  76. map: map,
  77.  
  78. icon: image,
  79. clickable: true
  80. });
  81. gmarkers.push(mapsMarker);
  82.  
  83. }
  84.  
  85. }
  86.  
  87. },
  88. error: function (error) {
  89. alert("Error: " + error.code + " " + error.message);
  90. }
  91. });
  92.  
  93. // searchedItem = null;
  94. // scope.searching = null;
  95.  
  96. */
  97. }
  98.  
  99. function makeMarkersforUsers() {
  100. /**
  101. //if (eventsReady || !scope.events) {
  102. // return;
  103. //}
  104.  
  105. eventsReady = true;
  106.  
  107. var i = 0;
  108.  
  109. var query = new Parse.Query('User');
  110. //query.include(' parent');
  111. query.find({
  112. success: function (results) {
  113. //window.alert("Successfully retrieved " + results.length + " scores.");
  114.  
  115. //console.log(userService.username);
  116. // Do something with the returned Parse.Object values
  117. for (var i = 0; i < results.length; i++) {
  118. object = results[i];
  119.  
  120.  
  121. // code below draws marker for all users in DB except for logged in user.
  122.  
  123. if (Parse.User.current().id != object.id) {
  124. // alert(Parse.User.current().id);
  125.  
  126. var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
  127. mapsMarker = new $window.google.maps.Marker({
  128. position: new $window.google.maps.LatLng(object.get('lat'), object.get('long')),
  129. map: map,
  130.  
  131. icon: image,
  132. clickable: true
  133. });
  134. gmarkers.push(mapsMarker);
  135.  
  136. }
  137.  
  138. }
  139.  
  140. },
  141. error: function (error) {
  142. alert("Error: " + error.code + " " + error.message);
  143. }
  144. });
  145. */
  146.  
  147. }
  148. var watcher2 = scope.$watch(scope.searching, function () { alert("search string is: " + " " + scope.searching), true });
  149.  
  150. var watcher1 = scope.$watch(function () {
  151. return scope.searching;
  152. }, function (searching) {
  153. alert("value has changed");
  154. if (searching) {
  155. if (map) {
  156. alert(scope.searching);
  157. searchedItem = scope.searching;
  158. //TODO: update markers on map based on search string
  159. removeMarkers();
  160. makeMarkers();
  161. scope.searching = null;
  162.  
  163. }
  164. watcher1();
  165. }
  166. else {
  167. // alert("nothing searched");
  168. }
  169.  
  170. });
  171.  
  172.  
  173. function removeMarkers() {
  174. for (i = 0; i < gmarkers.length; i++) {
  175. gmarkers[i].setMap(null);
  176. }
  177. }
  178.  
  179. function makeMapAndMarkers() {
  180.  
  181. var mapOptions = {
  182. zoom: 13,
  183. disableDefaultUI: true
  184. };
  185. if (!map) {
  186. map = new $window.google.maps.Map(element[0], mapOptions);
  187. scope.imap = map;
  188.  
  189. /*
  190. if (navigator.geolocation) navigator.geolocation.getCurrentPosition(function (pos) {
  191.  
  192. var myloc = new google.maps.Marker({
  193.  
  194. clickable: false,
  195. icon: new google.maps.MarkerImage('//maps.gstatic.com/mapfiles/mobile/mobileimgs2.png',
  196. new google.maps.Size(22, 22),
  197. new google.maps.Point(0, 18),
  198. new google.maps.Point(11, 11)),
  199. shadow: null,
  200. zIndex: 999,
  201. map: map
  202. });
  203.  
  204.  
  205.  
  206. var me = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
  207. mylat = pos.coords.latitude;
  208. mylong = pos.coords.longitude;
  209. // set fields of lat and long in user DB
  210. Parse.User.current().set("lat", mylat);
  211. Parse.User.current().set("long", mylong);
  212. Parse.User.current().save();
  213. map.setCenter(me);
  214. myloc.setPosition(me);
  215.  
  216.  
  217. }, function (error) {
  218. // ...
  219. });
  220. */
  221. }
  222.  
  223.  
  224.  
  225. makeMarkersforUsers();
  226. }
  227.  
  228. //load google maps api script async, avoiding 'document.write' error
  229. function injectGoogle() {
  230. var cbId,
  231. wf,
  232. s,
  233. apiKey;
  234.  
  235. //callback id
  236. cbId = '_gmap_' + counter;
  237. $window[cbId] = makeMapAndMarkers;
  238. apiKey = 'key=' + scope.apiKey + '&';
  239.  
  240.  
  241.  
  242. wf = document.createElement('script');
  243. wf.src = ('https:' === document.location.protocol ? 'https' : 'http') +
  244. '://maps.googleapis.com/maps/api/js?' + apiKey + 'v=3&callback=' + cbId;
  245. wf.type = 'text/javascript';
  246. wf.async = 'true';
  247. document.body.appendChild(wf);
  248. }
  249.  
  250. if (!$window.google) {
  251. counter += 1;
  252. injectGoogle();
  253. //window.alert("inject google");
  254. } else {
  255. makeMapAndMarkers();
  256.  
  257.  
  258. }
  259. }
  260. };
  261. }
  262. ]);
  263. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement