Skip to content

Commit

Permalink
fix polygon drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
s-tittel committed Sep 12, 2024
1 parent 20095e3 commit 30e0b7e
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/plugins/leaflet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,8 @@ export class LeafletPlugin extends Plugin {
html: '•'
}
}))())
this.map.on('editable:drawing:end', () => {
if (this.displayedShape instanceof L.Marker) {
const pos = this.displayedShape.getLatLng()
this.createdGeometry = { type: 'Point', coordinates: [pos.lng, pos.lat] }
} else if (this.displayedShape instanceof L.Polygon) {
const positions = this.displayedShape.getLatLngs() as L.LatLng[][]
this.createdGeometry = { type: 'Polygon', coordinates: [positions[0].map((pos) => { return [ pos.lng, pos.lat ] })] }
} else {
this.createdGeometry = undefined
}
})
this.map.on('editable:drawing:end', () => { this.saveChanges() })
this.map.on('editable:vertex:dragend', () => { this.saveChanges() })

const dialog = form.querySelector('#shaclMapDialog') as HTMLDialogElement
dialog.addEventListener('close', () => {
Expand Down Expand Up @@ -186,4 +177,20 @@ export class LeafletPlugin extends Plugin {
map.setZoom(5)
}
}

saveChanges() {
if (this.displayedShape instanceof L.Marker) {
const pos = this.displayedShape.getLatLng()
this.createdGeometry = { type: 'Point', coordinates: [pos.lng, pos.lat] }
} else if (this.displayedShape instanceof L.Polygon) {
const positions = this.displayedShape.getLatLngs() as L.LatLng[][]
// force closed polygon
if (!positions[0][0].equals(positions[0][positions[0].length - 1])) {
positions[0].push(positions[0][0])
}
this.createdGeometry = { type: 'Polygon', coordinates: [positions[0].map((pos) => { return [ pos.lng, pos.lat ] })] }
} else {
this.createdGeometry = undefined
}
}
}

0 comments on commit 30e0b7e

Please sign in to comment.