-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow pinpointing of locations on Explore map
Uses the Leaflet GeoSearch plugin, and OSM Nominatim geocoder, to allow users to place a marker on the map, at the requested location. Handy when you know the name of a place, but not which constituency it’s in! Part of this work required me to have access to `this.map` inside a callback function, which revealed the same Vue.js proxying issue we’ve encountered in other projects. The fix is to ensure we “un-proxy” all references `this.map` with `toRaw(this.map)`.
- Loading branch information
Showing
8 changed files
with
283 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
// Customised version of | ||
// https://github.com/smeijer/leaflet-geosearch/blob/v3.6.1/assets/css/leaflet.css | ||
// to match our Bootstrap styling | ||
|
||
.leaflet-control-geosearch { | ||
*, | ||
*:before, | ||
*:after { | ||
box-sizing: border-box; | ||
} | ||
|
||
.leaflet-bar-part { | ||
border-radius: 4px; | ||
border-bottom: none; | ||
} | ||
|
||
a.leaflet-bar-part:before, | ||
a.leaflet-bar-part:after { | ||
position: absolute; | ||
display: block; | ||
content: ''; | ||
} | ||
|
||
a.leaflet-bar-part:before { | ||
top: 17px; | ||
left: 15px; | ||
width: 7px; | ||
border-top: 2px solid black; | ||
transform: rotateZ(45deg); | ||
} | ||
|
||
a.leaflet-bar-part:after { | ||
top: 8px; | ||
left: 8px; | ||
height: 10px; | ||
width: 10px; | ||
border-radius: 50%; | ||
border: 2px solid black; | ||
} | ||
|
||
&.error a.leaflet-bar-part:before, | ||
&.pending a.leaflet-bar-part:before { | ||
display: none; | ||
} | ||
|
||
&.error a.leaflet-bar-part:after, | ||
&.pending a.leaflet-bar-part:after { | ||
left: 50%; | ||
top: 50%; | ||
width: 18px; | ||
height: 18px; | ||
margin: -9px 0 0 -9px; | ||
border-radius: 50%; | ||
} | ||
|
||
&.pending a.leaflet-bar-part:after { | ||
content: ''; | ||
border: 2px solid black; | ||
border-top: 2px solid #f3f3f3; | ||
animation: spin 1s linear infinite; | ||
} | ||
|
||
&.error a.leaflet-bar-part:after { | ||
content: '!'; | ||
line-height: initial; | ||
font-weight: 600; | ||
font-size: 18px; | ||
border: none; | ||
} | ||
|
||
form { | ||
display: none; | ||
position: absolute; | ||
top: -2px; | ||
left: 30px; | ||
border-radius: 0 4px 4px 0; | ||
background-color: #fff; | ||
background-clip: padding-box; | ||
z-index: -1; | ||
height: auto; | ||
margin: 0; | ||
padding: 0 8px; | ||
border: 2px solid rgba(0,0,0,0.2); | ||
|
||
font-family: var(--bs-body-font-family); | ||
font-size: 0.875rem; | ||
|
||
.leaflet-right & { | ||
right: 28px; | ||
left: initial; | ||
border-radius: 4px 0 0 4px; | ||
border-left: inherit; | ||
border-right: none; | ||
} | ||
|
||
&.open { | ||
border-radius: 0 4px 4px 4px; | ||
} | ||
|
||
input { | ||
min-width: 200px; | ||
width: 100%; | ||
outline: none; | ||
border: none; | ||
margin: 0; | ||
padding: 0; | ||
font-size: 1em; | ||
height: 30px; | ||
border: none; | ||
border-radius: 0 4px 4px 0; | ||
} | ||
} | ||
|
||
&.active form { | ||
display: block; | ||
} | ||
|
||
.results { | ||
background: #fff; | ||
|
||
&.active { | ||
padding: 8px 0; | ||
border-top: 1px solid #c6c6c6; | ||
} | ||
|
||
& > * { | ||
padding: 6px 12px; | ||
border-radius: 0.25rem; | ||
|
||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
} | ||
|
||
& > .active, | ||
& > :hover { | ||
background-color: $dropdown-link-hover-bg; | ||
cursor: pointer; | ||
} | ||
} | ||
|
||
button.reset { | ||
color: black; | ||
font-weight: bold; | ||
position: absolute; | ||
line-height: 30px; | ||
padding: 0 10px; | ||
right: 0; | ||
top: 0; | ||
cursor: pointer; | ||
border: none; | ||
text-decoration: none; | ||
background-color: #fff; | ||
border-radius: 0 4px 4px 0; | ||
|
||
&:hover { | ||
background: #f5f5f5; | ||
} | ||
} | ||
} | ||
|
||
.leaflet-geosearch-button { | ||
&.active { | ||
.leaflet-bar-part { | ||
border-radius: 4px 0 0 4px !important; | ||
width: 30px; | ||
} | ||
} | ||
|
||
form { | ||
max-width: 350px; | ||
} | ||
} | ||
|
||
// animations | ||
@keyframes spin { | ||
0% { transform: rotate(0deg); } | ||
100% { transform: rotate(360deg); } | ||
} | ||
|
||
.leaflet-geosearch-bar { | ||
position: relative; | ||
display: block; | ||
height: auto; | ||
width: 400px; | ||
max-width: calc(100% - 120px); | ||
margin: 10px auto 0; | ||
cursor: auto; | ||
z-index: 1000; | ||
|
||
.leaflet-top &, | ||
.leaflet-bottom & { | ||
display: none; | ||
} | ||
|
||
form { | ||
position: relative; | ||
top: 0; | ||
left: 0; | ||
display: block; | ||
border-radius: 4px; | ||
|
||
input { | ||
min-width: 100%; | ||
width: 100%; | ||
} | ||
} | ||
|
||
.results.active:after { | ||
opacity: .2; | ||
} | ||
} | ||
|
||
.leaflet-bar-notfound { | ||
font-style: italic; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.leaflet-default-icon-path { | ||
// L.Icon.Default uses this style to work out the path to our images. | ||
background-image: url("/static/leaflet/images/marker-icon.png") !important; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import Modal from 'bootstrap/js/dist/modal' | ||
import { createApp } from 'vue/dist/vue.esm-bundler.js' | ||
import { createApp, toRaw } from 'vue/dist/vue.esm-bundler.js' | ||
import { Map } from 'leaflet/src/map' | ||
import { TileLayer } from 'leaflet/src/layer/tile' | ||
import { GeoJSON } from 'leaflet/src/layer' | ||
import { Icon } from 'leaflet/src/layer/marker' | ||
import { Zoom } from 'leaflet/src/control' | ||
import { Attribution } from 'leaflet/src/control' | ||
import { GeoSearchControl, OpenStreetMapProvider } from 'leaflet-geosearch'; | ||
import trackEvent from './analytics.esm.js' | ||
|
||
const app = createApp({ | ||
|
@@ -381,7 +383,7 @@ const app = createApp({ | |
// because the map element should be visible by then. | ||
var _this = this | ||
setTimeout(function(){ | ||
_this.map.invalidateSize() | ||
toRaw(_this.map).invalidateSize() | ||
}, 100) | ||
} | ||
}, | ||
|
@@ -410,6 +412,7 @@ const app = createApp({ | |
}); | ||
}, | ||
setUpMap() { | ||
var _this = this; | ||
this.loading = true | ||
this.map = new Map(this.$refs.map).setView([54.0934, -2.8948], 7) | ||
|
||
|
@@ -419,12 +422,35 @@ const app = createApp({ | |
maxZoom: 19, | ||
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' | ||
} | ||
).addTo(this.map) | ||
).addTo(toRaw(this.map)) | ||
|
||
const searchControl = new GeoSearchControl({ | ||
provider: new OpenStreetMapProvider({ | ||
params: { | ||
email: '[email protected]', | ||
countrycodes: 'gb', // limit results to UK | ||
addressdetails: 1 | ||
} | ||
}), | ||
resultFormat: function(args){ | ||
// no need to include UK in the labels | ||
return args.result.label.replace(', United Kingdom', ''); | ||
}, | ||
style: 'button', | ||
updateMap: false // we handle our own pan/zoom | ||
}); | ||
toRaw(this.map).addControl(searchControl); | ||
toRaw(this.map).on('geosearch/showlocation', function(args){ | ||
toRaw(_this.map).fitBounds(args.location.bounds, { | ||
maxZoom: 10, | ||
padding: [100, 100] | ||
}); | ||
}); | ||
|
||
return this.setUpMapAreas() | ||
}, | ||
removeMapAreas() { | ||
this.map.removeLayer(window.geojson) | ||
toRaw(this.map).removeLayer(window.geojson) | ||
return this.setUpMapAreas() | ||
}, | ||
setUpMapAreas() { | ||
|
@@ -458,7 +484,7 @@ const app = createApp({ | |
}, | ||
}) | ||
} | ||
}).addTo(this.map) | ||
}).addTo(toRaw(this.map)) | ||
|
||
this.key = null | ||
this.legend = null | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.