demarches-normaliennes/app/assets/javascripts/carte/carte.js

122 lines
3 KiB
JavaScript
Raw Normal View History

var LON = '2.428462';
var LAT = '46.538192';
function initCarto() {
OSM = L.tileLayer("http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png", {
2015-11-12 10:51:53 +01:00
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
position = get_position() || default_position();
2015-11-25 10:26:55 +01:00
map = L.map("map", {
center: new L.LatLng(position.lat, position.lon),
zoom: position.zoom,
layers: [OSM]
});
if (qp_active())
display_qp(JSON.parse($("#quartier_prioritaires").val()));
if (cadastre_active())
display_cadastre(JSON.parse($("#cadastres").val()));
freeDraw = new L.FreeDraw();
freeDraw.options.setSmoothFactor(4);
freeDraw.options.simplifyPolygon = false;
map.addLayer(freeDraw);
if ($("#json_latlngs").val() != '' && $("#json_latlngs").val() != '[]') {
map.setZoom(18);
$.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());
}
else if (position.lat == LAT && position.lon == LON)
map.setView(new L.LatLng(position.lat, position.lon), 5);
2015-11-25 10:26:55 +01:00
add_event_freeDraw();
}
function default_position() {
return {lon: LON, lat: LAT, zoom: 13}
}
function get_external_data(latLngs) {
if (qp_active())
display_qp(get_qp(latLngs));
if (cadastre_active()) {
cadastre_list = [];
polygons = {"type": "FeatureCollection", "features": []};
for (i = 0; i < latLngs.length; i++)
polygons.features.push(feature_polygon_latLngs(latLngs[i]))
if (turf.area(polygons) < 300000)
cadastre_list = get_cadastre(latLngs);
else
cadastre_list = [{zoom_error: true}];
display_cadastre(cadastre_list);
}
}
function feature_polygon_latLngs(coordinates) {
return ({
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
JSON.parse(L.FreeDraw.Utilities.getJsonPolygons([coordinates]))['latLngs']
]
}
})
}
2015-11-25 10:26:55 +01:00
function add_event_freeDraw() {
freeDraw.on('markers', function (e) {
$("#json_latlngs").val(JSON.stringify(e.latLngs));
get_external_data(e.latLngs);
});
$("#new").on('click', function (e) {
freeDraw.setMode(L.FreeDraw.MODES.CREATE);
});
$("#edit").on('click', function (e) {
freeDraw.setMode(L.FreeDraw.MODES.EDIT);
});
$("#delete").on('click', function (e) {
freeDraw.setMode(L.FreeDraw.MODES.DELETE);
});
}
function get_position() {
2015-08-10 11:05:06 +02:00
var position;
$.ajax({
url: '/users/dossiers/' + dossier_id + '/carte/position',
2015-08-10 11:05:06 +02:00
dataType: 'json',
async: false
}).done(function (data) {
position = data
position.zoom = default_position().zoom
2015-08-10 11:05:06 +02:00
});
return position;
2015-11-25 10:26:55 +01:00
}
function jsObject_to_array(qp_list) {
return Object.keys(qp_list).map(function (v) {
return qp_list[v];
});
}