main.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. $( document ).ready(function() {
  2. // Create map
  3. var map = L.map('map').setView([47.218371,-1.553621], 13);
  4. L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  5. attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
  6. maxZoom: 18
  7. }).addTo(map);
  8. // Create marker
  9. var marker = L.marker([47.218371,-1.553621], {
  10. draggable: true
  11. }).addTo(map);
  12. // every time the marker is dragged, update the coordinates container
  13. marker.on('dragend', ondragend);
  14. // Set the initial marker coordinate on load.
  15. ondragend();
  16. function ondragend() {
  17. var m = marker.getLatLng();
  18. $('latitude').value = m.lat;
  19. $('longitude').value = m.lng;
  20. }
  21. // Search sub form
  22. $('#search-btn').click(function(e){
  23. e.preventDefault();
  24. // Geocoding
  25. var searchString = $('#search').val();
  26. $.getJSON('http://nominatim.openstreetmap.org/search?limit=5&format=json&q='+searchString, function(data){
  27. var items = [];
  28. $.each(data, function(key, val) {
  29. items.push(
  30. "<li class='list-group-item'><a href='#' data-lat='"+val.lat+"' data-lng='"+val.lon+"'>" + val.display_name + '</a></li>'
  31. );
  32. });
  33. $('#modal .modal-body').empty();
  34. if (items.length != 0) {
  35. $('<ul/>').addClass("list-group").html(items.join('')).appendTo('#modal .modal-body');
  36. } else {
  37. $('<p/>', { html: "Aucun résultat" }).appendTo('#modal .modal-body');
  38. }
  39. $('#modal').modal('show');
  40. // Bind click on results and update coordinates
  41. $('#modal .modal-body a').on('click', function(e){
  42. e.preventDefault();
  43. marker.setLatLng({lat:$(this).data('lat'), lng:$(this).data('lng')}).update();
  44. map.panTo({lat:$(this).data('lat'), lng:$(this).data('lng')});
  45. ondragend();
  46. $('#modal').modal('hide');
  47. });
  48. }); // getJSON
  49. }); // Search sub form
  50. // Contrib share dynamic form
  51. if ($('[name="contrib-type"]:checked').val() == 'share') { $('#contrib-type-share').show(); }
  52. else { $('#contrib-type-share').hide(); }
  53. // On change
  54. $('[name="contrib-type"]').change(function(e){
  55. if ($(this).val() == 'share') { $('#contrib-type-share').slideDown(); }
  56. else { $('#contrib-type-share').slideUp(); }
  57. });
  58. });