maps-google.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. var MapsGoogle = 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: '<span style="color:#000">HTML Content!</span>'
  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 = [
  47. [-12.044012922866312, -77.02470665341184],
  48. [-12.05449279282314, -77.03024273281858],
  49. [-12.055122327623378, -77.03039293652341],
  50. [-12.075917129727586, -77.02764635449216],
  51. [-12.07635776902266, -77.02792530422971],
  52. [-12.076819390363665, -77.02893381481931],
  53. [-12.088527520066453, -77.0241058385925],
  54. [-12.090814532191756, -77.02271108990476]
  55. ];
  56. map.drawPolyline({
  57. path: path,
  58. strokeColor: '#131540',
  59. strokeOpacity: 0.6,
  60. strokeWeight: 6
  61. });
  62. }
  63. var mapGeolocation = function () {
  64. var map = new GMaps({
  65. div: '#gmap_geo',
  66. lat: -12.043333,
  67. lng: -77.028333
  68. });
  69. GMaps.geolocate({
  70. success: function (position) {
  71. map.setCenter(position.coords.latitude, position.coords.longitude);
  72. },
  73. error: function (error) {
  74. alert('Geolocation failed: ' + error.message);
  75. },
  76. not_supported: function () {
  77. alert("Your browser does not support geolocation");
  78. },
  79. always: function () {
  80. //alert("Geolocation Done!");
  81. }
  82. });
  83. }
  84. var mapGeocoding = function () {
  85. var map = new GMaps({
  86. div: '#gmap_geocoding',
  87. lat: -12.043333,
  88. lng: -77.028333
  89. });
  90. var handleAction = function () {
  91. var text = $.trim($('#gmap_geocoding_address').val());
  92. GMaps.geocode({
  93. address: text,
  94. callback: function (results, status) {
  95. if (status == 'OK') {
  96. var latlng = results[0].geometry.location;
  97. map.setCenter(latlng.lat(), latlng.lng());
  98. map.addMarker({
  99. lat: latlng.lat(),
  100. lng: latlng.lng()
  101. });
  102. App.scrollTo($('#gmap_geocoding'));
  103. }
  104. }
  105. });
  106. }
  107. $('#gmap_geocoding_btn').click(function (e) {
  108. e.preventDefault();
  109. handleAction();
  110. });
  111. $("#gmap_geocoding_address").keypress(function (e) {
  112. var keycode = (e.keyCode ? e.keyCode : e.which);
  113. if (keycode == '13') {
  114. e.preventDefault();
  115. handleAction();
  116. }
  117. });
  118. }
  119. var mapPolygone = function () {
  120. var map = new GMaps({
  121. div: '#gmap_polygons',
  122. lat: -12.043333,
  123. lng: -77.028333
  124. });
  125. var path = [
  126. [-12.040397656836609, -77.03373871559225],
  127. [-12.040248585302038, -77.03993927003302],
  128. [-12.050047116528843, -77.02448169303511],
  129. [-12.044804866577001, -77.02154422636042]
  130. ];
  131. var polygon = map.drawPolygon({
  132. paths: path,
  133. strokeColor: '#BBD8E9',
  134. strokeOpacity: 1,
  135. strokeWeight: 3,
  136. fillColor: '#BBD8E9',
  137. fillOpacity: 0.6
  138. });
  139. }
  140. var mapRoutes = function () {
  141. var map = new GMaps({
  142. div: '#gmap_routes',
  143. lat: -12.043333,
  144. lng: -77.028333
  145. });
  146. $('#gmap_routes_start').click(function (e) {
  147. e.preventDefault();
  148. App.scrollTo($(this), 400);
  149. map.travelRoute({
  150. origin: [-12.044012922866312, -77.02470665341184],
  151. destination: [-12.090814532191756, -77.02271108990476],
  152. travelMode: 'driving',
  153. step: function (e) {
  154. $('#gmap_routes_instructions').append('<li>' + e.instructions + '</li>');
  155. $('#gmap_routes_instructions li:eq(' + e.step_number + ')').delay(800 * e.step_number).fadeIn(500, function () {
  156. map.setCenter(e.end_location.lat(), e.end_location.lng());
  157. map.drawPolyline({
  158. path: e.path,
  159. strokeColor: '#131540',
  160. strokeOpacity: 0.6,
  161. strokeWeight: 6
  162. });
  163. });
  164. }
  165. });
  166. });
  167. }
  168. return {
  169. //main function to initiate map samples
  170. init: function () {
  171. mapBasic();
  172. mapMarker();
  173. mapGeolocation();
  174. mapGeocoding();
  175. mapPolylines();
  176. mapPolygone();
  177. mapRoutes();
  178. }
  179. };
  180. }();