2015-12-10 14:33:09 +01:00
|
|
|
var LON = '2.428462';
|
|
|
|
var LAT = '46.538192';
|
|
|
|
|
2015-11-10 18:08:47 +01:00
|
|
|
function initCarto() {
|
|
|
|
OSM = L.tileLayer("http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png", {
|
2015-11-12 10:51:53 +01:00
|
|
|
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
|
2015-11-10 18:08:47 +01:00
|
|
|
});
|
|
|
|
|
2015-12-10 14:33:09 +01:00
|
|
|
position = get_position() || default_position();
|
2015-11-19 16:47:56 +01:00
|
|
|
|
2015-12-10 14:33:09 +01:00
|
|
|
if (position.lon == "0" && position.lat == "0")
|
|
|
|
position = default_position();
|
2015-11-19 16:47:56 +01:00
|
|
|
|
|
|
|
if (typeof position.zoom == 'undefined')
|
|
|
|
position.zoom = 13;
|
2015-11-10 18:08:47 +01:00
|
|
|
|
2015-11-25 10:26:55 +01:00
|
|
|
map = L.map("map", {
|
2015-11-10 18:08:47 +01:00
|
|
|
center: new L.LatLng(position.lat, position.lon),
|
2015-11-19 16:47:56 +01:00
|
|
|
zoom: position.zoom,
|
2015-11-10 18:08:47 +01:00
|
|
|
layers: [OSM]
|
|
|
|
});
|
|
|
|
|
2015-12-08 11:18:49 +01:00
|
|
|
freeDraw = new L.FreeDraw();
|
2015-11-10 18:08:47 +01:00
|
|
|
map.addLayer(freeDraw);
|
2015-11-12 17:34:12 +01:00
|
|
|
|
2015-11-19 16:47:56 +01:00
|
|
|
if ($("#json_latlngs").val() != '' && $("#json_latlngs").val() != '[]') {
|
|
|
|
$.each($.parseJSON($("#json_latlngs").val()), function (i, val) {
|
|
|
|
freeDraw.createPolygon(val);
|
|
|
|
});
|
2015-11-19 17:20:22 +01:00
|
|
|
|
|
|
|
map.fitBounds(freeDraw.polygons[0].getBounds());
|
2015-11-19 16:47:56 +01:00
|
|
|
}
|
|
|
|
else if (position.lat == LAT && position.lon == LON)
|
|
|
|
map.setView(new L.LatLng(position.lat, position.lon), 5);
|
2015-11-12 17:34:12 +01:00
|
|
|
|
2015-11-25 10:26:55 +01:00
|
|
|
add_event_freeDraw();
|
2015-11-25 17:05:14 +01:00
|
|
|
|
2015-12-08 11:18:49 +01:00
|
|
|
if (qp_active())
|
|
|
|
display_qp(JSON.parse($("#quartier_prioritaires").val()));
|
2015-11-12 17:34:12 +01:00
|
|
|
}
|
|
|
|
|
2015-12-10 14:33:09 +01:00
|
|
|
function default_position (){
|
|
|
|
return {lon: LON, lat: LAT, zoom: 13}
|
|
|
|
}
|
|
|
|
|
2015-11-25 10:26:55 +01:00
|
|
|
function add_event_freeDraw() {
|
2015-11-19 16:47:56 +01:00
|
|
|
freeDraw.on('markers', function (e) {
|
2015-11-12 17:34:12 +01:00
|
|
|
$("#json_latlngs").val(JSON.stringify(e.latLngs));
|
2015-12-08 11:18:49 +01:00
|
|
|
|
|
|
|
if (qp_active())
|
|
|
|
display_qp(get_qp(e.latLngs));
|
2015-11-12 17:34:12 +01:00
|
|
|
});
|
|
|
|
|
2015-11-19 16:47:56 +01:00
|
|
|
$("#new").on('click', function (e) {
|
2015-11-12 17:34:12 +01:00
|
|
|
freeDraw.setMode(L.FreeDraw.MODES.CREATE);
|
|
|
|
});
|
|
|
|
|
2015-11-19 16:47:56 +01:00
|
|
|
$("#edit").on('click', function (e) {
|
2015-11-12 17:34:12 +01:00
|
|
|
freeDraw.setMode(L.FreeDraw.MODES.EDIT);
|
|
|
|
});
|
|
|
|
|
2015-11-19 16:47:56 +01:00
|
|
|
$("#delete").on('click', function (e) {
|
2015-11-12 17:34:12 +01:00
|
|
|
freeDraw.setMode(L.FreeDraw.MODES.DELETE);
|
|
|
|
});
|
2015-11-10 18:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_position() {
|
2015-08-10 11:05:06 +02:00
|
|
|
var position;
|
|
|
|
|
|
|
|
$.ajax({
|
2015-11-10 18:08:47 +01:00
|
|
|
url: '/users/dossiers/' + dossier_id + '/carte/position',
|
2015-08-10 11:05:06 +02:00
|
|
|
dataType: 'json',
|
|
|
|
async: false
|
|
|
|
}).done(function (data) {
|
|
|
|
position = data
|
|
|
|
});
|
|
|
|
|
|
|
|
return position;
|
2015-11-25 10:26:55 +01:00
|
|
|
}
|
|
|
|
|
2015-12-08 11:18:49 +01:00
|
|
|
function qp_active() {
|
|
|
|
return $("#map.qp").length > 0
|
|
|
|
}
|
|
|
|
|
2015-11-25 10:26:55 +01:00
|
|
|
function get_qp(coordinates) {
|
2015-12-08 11:18:49 +01:00
|
|
|
if (!qp_active())
|
|
|
|
return;
|
|
|
|
|
2015-11-25 10:26:55 +01:00
|
|
|
var qp;
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
method: 'post',
|
|
|
|
url: '/users/dossiers/' + dossier_id + '/carte/qp',
|
|
|
|
data: {coordinates: JSON.stringify(coordinates)},
|
|
|
|
dataType: 'json',
|
|
|
|
async: false
|
|
|
|
}).done(function (data) {
|
|
|
|
qp = data
|
|
|
|
});
|
|
|
|
|
2015-12-08 11:18:49 +01:00
|
|
|
return qp['quartier_prioritaires'];
|
2015-11-25 10:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function display_qp(qp_list) {
|
2015-12-08 11:18:49 +01:00
|
|
|
if (!qp_active())
|
|
|
|
return;
|
|
|
|
|
2015-11-25 10:26:55 +01:00
|
|
|
qp_array = jsObject_to_array(qp_list);
|
|
|
|
|
|
|
|
$("#qp_list ul").html('');
|
|
|
|
|
|
|
|
new_qpLayer();
|
|
|
|
|
|
|
|
if (qp_array.length > 0) {
|
|
|
|
qp_array.forEach(function (qp) {
|
|
|
|
$("#qp_list ul").append('<li>' + qp.commune + ' : ' + qp.nom + '</li>');
|
2015-11-25 17:05:14 +01:00
|
|
|
|
|
|
|
qpItems.addData(qp.geometry);
|
2015-11-25 10:26:55 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$("#qp_list ul").html('<li>AUCUN</li>');
|
|
|
|
}
|
|
|
|
|
|
|
|
function new_qpLayer() {
|
|
|
|
if (typeof qpItems != 'undefined')
|
|
|
|
map.removeLayer(qpItems);
|
|
|
|
|
|
|
|
qpItems = new L.GeoJSON();
|
|
|
|
qpItems.addTo(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
function jsObject_to_array(qp_list) {
|
|
|
|
return Object.keys(qp_list).map(function (v) {
|
|
|
|
return qp_list[v];
|
|
|
|
});
|
|
|
|
}
|