|
// Add this to your app.js phoenix application |
|
|
|
let Hooks = {} |
|
|
|
Hooks.Map = { |
|
mounted(){ |
|
const markers = {} |
|
const map = L.map('mapid').setView([51.505, -0.09], 14) |
|
const paths = {} |
|
let geojsonLayer = L.geoJSON().addTo(map).setStyle({color: "#6435c9"}) |
|
|
|
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', { |
|
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>', |
|
maxZoom: 18, |
|
id: 'mapbox/streets-v11', |
|
tileSize: 512, |
|
zoomOffset: -1, |
|
accessToken: YOUR_MAPBOX_ACCESS_TOKEN |
|
}).addTo(map) |
|
|
|
this.handleEvent("update_marker_position", ({reference, lat, lon, center_view}) => { |
|
markers[reference].setLatLng(L.latLng(lat, lon)) |
|
|
|
if (center_view) { |
|
map.flyTo(L.latLng(lat, lon)) |
|
} |
|
}) |
|
|
|
this.handleEvent("draw_path", ({reference, coordinates, color}) => { |
|
data = { |
|
"type": "LineString", |
|
"coordinates": coordinates |
|
} |
|
|
|
geojsonLayer.addData(data) |
|
}) |
|
|
|
this.handleEvent("view_init", ({reference, lat, lon, zoom_level = 20}) => { |
|
geojsonLayer.remove() |
|
|
|
geojsonLayer = L.geoJSON().addTo(map).setStyle({color: "#6435c9"}) |
|
|
|
map.setView(L.latLng(lat, lon), zoom_level) |
|
}) |
|
|
|
this.handleEvent("set_zoom_level", ({zoom_level}) => { |
|
map.setZoom(zoom_level) |
|
}) |
|
|
|
this.handleEvent("add_marker", ({reference, lat, lon}) => { |
|
// lets not add duplicates for the same marker! |
|
if (markers[reference] == null) { |
|
const marker = L.marker(L.latLng(lat, lon)) |
|
|
|
marker.addTo(map) |
|
|
|
markers[reference] = marker |
|
} |
|
}) |
|
|
|
this.handleEvent("add_marker_with_popup", ({reference, lat, lon, link}) => { |
|
// lets not add duplicates for the same marker! |
|
if (markers[reference] == null) { |
|
const marker = L.marker(L.latLng(lat, lon)) |
|
|
|
marker.bindPopup(`<a href=\"${link}\">${reference}</a>`) |
|
|
|
marker.addTo(map) |
|
|
|
markers[reference] = marker |
|
} |
|
}) |
|
|
|
this.handleEvent("clear", () => { |
|
geojsonLayer.remove() |
|
|
|
geojsonLayer = L.geoJSON().addTo(map) |
|
|
|
for (const [reference, value] of Object.entries(markers)) { |
|
marker = markers[reference] |
|
|
|
marker.remove() |
|
|
|
markers.delete(reference) |
|
} |
|
|
|
}) |
|
|
|
this.handleEvent("remove_marker", ({reference}) => { |
|
if (markers[reference] != null) { |
|
marker = markers[reference] |
|
|
|
marker.remove() |
|
|
|
markers.delete(reference) |
|
} |
|
|
|
geojsonLayer.remove() |
|
}) |
|
} |
|
} |
Great approach to get leaflet working in Elixir!
I spent some time getting this to work: The biggest time consumer was to discover that the
flexclass in line 15 of map_live would render the map gray. Anyways, here is what else i changed to make it runliveview_setup_map(looked like some left overs)liveview_setup_mapinstead ofliveview_set_map_to_addressinmap_live.ex<head>inroot.html.heexfrom hosted source https://leafletjs.com/download.htmlalias Components.LeafletMap, as: LeafletMapto map_live.exapp.jsby altering this line: