No Description

google-maps-admin.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Integration for Google Maps in the django admin.
  3. How it works:
  4. You have an address field on the page.
  5. Enter an address and an on change event will update the map
  6. with the address. A marker will be placed at the address.
  7. If the user needs to move the marker, they can and the geolocation
  8. field will be updated.
  9. Only one marker will remain present on the map at a time.
  10. This script expects:
  11. <input type="text" name="address" id="id_address" />
  12. <input type="text" name="geolocation" id="id_geolocation" />
  13. <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
  14. */
  15. function googleMapAdmin() {
  16. var autocomplete;
  17. var geocoder = new google.maps.Geocoder();
  18. var map;
  19. var marker;
  20. var geolocationId = 'id_geolocation';
  21. var addressId = 'id_address';
  22. var self = {
  23. initialize: function() {
  24. var lat = 0;
  25. var lng = 0;
  26. var zoom = 2;
  27. // set up initial map to be world view. also, add change
  28. // event so changing address will update the map
  29. var existinglocation = self.getExistingLocation();
  30. if (existinglocation) {
  31. lat = existinglocation[0];
  32. lng = existinglocation[1];
  33. zoom = 18;
  34. }
  35. var latlng = new google.maps.LatLng(lat,lng);
  36. var myOptions = {
  37. zoom: zoom,
  38. center: latlng,
  39. mapTypeId: self.getMapType()
  40. };
  41. map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  42. if (existinglocation) {
  43. self.setMarker(latlng);
  44. }
  45. autocomplete = new google.maps.places.Autocomplete(
  46. /** @type {!HTMLInputElement} */(document.getElementById(addressId)),
  47. self.getAutoCompleteOptions());
  48. // this only triggers on enter, or if a suggested location is chosen
  49. // todo: if a user doesn't choose a suggestion and presses tab, the map doesn't update
  50. autocomplete.addListener("place_changed", self.codeAddress);
  51. // don't make enter submit the form, let it just trigger the place_changed event
  52. // which triggers the map update & geocode
  53. $("#" + addressId).keydown(function (e) {
  54. if (e.keyCode == 13) { // enter key
  55. e.preventDefault();
  56. return false;
  57. }
  58. });
  59. },
  60. getMapType : function() {
  61. // https://developers.google.com/maps/documentation/javascript/maptypes
  62. var geolocation = document.getElementById(addressId);
  63. var allowedType = ['roadmap', 'satellite', 'hybrid', 'terrain'];
  64. var mapType = geolocation.getAttribute('data-map-type');
  65. if (mapType && -1 !== allowedType.indexOf(mapType)) {
  66. return mapType;
  67. }
  68. return google.maps.MapTypeId.HYBRID;
  69. },
  70. getAutoCompleteOptions : function() {
  71. var geolocation = document.getElementById(addressId);
  72. var autocompleteOptions = geolocation.getAttribute('data-autocomplete-options');
  73. if (!autocompleteOptions) {
  74. return {
  75. types: ['geocode']
  76. };
  77. }
  78. return JSON.parse(autocompleteOptions);
  79. },
  80. getExistingLocation: function() {
  81. var geolocation = document.getElementById(geolocationId).value;
  82. if (geolocation) {
  83. return geolocation.split(',');
  84. }
  85. },
  86. codeAddress: function() {
  87. var place = autocomplete.getPlace();
  88. if(place.geometry !== undefined) {
  89. self.updateWithCoordinates(place.geometry.location);
  90. }
  91. else {
  92. geocoder.geocode({'address': place.name}, function(results, status) {
  93. if (status == google.maps.GeocoderStatus.OK) {
  94. var latlng = results[0].geometry.location;
  95. self.updateWithCoordinates(latlng);
  96. } else {
  97. alert("Geocode was not successful for the following reason: " + status);
  98. }
  99. });
  100. }
  101. },
  102. updateWithCoordinates: function(latlng) {
  103. map.setCenter(latlng);
  104. map.setZoom(18);
  105. self.setMarker(latlng);
  106. self.updateGeolocation(latlng);
  107. },
  108. setMarker: function(latlng) {
  109. if (marker) {
  110. self.updateMarker(latlng);
  111. } else {
  112. self.addMarker({'latlng': latlng, 'draggable': true});
  113. }
  114. },
  115. addMarker: function(Options) {
  116. marker = new google.maps.Marker({
  117. map: map,
  118. position: Options.latlng
  119. });
  120. var draggable = Options.draggable || false;
  121. if (draggable) {
  122. self.addMarkerDrag(marker);
  123. }
  124. },
  125. addMarkerDrag: function() {
  126. marker.setDraggable(true);
  127. google.maps.event.addListener(marker, 'dragend', function(new_location) {
  128. self.updateGeolocation(new_location.latLng);
  129. });
  130. },
  131. updateMarker: function(latlng) {
  132. marker.setPosition(latlng);
  133. },
  134. updateGeolocation: function(latlng) {
  135. document.getElementById(geolocationId).value = latlng.lat() + "," + latlng.lng();
  136. $("#" + geolocationId).trigger('change');
  137. }
  138. };
  139. return self;
  140. }
  141. $(document).ready(function() {
  142. var googlemap = googleMapAdmin();
  143. googlemap.initialize();
  144. });