main.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. var btn = $(this).button('loading');
  25. // Geocoding
  26. var searchString = $('#search').val();
  27. $.getJSON('http://nominatim.openstreetmap.org/search?limit=5&format=json&q='+searchString, function(data){
  28. var items = [];
  29. $.each(data, function(key, val) {
  30. items.push(
  31. "<li class='list-group-item'><a href='#' data-lat='"+val.lat+"' data-lng='"+val.lon+"'>" + val.display_name + '</a></li>'
  32. );
  33. });
  34. $('#modal .modal-body').empty();
  35. if (items.length != 0) {
  36. $('<ul/>').addClass("list-group").html(items.join('')).appendTo('#modal .modal-body');
  37. } else {
  38. $('<p/>', { html: "Aucun résultat" }).appendTo('#modal .modal-body');
  39. }
  40. $('#modal').modal('show');
  41. // Bind click on results and update coordinates
  42. $('#modal .modal-body a').on('click', function(e){
  43. e.preventDefault();
  44. marker.setLatLng({lat:$(this).data('lat'), lng:$(this).data('lng')}).update();
  45. map.panTo({lat:$(this).data('lat'), lng:$(this).data('lng')});
  46. ondragend();
  47. $('#modal').modal('hide');
  48. });
  49. btn.button('reset');
  50. }); // getJSON
  51. }); // Search sub form
  52. // Contrib share dynamic form
  53. if ($('[name="contrib-type"]:checked').val() == 'share') { $('#contrib-type-share').show(); }
  54. else { $('#contrib-type-share').hide(); }
  55. // On change
  56. $('[name="contrib-type"]').change(function(e){
  57. if ($(this).val() == 'share') { $('#contrib-type-share').slideDown(); }
  58. else { $('#contrib-type-share').slideUp(); }
  59. });
  60. });