48 public function getHtml($htmlname, $geojson, $centroidjson, $markertype)
52 $out =
'<input id="' . $htmlname .
'" name="' . $htmlname .
'" size="100" value="' . htmlentities($geojson, ENT_QUOTES) .
'"/>';
53 $out .=
'<div id="map_' . $htmlname .
'" style="width: 600px; height: 350px;"></div>';
54 if ($geojson !=
'{}') {
57 $tmp = json_decode($geojson);
59 $tmp2->type = $tmp->type;
60 $tmp2->coordinates = [];
61 if ($tmp->type ==
'Point') {
62 $tmp2->coordinates = [$tmp->coordinates[1], $tmp->coordinates[0]];
63 } elseif ($tmp->type ==
'Polygon') {
64 foreach ($tmp->coordinates as $polygon) {
66 foreach ($polygon as $key => $value) {
67 $polyg[] = [$value[1], $value[0]];
69 $tmp2->coordinates[] = $polyg;
72 foreach ($tmp->coordinates as $key => $value) {
73 $tmp2->coordinates[] = [$value[1], $value[0]];
76 $geojson = json_encode($tmp2);
78 if ($centroidjson !=
'{}') {
79 if (
null === json_decode($centroidjson)) {
84 $tmp = json_decode($centroidjson);
86 $tmp2->type = $tmp->type;
87 $tmp2->coordinates = [];
88 if ($tmp->type ==
'Point') {
89 $tmp2->coordinates = [$tmp->coordinates[1], $tmp->coordinates[0]];
91 foreach ($tmp->coordinates as $key => $value) {
92 $tmp2->coordinates[] = [$value[1], $value[0]];
95 $centroidjson = json_encode($tmp2);
100 var geoms = JSON.parse(\'' . $geojson .
'\');
101 var centroid = JSON.parse(\
'' . $centroidjson .
'\');
102 var markerType =
"' . $markertype . '";
103 var map = L.map(
"map_' . $htmlname . '");
104 console.log(markerType);
111 drawCircleMarker: false,
113 drawRectangle: false,
117 customControls: false,
119 console.log(centroid);
120 if (centroid.type == "Point") {
121 map.setView(centroid.coordinates, 11);
123 map.setView([48.852, 2.351], 12);
125 if (markerType == "point" && Object.keys(geoms).length != 0) {
126 //map.setView(geoms.coordinates, 17);
127 L.marker(geoms.coordinates).addTo(map);
134 } else if (markerType == "multipts" && Object.keys(geoms).length != 0) {
135 L.multipoint(geoms.coordinates).addTo(map);
136 } else if (markerType == "linestrg" && Object.keys(geoms).length != 0) {
137 L.polyline(geoms.coordinates).addTo(map);
138 } else if (markerType == "polygon" && Object.keys(geoms).length != 0) {
139 L.polygon(geoms.coordinates).addTo(map);
140 } else if (Object.keys(geoms).length === 0) {
141 // map.setView([48.852, 2.351], 12);
143 var tiles = L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
145 attribution: \'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>\'
147 map.pm.setLang("' . ($langs->shortlang ??
'en') .
'");
149 enableMarker(markerType);
150 // if (geoms && geoms.type == "Point") {
151 // L.marker([geoms.coordinates[1], geoms.coordinates[0]]).addTo(map);
154 map.on("pm:drawend", (e) => {
157 console.log("pm:drawend");
160 map.on("pm:markerdragend", (e) => {
163 console.log("pm:markerdragend");
166 map.on("pm:remove", (e) => {
169 $("#' . $htmlname .
'").val ("{}");
171 map.on("pm:edit", (e) => {
172 console.log("pm:edit");
175 map.on("pm:create", (e) => {
176 console.log("pm:create");
179 map.on("pm:globaleditmodetoggled", (e) => {
183 function enableMarker(type) {
184 console.log("enable : " + type);
185 if (type == "point") {
191 function disableMarkers(){
199 function generateGeoJson(){
200 var fg = L.featureGroup();
201 var layers = findLayers(map);
202 layers.forEach(function(layer){
205 console.log(fg.toGeoJSON());
206 $("#' . $htmlname .
'").val (JSON.stringify(fg.toGeoJSON().features[0].geometry));
208 function findLayers(map) {
209 // https://stackoverflow.com/questions/62887120/leafletjs-geoman-into-json-data
211 map.eachLayer(layer => {
213 layer instanceof L.Polyline || // Don"t worry about Polygon and Rectangle they are included in Polyline
214 layer instanceof L.Marker ||
215 layer instanceof L.Circle ||
216 layer instanceof L.CircleMarker
221 // filter out layers that don"t have the leaflet-geoman instance
222 layers = layers.filter(layer => !!layer.pm);
223 // filter out everything that"s leaflet-geoman specific temporary stuff
224 layers = layers.filter(layer => !layer._pmTempLayer);