Compare commits

...

4 commits

Author SHA1 Message Date
j
80d3919b46 one layer per rectangle to redouce events 2025-08-06 23:31:07 +02:00
j
1bac02d24f fixup 2025-08-06 23:18:21 +02:00
j
56123e4575 map_editor example should use async code 2025-08-06 23:02:19 +02:00
j
bc174c49dd support updating MapEditor places 2025-08-06 23:01:31 +02:00
5 changed files with 80 additions and 22 deletions

View file

@ -5,12 +5,43 @@ In this example, we use Ox.MapEditor
'use strict'; 'use strict';
Ox.load(['UI', 'Geo'], function() { Ox.load(['UI', 'Geo'], function() {
var $storage = Ox.localStorage("map_editor")
var places = $storage('places') || []
var $map = Ox.MapEditor({ var $map = Ox.MapEditor({
addPlace: function(place, callback) { addPlace: function(place, callback) {
console.log("addPlace", place) console.log("addPlace", place)
place.id = Ox.encodeBase26(Ox.uid())
places.push(place)
$storage("places", places)
$map.options({
places: Ox.api(places, {
geo: true,
sort: '-area'
})
})
setTimeout(() => {
callback({
status: {
code: 200
},
data: place
})
}, 200)
}, },
editPlace: function(place, callback) { editPlace: function(place, callback) {
console.log("editPlace", place) places.forEach(p => {
if (p.id == place.id) {
Object.assign(p, place);
place = p
}
})
$storage("places", places)
callback({
status: {
code: 200
},
data: place
})
}, },
getMatches: function(names, callback) { getMatches: function(names, callback) {
console.log("getMatches", names) console.log("getMatches", names)
@ -20,9 +51,28 @@ Ox.load(['UI', 'Geo'], function() {
height: 800, height: 800,
mode: 'add', // 'define', mode: 'add', // 'define',
names: null, names: null,
places: [], places: Ox.api(places, {
geo: true,
sort: '-area'
}),
removePlace: function(place, callback) { removePlace: function(place, callback) {
console.log("removePlace", place) console.log("removePlace", place.id, places)
places = places.filter(p => { return p.id != place.id })
console.log("new places", places)
$storage("places", places)
$map.options({
places: Ox.api(places, {
geo: true,
sort: '-area'
})
})
setTimeout(() => {
callback({
status: {
code: 200
},
})
}, 100)
}, },
selected: '', selected: '',
showControls: false, showControls: false,

View file

@ -621,8 +621,6 @@ Ox.Map = function(options, self) {
} }
function clickMap(event) { function clickMap(event) {
console.log("not this one", event)
return
var place = getSelectedPlace(); var place = getSelectedPlace();
if (self.options.clickable/* && !editing()*/) { if (self.options.clickable/* && !editing()*/) {
getPlaceByLatLng(event.lngLat, self.map.getBounds(), function(place) { getPlaceByLatLng(event.lngLat, self.map.getBounds(), function(place) {
@ -718,6 +716,7 @@ Ox.Map = function(options, self) {
callback = point; callback = point;
point = self.map.getCenter(); point = self.map.getCenter();
} }
// fixme, why is getMaxZoom off by one?
let maxZoom = self.map.getMaxZoom() let maxZoom = self.map.getMaxZoom()
setTimeout(() => { setTimeout(() => {
callback(maxZoom) callback(maxZoom)

View file

@ -46,6 +46,15 @@ Ox.MapEditor = function(options, self) {
self.$list.size(); self.$list.size();
self.$map.resizeMap(); self.$map.resizeMap();
}, },
places: function() {
self.isAsync = Ox.isFunction(self.options.places);
self.$list.options({
items: Ox.clone(self.options.places)
})
self.$map.options({
places: self.options.places
})
},
selected: function() { selected: function() {
self.$list.options({selected: self.options.selected}); self.$list.options({selected: self.options.selected});
}, },

View file

@ -22,23 +22,25 @@ class MapLibreRectangle {
id: this.id id: this.id
} }
}; };
this.source = this.map.getSource('rectangles') var sourceId = `${this.id}-rectangles`
this.source = this.map.getSource(sourceId)
if (!this.source) { if (!this.source) {
this.map.addSource('rectangles', { this.map.addSource(sourceId, {
type: 'geojson', type: 'geojson',
data: { data: {
type: 'FeatureCollection', type: 'FeatureCollection',
features: [] features: []
} }
}); });
this.source = this.map.getSource('rectangles') this.source = this.map.getSource(sourceId)
// Add fill layer // Add fill layer
if (!this.map.getLayer('rectangles-fill')) { var layerId = `${this.id}-fill`
if (!this.map.getLayer(layerId)) {
this.map.addLayer({ this.map.addLayer({
id: 'rectangles-fill', id: layerId,
type: 'fill', type: 'fill',
source: 'rectangles', source: sourceId,
paint: { paint: {
'fill-color': '#088', 'fill-color': '#088',
'fill-opacity': 0.3 'fill-opacity': 0.3
@ -47,11 +49,12 @@ class MapLibreRectangle {
} }
// Add outline layer // Add outline layer
if (!this.map.getLayer('rectangles-outline')) { var layerId = `${this.id}-outline`
if (!this.map.getLayer(layerId)) {
this.map.addLayer({ this.map.addLayer({
id: 'rectangles-outline', id: layerId,
type: 'line', type: 'line',
source: 'rectangles', source: sourceId,
paint: { paint: {
'line-color': '#000', 'line-color': '#000',
'line-width': 2 'line-width': 2
@ -140,7 +143,7 @@ class MapLibreRectangle {
}); });
} }
_enableClicking() { _enableClicking() {
this.map.on('click', `rectangles-fill`, e => { this.map.on('click', `${this.id}-fill`, e => {
console.log('click', e) console.log('click', e)
if (this.onclick) { if (this.onclick) {
e.preventDefault() e.preventDefault()

View file

@ -103,12 +103,9 @@ Ox.MapRectangleMarker = function(options) {
@*/ @*/
that.add = function() { that.add = function() {
that.marker.addTo(that.map.map); that.marker.addTo(that.map.map);
console.log("add marker, fix events") that.marker.on('dragstart', dragstart);
/* that.marker.on('drag', drag);
google.maps.event.addListener(that.marker, 'dragstart', dragstart); that.marker.on('dragend', dragend);
google.maps.event.addListener(that.marker, 'drag', drag);
google.maps.event.addListener(that.marker, 'dragend', dragend);
*/
}; };
/*@ /*@
@ -124,7 +121,7 @@ Ox.MapRectangleMarker = function(options) {
update <f> update update <f> update
@*/ @*/
that.update = function() { that.update = function() {
marker = new maplibregl.Marker({ var marker = new maplibregl.Marker({
cursor: that.position + '-resize', cursor: that.position + '-resize',
draggable: true, draggable: true,
element: Ox.MapMarkerImage({ element: Ox.MapMarkerImage({