gmaps-scripts.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. var GoogleMaps = function () {
  2. var mapBasic = function () {
  3. new GMaps({
  4. div: '#gmap_basic',
  5. lat: -12.043333,
  6. lng: -77.028333
  7. });
  8. }
  9. var mapMarker = function () {
  10. var map = new GMaps({
  11. div: '#gmap_marker',
  12. lat: -12.043333,
  13. lng: -77.028333
  14. });
  15. map.addMarker({
  16. lat: -12.043333,
  17. lng: -77.03,
  18. title: 'Lima',
  19. details: {
  20. database_id: 42,
  21. author: 'HPNeo'
  22. },
  23. click: function (e) {
  24. if (console.log) console.log(e);
  25. alert('You clicked in this marker');
  26. }
  27. });
  28. map.addMarker({
  29. lat: -12.042,
  30. lng: -77.028333,
  31. title: 'Marker with InfoWindow',
  32. infoWindow: {
  33. content: 'HTML Content!!!!'
  34. }
  35. });
  36. }
  37. var mapPolylines = function() {
  38. var map = new GMaps({
  39. div: '#gmap_polylines',
  40. lat: -12.043333,
  41. lng: -77.028333,
  42. click: function(e){
  43. console.log(e);
  44. }
  45. });
  46. path = [[-12.044012922866312, -77.02470665341184], [-12.05449279282314, -77.03024273281858], [-12.055122327623378, -77.03039293652341], [-12.075917129727586, -77.02764635449216], [-12.07635776902266, -77.02792530422971], [-12.076819390363665, -77.02893381481931], [-12.088527520066453, -77.0241058385925], [-12.090814532191756, -77.02271108990476]];
  47. map.drawPolyline({
  48. path: path,
  49. strokeColor: '#131540',
  50. strokeOpacity: 0.6,
  51. strokeWeight: 6
  52. });
  53. }
  54. var mapGeolocation = function() {
  55. var map = new GMaps({
  56. div: '#gmap_geo',
  57. lat: -12.043333,
  58. lng: -77.028333
  59. });
  60. GMaps.geolocate({
  61. success: function(position) {
  62. map.setCenter(position.coords.latitude, position.coords.longitude);
  63. },
  64. error: function(error) {
  65. alert('Geolocation failed: '+error.message);
  66. },
  67. not_supported: function() {
  68. alert("Your browser does not support geolocation");
  69. },
  70. always: function() {
  71. //alert("Geolocation Done!");
  72. }
  73. });
  74. }
  75. var mapGeocoding = function() {
  76. var map = new GMaps({
  77. div: '#gmap_geocoding',
  78. lat: -12.043333,
  79. lng: -77.028333
  80. });
  81. var handleAction = function() {
  82. var text = $.trim($('#gmap_geocoding_address').val());
  83. GMaps.geocode({
  84. address: text,
  85. callback: function(results, status){
  86. if(status=='OK'){
  87. var latlng = results[0].geometry.location;
  88. map.setCenter(latlng.lat(), latlng.lng());
  89. map.addMarker({
  90. lat: latlng.lat(),
  91. lng: latlng.lng()
  92. });
  93. App.scrollTo($('#gmap_geocoding'));
  94. }
  95. }
  96. });
  97. }
  98. $('#gmap_geocoding_btn').click(function(e){
  99. e.preventDefault();
  100. handleAction();
  101. });
  102. $("#gmap_geocoding_address").keypress(function(e){
  103. var keycode = (e.keyCode ? e.keyCode : e.which);
  104. if(keycode == '13') {
  105. e.preventDefault();
  106. handleAction();
  107. }
  108. });
  109. }
  110. var mapPolygone = function() {
  111. var map = new GMaps({
  112. div: '#gmap_polygons',
  113. lat: -12.043333,
  114. lng: -77.028333
  115. });
  116. var path = [[-12.040397656836609,-77.03373871559225], [-12.040248585302038,-77.03993927003302],
  117. [-12.050047116528843,-77.02448169303511],
  118. [-12.044804866577001,-77.02154422636042]];
  119. var polygon = map.drawPolygon({
  120. paths: path,
  121. strokeColor: '#BBD8E9',
  122. strokeOpacity: 1,
  123. strokeWeight: 3,
  124. fillColor: '#BBD8E9',
  125. fillOpacity: 0.6
  126. });
  127. }
  128. var mapRoutes = function() {
  129. var map = new GMaps({
  130. div: '#gmap_routes',
  131. lat: -12.043333,
  132. lng: -77.028333
  133. });
  134. $('#gmap_routes_start').click(function(e){
  135. e.preventDefault();
  136. map.travelRoute({
  137. origin: [-12.044012922866312, -77.02470665341184],
  138. destination: [-12.090814532191756, -77.02271108990476],
  139. travelMode: 'driving',
  140. step: function(e){
  141. $('#gmap_routes_instructions').append('<li>'+e.instructions+'</li>');
  142. $('#gmap_routes_instructions li:eq('+e.step_number+')').delay(800*e.step_number).fadeIn(500, function(){
  143. map.setCenter(e.end_location.lat(), e.end_location.lng());
  144. map.drawPolyline({
  145. path: e.path,
  146. strokeColor: '#131540',
  147. strokeOpacity: 0.6,
  148. strokeWeight: 6
  149. });
  150. App.scrollTo($(this));
  151. });
  152. }
  153. });
  154. });
  155. }
  156. return {
  157. //main function to initiate map samples
  158. init: function () {
  159. mapBasic();
  160. mapMarker();
  161. mapGeolocation();
  162. mapGeocoding();
  163. mapPolylines();
  164. mapPolygone();
  165. mapRoutes();
  166. }
  167. };
  168. }();