You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
9.5 KiB

3 years ago
let currentGeoJson = "";
let actionLocationAc = "add";
let lat = -6.228000;
let long = 106.559242;
let zoom = 10;
let currentIdAct = 0;
let map = L.map('map_activity').setView([lat, long], zoom);
1 year ago
let drawnItems = new L.FeatureGroup().addTo(map);
1 year ago
let inter = 5;
3 years ago
1 year ago
let circLat = "",
circLong = "";
3 years ago
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var drawControl = new L.Control.Draw({
draw: {
circlemarker: false,
1 year ago
circle: false,
3 years ago
polyline: false
},
});
var drawnControlEdit = new L.Control.Draw({
draw: false,
1 year ago
edit: {
featureGroup: drawnItems,
delete: true
3 years ago
}
});
map.addControl(drawControl);
// add searching location (nominatim) on map
L.Control.geocoder().addTo(map);
1 year ago
function addDrawnLayer(layer) {
drawnItems.addLayer(layer);
}
1 year ago
function updateCurrentGeoJson() {
let shapes = [];
drawnItems.eachLayer(function (layer) {
let geojson = layer.toGeoJSON();
if (layer instanceof L.Circle) {
geojson.properties.radius = layer.getRadius();
}
1 year ago
shapes.push(geojson);
});
currentGeoJson = {
type: 'FeatureCollection',
features: shapes
};
}
1 year ago
function openActivityMap(id) {
3 years ago
currentIdAct = id;
map.invalidateSize();
$.ajax({
url: `${base_url}task/edit/${id}`,
1 year ago
type: "get",
3 years ago
success: function (data) {
let dataRes = data.data
1 year ago
drawnItems.clearLayers();
1 year ago
if (dataRes.geom) {
3 years ago
let datageojson = JSON.parse(dataRes.geom);
currentGeoJson = datageojson
1 year ago
if (datageojson.type === 'FeatureCollection') {
datageojson.features.forEach(feature => {
let lGeoJson = L.geoJSON(feature);
let layers = lGeoJson.getLayers();
layers.forEach(layer => {
let radius = layer.feature.properties.radius;
if (radius) {
let latlon = layer.getLatLng();
1 year ago
let circle = L.circle([latlon.lat, latlon.lng], { radius: radius });
addDrawnLayer(circle);
}
else {
addDrawnLayer(layer)
}
});
1 year ago
});
} else {
1 year ago
if (datageojson.properties.radius) {
let radius = datageojson.properties.radius
let lGeoJson = L.geoJson(datageojson);
let layers = lGeoJson.getLayers();
let latlon = layers[0].getLatLng();
let newCircle = L.circle([latlon.lat, latlon.lng], { radius: radius });
addDrawnLayer(newCircle)
} else {
let lGeoJson = L.geoJson(datageojson);
let layers = lGeoJson.getLayers();
addDrawnLayer(layers[0])
}
3 years ago
}
actionLocationAc = "edit";
map.addControl(drawnControlEdit);
1 year ago
} else {
3 years ago
currentGeoJson = "";
actionLocationAc = "add";
drawnControlEdit.remove();
map.addControl(drawControl);
}
$("#modal_activity_location").modal("show");
},
error: function (data) {
gantt.alert("Get Activity Location Failed, try again later!");
}
});
}
$('#modal_activity_location').on('hide.bs.modal', function (event) {
1 year ago
currentGeoJson = ""
3 years ago
currentIdAct = 0;
actionLocationAc = "add";
drawnControlEdit.remove();
map.addControl(drawControl);
map.setView([lat, long], zoom);
});
// map initialization
$(document).ready(function () {
let intervalMap = setInterval(function () {
1 year ago
if (inter = 0) {
3 years ago
clearInterval(intervalMap)
}
map.invalidateSize();
inter--;
}, 1000);
map.on(L.Draw.Event.CREATED, function (e) {
var type = e.layerType,
layer = e.layer;
if (type === 'marker') {
let latlong = layer.getLatLng();
1 year ago
circLat = latlong.lat
3 years ago
circLong = latlong.lng
$("#modal_radius").modal("show");
// Do marker specific actions
// let attri = layer.getAttribution();
return false;
}
let geJson = layer.toGeoJSON();
currentGeoJson = geJson;
// let lgeojson = L.geoJson(geJson)
// layer.bindTooltip("cek");
// Do whatever else you need to. (save to db; add to map etc)
1 year ago
addDrawnLayer(layer);
updateCurrentGeoJson();
3 years ago
map.addControl(drawnControlEdit);
// map.addLayer(layer);
});
map.on(L.Draw.Event.DELETED, function (e) {
1 year ago
var type = e.layerType;
let layers = e.layers.getLayers();
layers.forEach(layer => {
drawnItems.removeLayer(layer);
});
updateCurrentGeoJson()
3 years ago
// Do whatever else you need to. (save to db; add to map etc)
1 year ago
3 years ago
// map.addLayer(layer);
});
map.on(L.Draw.Event.EDITED, function (e) {
// var type = e.layerType,
// layer = e.layer;
let layers = e.layers.getLayers();
1 year ago
layers.forEach(layer => {
1 year ago
let geoJson = layer.toGeoJSON();
if (layer instanceof L.Circle) {
geoJson.properties.radius = layer.getRadius();
}
updateCurrentGeoJson();
1 year ago
});
3 years ago
// Do whatever else you need to. (save to db; add to map etc)
// drawnControlEdit.remove();
// map.addControl(drawControl);
// map.addLayer(layer);
});
1 year ago
$("#modal_activity_location").on("click", "#btn_save_location", function () {
if (currentGeoJson == "") {
3 years ago
gantt.confirm({
text: "Lokasi belum ditentukan!, lanjutkan?",
1 year ago
ok: "Submit",
cancel: "Cancel",
callback: function (result) {
if (!result) {
3 years ago
return false
1 year ago
} else {
1 year ago
submitShapes();
3 years ago
}
}
});
1 year ago
} else {
1 year ago
submitShapes();
3 years ago
}
});
1 year ago
function submitShapes() {
let payload
1 year ago
if (currentGeoJson && currentGeoJson != "") {
1 year ago
payload = {
geom: JSON.stringify(currentGeoJson),
}
1 year ago
} else {
1 year ago
payload = {
geom: null,
}
}
$.ajax({
1 year ago
data: JSON.stringify(payload),
1 year ago
url: `${base_url}task/update-regular/${currentIdAct}`,
type: "PUT",
processData: false,
contentType: false,
success: function (data) {
$("#modal_activity_location").modal("hide");
1 year ago
currentGeoJson = ""
1 year ago
currentIdAct = 0;
gantt.alert("Activity Location successfully updated!");
},
error: function (data) {
$("#modal_activity_location").modal("hide");
1 year ago
currentGeoJson = ""
1 year ago
currentIdAct = 0;
gantt.alert("Activity Location failed updated!, try again later!");
}
});
}
1 year ago
function submitLocation() {
3 years ago
let payload
1 year ago
if (currentGeoJson && currentGeoJson != "") {
3 years ago
payload = {
geom: JSON.stringify(currentGeoJson),
}
1 year ago
} else {
3 years ago
payload = {
geom: null,
}
}
$.ajax({
1 year ago
data: JSON.stringify(payload),
3 years ago
url: `${base_url}task/update-regular/${currentIdAct}`,
type: "PUT",
processData: false,
contentType: false,
success: function (data) {
$("#modal_activity_location").modal("hide");
1 year ago
currentGeoJson = ""
3 years ago
currentIdAct = 0;
gantt.alert("Activity Location successfully updated!");
},
error: function (data) {
$("#modal_activity_location").modal("hide");
1 year ago
currentGeoJson = ""
3 years ago
currentIdAct = 0;
gantt.alert("Activity Location failed updated!, try again later!");
}
});
}
1 year ago
$("#modal_radius").on("click", "#btn_save_radius", function () {
3 years ago
let radius = $("#buffer_radius").val();
1 year ago
if (radius <= 0) {
3 years ago
gantt.alert("radius buffer tidak boleh kurang dari atau sama dengan 0!!");
1 year ago
} else {
3 years ago
submitCircle(radius);
}
});
});
1 year ago
function submitCircle(radius) {
let circle = L.circle([circLat, circLong], { radius: radius });
3 years ago
let geojsoncircle = circle.toGeoJSON();
geojsoncircle.properties.radius = radius
currentGeoJson = geojsoncircle
1 year ago
addDrawnLayer(circle);
updateCurrentGeoJson();
3 years ago
map.addControl(drawnControlEdit);
$("#modal_radius").modal('hide');
}
$('#modal_radius').on('hide.bs.modal', function (event) {
1 year ago
circLat = 0
circLong = 0
3 years ago
$("#buffer_radius").val("");
});