PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/example/example.html

https://github.com/garthenweb/backbone.googlemaps
HTML | 295 lines | 237 code | 56 blank | 2 comment | 0 complexity | a701acf38930736354e43e768d4a7b4b MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>Backbone.GoogleMaps | Example</title>
  6. <style type="text/css">
  7. html { height: 100% }
  8. body { height: 100%; margin: 0; padding: 0 }
  9. #map_canvas { height: 100% }
  10. .overlay {
  11. position:fixed;
  12. background:rgba(255, 255, 255, 0.95);
  13. border:1px solid #aaa;
  14. padding:20px;
  15. z-index:5;
  16. }
  17. #search-area {
  18. right:40px;
  19. top:40px;
  20. }
  21. #listing {
  22. left:40px;
  23. bottom:150px;
  24. }
  25. #listing li {
  26. list-style:none;
  27. border-bottom:1px solid #ccc;
  28. padding:10px;
  29. cursor: pointer;
  30. }
  31. #listing li:hover {
  32. background:#006;
  33. color:#fff;
  34. }
  35. button {
  36. background:#006;
  37. color:#fff;
  38. font-weight:bold;
  39. border:1px solid rgba(0,0,0,.85);
  40. font-size:14px;
  41. padding:10px 15px;
  42. outline:none;
  43. }
  44. </style>
  45. <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=[YOUR_API_KEY]&sensor=false"></script>
  46. <script type="text/javascript" src="lib/jquery-1.7.1.min.js"></script>
  47. <script type="text/javascript" src="lib/underscore-min.js"></script>
  48. <script type="text/javascript" src="lib/backbone-min.js"></script>
  49. <script type="text/javascript" src="../lib/backbone.googlemaps-min.js"></script>
  50. </head>
  51. <body>
  52. <!-- TEMPLATE: InfoWindow
  53. ============================== -->
  54. <script id="infoWindow-template" type="text/html">
  55. <h2><%=title %></h2>
  56. <p>Latitude: <%=lat %></p>
  57. <p>Longitude: <%=lng %></p>
  58. </script>
  59. <div id="search-area" class="overlay">
  60. <button id="museums">find museums</button>
  61. <button id="bars">find bars</button>
  62. <button id="addBtn">add</button>
  63. <button id="removeBtn">remove</button>
  64. </div>
  65. <div id="map_canvas" style="width:100%; height:100%"></div>
  66. <script type="text/javascript">
  67. /**
  68. * Example App
  69. */
  70. // Sample Data
  71. var museums = [
  72. {
  73. title: "Walker Art Center",
  74. lat: 44.9796635,
  75. lng: -93.2748776
  76. },
  77. {
  78. title: "Science Museum of Minnesota",
  79. lat: 44.9429618,
  80. lng: -93.0981016
  81. },
  82. {
  83. title: "The Museum of Russian Art",
  84. lat: 44.9036337,
  85. lng: -93.2755413
  86. }
  87. ];
  88. var bars = [
  89. {
  90. title: "Park Tavern",
  91. lat: 44.9413272,
  92. lng: -93.3705791,
  93. },
  94. {
  95. title: "Chatterbox Pub",
  96. lat: 44.9393882,
  97. lng: -93.2391039
  98. },
  99. {
  100. title: "Acadia Cafe",
  101. lat: 44.9709853,
  102. lng: -93.2470717
  103. }
  104. ];
  105. var App = {};
  106. App.Location = Backbone.GoogleMaps.Location.extend({
  107. idAttribute: 'title',
  108. defaults: {
  109. lat: 45,
  110. lng: -93
  111. }
  112. });
  113. App.LocationCollection = Backbone.GoogleMaps.LocationCollection.extend({
  114. model: App.Location
  115. });
  116. App.InfoWindow = Backbone.GoogleMaps.InfoWindow.extend({
  117. template: '#infoWindow-template',
  118. events: {
  119. 'mouseenter h2': 'logTest'
  120. },
  121. logTest: function() {
  122. console.log('test in InfoWindow');
  123. }
  124. });
  125. App.MarkerView = Backbone.GoogleMaps.MarkerView.extend({
  126. infoWindow: App.InfoWindow,
  127. overlayOptions: {
  128. draggable: true
  129. },
  130. initialize: function() {
  131. _.bindAll(this, 'handleDragEnd');
  132. },
  133. mapEvents: {
  134. 'dragend' : 'handleDragEnd'
  135. },
  136. handleDragEnd: function(e) {
  137. alert('Dropped at: \n Lat: ' + e.latLng.lat() + '\n lng: ' + e.latLng.lng());
  138. }
  139. });
  140. App.MarkerCollectionView = Backbone.GoogleMaps.MarkerCollectionView.extend({
  141. markerView: App.MarkerView
  142. });
  143. App.init = function() {
  144. this.createMap();
  145. this.places = new this.LocationCollection(museums);
  146. // Render Markers
  147. var markerCollectionView = new this.MarkerCollectionView({
  148. collection: this.places,
  149. map: this.map
  150. });
  151. markerCollectionView.render();
  152. // Render ListView
  153. var listView = new App.ListView({
  154. collection: this.places
  155. });
  156. listView.render();
  157. }
  158. App.createMap = function() {
  159. var mapOptions = {
  160. center: new google.maps.LatLng(44.9796635, -93.2748776),
  161. zoom: 12,
  162. mapTypeId: google.maps.MapTypeId.ROADMAP
  163. }
  164. // Instantiate map
  165. this.map = new google.maps.Map($('#map_canvas')[0], mapOptions);
  166. }
  167. /**
  168. * List view
  169. */
  170. App.ItemView = Backbone.View.extend({
  171. template: '<%=title %>',
  172. tagName: 'li',
  173. events: {
  174. 'mouseenter': 'selectItem',
  175. 'mouseleave': 'deselectItem'
  176. },
  177. initialize: function() {
  178. _.bindAll(this, 'render', 'selectItem', 'deselectItem')
  179. this.model.on("remove", this.close, this);
  180. },
  181. render: function() {
  182. var html = _.template(this.template, this.model.toJSON());
  183. this.$el.html(html);
  184. return this;
  185. },
  186. close: function() {
  187. this.$el.remove();
  188. },
  189. selectItem: function() {
  190. this.model.select();
  191. },
  192. deselectItem: function() {
  193. this.model.deselect();
  194. }
  195. });
  196. App.ListView = Backbone.View.extend({
  197. tagName: 'ul',
  198. className: 'overlay',
  199. id: 'listing',
  200. initialize: function() {
  201. _.bindAll(this, "refresh", "addChild");
  202. this.collection.on("reset", this.refresh, this);
  203. this.collection.on("add", this.addChild, this);
  204. this.$el.appendTo('body');
  205. },
  206. render: function() {
  207. this.collection.each(this.addChild);
  208. },
  209. addChild: function(childModel) {
  210. var childView = new App.ItemView({ model: childModel });
  211. childView.render().$el.appendTo(this.$el);
  212. },
  213. refresh: function() {
  214. this.$el.empty();
  215. this.render();
  216. }
  217. });
  218. $(document).ready(function() {
  219. App.init();
  220. $('#bars').click(function() {
  221. App.places.reset(bars);
  222. });
  223. $('#museums').click(function() {
  224. App.places.reset(museums);
  225. });
  226. $('#addBtn').click(function() {
  227. App.places.add({
  228. title: 'State Capitol Building',
  229. lat: 44.9543075,
  230. lng: -93.102222
  231. });
  232. });
  233. $('#removeBtn').click(function() {
  234. App.places.remove(App.places.at(0));
  235. });
  236. });
  237. </script>
  238. </body>
  239. </html>