map_coordinates.tpl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <div id="map" style="width:100%; height:400px;">
  2. </div>
  3. <script>
  4. $(document).ready(function() {
  5. initMap();
  6. });
  7. function initMap() {
  8. var center = new google.maps.LatLng(-3.480523, 7.866211);
  9. var bounds = new google.maps.LatLngBounds();
  10. var map = new google.maps.Map(document.getElementById("map"), {
  11. zoom: 2,
  12. center: center,
  13. mapTypeControl: true,
  14. mapTypeControlOptions: {
  15. style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
  16. },
  17. navigationControl: true,
  18. mapTypeId: google.maps.MapTypeId.ROADMAP
  19. });
  20. var extraFields = {{ extra_field_values_formatted|json_encode }};
  21. for (var i = 0; i < extraFields.length; i++) {
  22. var index = i + 1;
  23. for (var y = 0; y < extraFields[i].length; y++) {
  24. var address = extraFields[i][y]['address'];
  25. var userCompleteName = extraFields[i][y]['user_complete_name'];
  26. addMaker(address, map, bounds, userCompleteName, index);
  27. }
  28. }
  29. }
  30. function addMaker(address, map, bounds, userCompleteName, index) {
  31. if (index > 5) {
  32. return;
  33. }
  34. var infoWindow = new google.maps.InfoWindow();
  35. var geocoder = geocoder = new google.maps.Geocoder();
  36. var formattedAddress = '';
  37. geocoder.geocode({ 'address': address }, function (results, status) {
  38. if (status === google.maps.GeocoderStatus.OK) {
  39. if (results) {
  40. formattedAddress = results[0].formatted_address;
  41. } else {
  42. formattedAddress = '{{ 'Unknown' | get_lang }}';
  43. }
  44. var marker = new google.maps.Marker({
  45. map: map,
  46. position: results[0].geometry.location,
  47. label: userCompleteName
  48. });
  49. switch (index) {
  50. case 1:
  51. marker.setIcon('//maps.google.com/mapfiles/ms/icons/red-dot.png');
  52. break;
  53. case 2:
  54. marker.setIcon('//maps.google.com/mapfiles/ms/icons/blue-dot.png');
  55. break;
  56. case 3:
  57. marker.setIcon('//maps.google.com/mapfiles/ms/icons/green-dot.png');
  58. break;
  59. case 4:
  60. marker.setIcon('//maps.google.com/mapfiles/ms/icons/yellow-dot.png');
  61. break;
  62. case 5:
  63. marker.setIcon('//maps.google.com/mapfiles/ms/icons/purple-dot.png');
  64. break;
  65. }
  66. var infoWinContent = "<b>" + userCompleteName + "</b> - " + formattedAddress;
  67. marker.addListener('click', function() {
  68. infoWindow.setContent(infoWinContent);
  69. infoWindow.open(map, marker);
  70. });
  71. bounds.extend(marker.position);
  72. map.fitBounds(bounds);
  73. } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
  74. setTimeout(function() {
  75. addMaker(address, map, bounds, userCompleteName, index);
  76. }, 350);
  77. }
  78. });
  79. }
  80. </script>