Ajout de lieu presque fonctionnel

This commit is contained in:
Evarin 2017-04-17 00:00:41 +02:00
parent 8550776fab
commit 3b030aef70
5 changed files with 127 additions and 30 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View file

@ -11,6 +11,7 @@
<link rel="stylesheet" type="text/css" href="{% static "css/leaflet.css" %}" /> <link rel="stylesheet" type="text/css" href="{% static "css/leaflet.css" %}" />
{% endblock %} {% endblock %}
<p><input id="addlieu" type="text" placeholder="Chercher un établissement" /></p>
<div id="map_addlieu"></div> <div id="map_addlieu"></div>
<div id="candidats"></div> <div id="candidats"></div>
@ -29,41 +30,137 @@
<input type="submit" /> <input type="submit" />
<script type="text/javascript"> <script type="text/javascript">
$(function(){ $(function(){
var map = L.map('map_addlieu').setView([51.505, -0.09], 13); var STATIC_ROOT = "{{ STATIC_URL }}";
// Affiche la carte
var map = L.map('map_addlieu').setView([48.8422411,2.3430553], 13);
var layer = new L.StamenTileLayer("terrain"); var layer = new L.StamenTileLayer("terrain");
map.addLayer(layer); map.addLayer(layer);
var mainmarker;
var propositions = [];
new L.Control.GPlaceAutocomplete({ // Autocomplete
position: "topleft", var input = document.getElementById('addlieu');
callback: function(place){ var autocomplete = new google.maps.places.Autocomplete(input);
// object of google place is given autocomplete.setTypes(["geocode", "establishment"]);
var location = {lat: place.geometry.location.lat(), autocomplete.addListener('place_changed', handlePlaceSearch);
lng: place.geometry.location.lng()};
map.panTo(location); // Préparation de la suite
if(mainmarker===undefined) var mainmarker;
mainmarker = L.marker(location, {draggable:true}).addTo(map); var suggestion_actuelle;
else var propositions_data = [];
mainmarker.setLatLng(location); var propositions_markers = [];
var choix_actuel;
// Icones
var iconProps = {
iconSize: [36, 46],
iconAnchor: [18, 45],
popupAnchor: [0, -48]
};
var greenIcon = L.icon($.extend({}, iconProps, {iconUrl: STATIC_ROOT + 'images/marker-green.png'}));
var redIcon = L.icon($.extend({}, iconProps, {iconUrl: STATIC_ROOT + 'images/marker-red.png'}));
var blueIcon = L.icon($.extend({}, iconProps, {iconUrl: STATIC_ROOT + 'images/marker-blue.png'}));
// Callback autocomplete
function handlePlaceSearch() {
var place = autocomplete.getPlace();
if (!place.geometry) {
window.alert("No details available for input: '" + place.name + "'");
return;
}
// Processing du lieu
var data = {};
$.each(place.address_components, function(i, obj) {
for (var j=0; j<obj.types.length; j++) {
switch(obj.types[j]) {
case "locality":
data.ville = obj.long_name;
break;
case "country":
data.pays = obj.short_name;
break;
}
}
});
data.nom = place.name;
data.coord = {lng: place.geometry.location.lng(),
lat: place.geometry.location.lat()};
data.orig_coord = data.coord;
suggestion_actuelle = data;
console.log(place); console.log(place);
var location = data.coord;
map.panTo(location);
if(mainmarker===undefined) {
mainmarker = L.marker(location,
{draggable: true,
icon: redIcon}).addTo(map);
mainmarker.on("dragend", refreshSuggestedPosition);
} else
mainmarker.setLatLng(location);
mainmarker.bindPopup(popupText(data, ""));
// Affichage des suggestions
$.getJSON("/api/v1/lieu/", {"format":"json", $.getJSON("/api/v1/lieu/", {"format":"json",
"lat":location.lat, "lat":location.lat,
"lng":location.lng}, showPropositions); "lng":location.lng}, showPropositions);
}
})
.addTo(map);
}
// Callback suggestions
function showPropositions(data) { function showPropositions(data) {
$.each(propositions, function(i, item) { item.remove(); }); // TODO gérer les appels concurrents
propositions = []; $.each(propositions_markers, function(i, item) { item.remove(); });
propositions_markers = [];
propositions_data = data.objects;
$.each(data.objects, function(i, item) { $.each(data.objects, function(i, item) {
var mark = L.marker([item.coord.lat, item.coord.lng]); var mark = L.marker([item.coord.lat, item.coord.lng],
mark.addTo(map).bindPopup("<b>"+item.nom+"</b><br />"+ {icon: blueIcon});
item.ville+", "+item.pays); mark.addTo(map).bindPopup(popupText(item, i));
propositions.push(mark); propositions_markers.push(mark);
}); });
} }
function popupText(data, ilieu) {
var desc = "<div><b>" + data.nom +"</b><br />"+data.ville+", "+data.pays+"<br />";
if(ilieu !== "") {
desc += "<a href=\"javascript:choixLieuStage("+ilieu+")\">Choisir ce lieu</a></div>"
} else {
desc += "<a href=\"javascript:restoreSuggestedPosition()\">Restaurer la position originale</a><br/>"
desc += "<a href=\"javascript:choixLieuStage()\">Créer un nouveau lieu</a></div>"
}
return desc;
}
function refreshSuggestedPosition() {
var coord = mainmarker.getLatLng();
suggestion_actuelle.coord = {lat:coord.lat, lng:coord.lng};
if (choix_actuel == suggestion_actuelle) {
$("#id_coord_0").val(choix_actuel.coord.lat);
$("#id_coord_1").val(choix_actuel.coord.lng);
}
}
window.restoreSuggestedPosition = function() {
mainmarker.setLatLng(suggestion_actuelle.orig_coord);
refreshSuggestedPosition();
}
window.choixLieuStage = function(ilieu) {
var choix;
if(ilieu === undefined)
choix = suggestion_actuelle;
else
choix = propositions_data[ilieu];
$("#id_nom").val(choix.nom);
$("#id_ville").val(choix.ville);
$("#id_pays").val(choix.pays);
$("#id_coord_0").val(choix.coord.lat);
$("#id_coord_1").val(choix.coord.lng);
choix_actuel = choix;
}
}); });
</script> </script>