Skip to content

Commit f83d59a

Browse files
committed
select basemap features also on touch
1 parent 38d8b5e commit f83d59a

1 file changed

Lines changed: 51 additions & 33 deletions

File tree

app/javascript/maplibre/layers/basemap.js

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,56 +71,74 @@ export class BasemapLayer extends Layer {
7171
}
7272

7373
/**
74-
* Removes event handlers including custom contextmenu handler.
74+
* Removes event handlers including custom contextmenu and touch handlers.
7575
*/
7676
removeEventHandlers() {
7777
super.removeEventHandlers()
7878
if (this.contextMenuHandler) {
7979
map.off('contextmenu', this.contextMenuHandler)
8080
this.contextMenuHandler = null
8181
}
82+
if (this.touchStartHandler) {
83+
map.off('touchstart', this.touchStartHandler)
84+
this.touchStartHandler = null
85+
}
86+
}
87+
88+
/**
89+
* Handles feature highlighting at a given point (used by both mouse and touch).
90+
*/
91+
highlightFeatureAtPoint(point) {
92+
if (stickyFeatureHighlight && highlightedFeatureId) { return }
93+
if (document.querySelector('.show > .map-modal')) { return }
94+
95+
const basemapSource = basemaps()[mapProperties.base_map].sourceName
96+
const mapLayers = map.getStyle().layers
97+
const queryLayerIds = mapLayers.filter(layer => layer.source === basemapSource).map(layer => layer.id)
98+
const features = map.queryRenderedFeatures(point, { layers: queryLayerIds})
99+
100+
if (features.length) {
101+
const feature = features[0]
102+
103+
// exit early when moving over same feature
104+
if (JSON.stringify(feature.geometry) === JSON.stringify(this?.selectedFeature?.geometry)) { return }
105+
this.selectedFeature = feature
106+
hideContextMenu()
107+
108+
console.log('Selected features: ', features)
109+
110+
const geojsonFeature = {
111+
type: 'Feature',
112+
geometry: feature.geometry,
113+
properties: feature.properties
114+
}
115+
geojsonFeature.id = geojsonFeature.properties.id = functions.featureId()
116+
geojsonFeature.properties.desc = overpassDescription(geojsonFeature.properties)
117+
const height = geojsonFeature.properties['hoehe'] || geojsonFeature.properties['render_height']
118+
if (height) {
119+
geojsonFeature.properties['fill-extrusion-height'] = height
120+
}
121+
122+
this.layer.geojson.features = [geojsonFeature]
123+
map.getSource(this.sourceId).setData(this.layer.geojson, false)
124+
}
82125
}
83126

84127
/**
85128
* Custom mousemove handler for basemap layer - queries basemap source layers.
86129
*/
87130
setupMouseMoveHandler() {
88131
this.mouseMoveHandler = (e) => {
89-
if (stickyFeatureHighlight && highlightedFeatureId) { return }
90-
if (document.querySelector('.show > .map-modal')) { return }
91-
92-
const basemapSource = basemaps()[mapProperties.base_map].sourceName
93-
const mapLayers = map.getStyle().layers
94-
const queryLayerIds = mapLayers.filter(layer => layer.source === basemapSource).map(layer => layer.id)
95-
const features = map.queryRenderedFeatures(e.point, { layers: queryLayerIds})
132+
this.highlightFeatureAtPoint(e.point)
133+
}
96134

97-
if (features.length) {
98-
const feature = features[0]
99-
100-
// exit early when moving over same feature
101-
if (JSON.stringify(feature.geometry) === JSON.stringify(this?.selectedFeature?.geometry)) { return }
102-
this.selectedFeature = feature
103-
hideContextMenu()
104-
105-
console.log('Selected features: ', features)
106-
107-
const geojsonFeature = {
108-
type: 'Feature',
109-
geometry: feature.geometry,
110-
properties: feature.properties
111-
}
112-
geojsonFeature.id = geojsonFeature.properties.id = functions.featureId()
113-
geojsonFeature.properties.desc = overpassDescription(geojsonFeature.properties)
114-
const height = geojsonFeature.properties['hoehe'] || geojsonFeature.properties['render_height']
115-
if (height) {
116-
geojsonFeature.properties['fill-extrusion-height'] = height
117-
}
118-
119-
this.layer.geojson.features = [geojsonFeature]
120-
map.getSource(this.sourceId).setData(this.layer.geojson, false)
121-
}
135+
this.touchStartHandler = (e) => {
136+
// Only handle single touch
137+
if (e.originalEvent.touches && e.originalEvent.touches.length !== 1) { return }
138+
this.highlightFeatureAtPoint(e.point)
122139
}
123140

124141
map.on('mousemove', this.mouseMoveHandler)
142+
map.on('touchstart', this.touchStartHandler)
125143
}
126144
}

0 commit comments

Comments
 (0)