Skip to content

Commit

Permalink
Allow pinpointing of locations on Explore map
Browse files Browse the repository at this point in the history
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
zarino committed Jul 16, 2024
1 parent 8848657 commit 01ae69a
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 6 deletions.
216 changes: 216 additions & 0 deletions hub/static/css/_leaflet-geosearch.scss
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;
}
4 changes: 4 additions & 0 deletions hub/static/css/_leaflet.scss
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;
}
3 changes: 3 additions & 0 deletions hub/static/css/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@

@import "../../../vendor/leaflet/leaflet-1.8.0";

@import "leaflet";
@import "leaflet-geosearch";

@import "site-header";
@import "site-footer";
@import "site-feedback";
Expand Down
36 changes: 31 additions & 5 deletions hub/static/js/explore.esm.js
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({
Expand Down Expand Up @@ -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)
}
},
Expand Down Expand Up @@ -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)

Expand All @@ -419,12 +422,35 @@ const app = createApp({
maxZoom: 19,
attribution: '&copy; <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() {
Expand Down Expand Up @@ -458,7 +484,7 @@ const app = createApp({
},
})
}
}).addTo(this.map)
}).addTo(toRaw(this.map))

this.key = null
this.legend = null
Expand Down
3 changes: 2 additions & 1 deletion local_intelligence_hub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@
("bootstrap", BASE_DIR / "vendor" / "bootstrap" / "js"),
("chartjs", BASE_DIR / "vendor" / "chartjs" / "js"),
("jquery", BASE_DIR / "vendor" / "jquery" / "js"),
("leaflet", BASE_DIR / "vendor" / "leaflet" / "js"),
("leaflet", BASE_DIR / "vendor" / "leaflet"),
("leaflet-geosearch", BASE_DIR / "vendor" / "leaflet-geosearch" / "js"),
("papaparse", BASE_DIR / "vendor" / "papaparse" / "js"),
("popper", BASE_DIR / "vendor" / "popper" / "js"),
("vue", BASE_DIR / "vendor" / "vue" / "js"),
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"chart.js": "^4.4.1",
"jquery": "^3.7.1",
"leaflet": "^1.9.4",
"leaflet-geosearch": "^4.0.0",
"vue": "^3.4.15"
}
}
1 change: 1 addition & 0 deletions vendor/leaflet-geosearch/js/leaflet-geosearch-4.0.0.min.js

Large diffs are not rendered by default.

0 comments on commit 01ae69a

Please sign in to comment.