Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add all locations layer to the map #3056

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 93 additions & 5 deletions client/src/components/Leaflet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Control, CRS, Icon, Map, Marker, TileLayer } from "leaflet"
import AppContext from "components/AppContext"
import GeoLocation from "components/GeoLocation"
import { convertLatLngToMGRS } from "geoUtils"
import { Control, CRS, DivIcon, Icon, Map, Marker, TileLayer } from "leaflet"
import "leaflet-defaulticon-compatibility"
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.webpack.css"
import {
Expand All @@ -16,6 +19,9 @@ import "leaflet/dist/leaflet.css"
import { Location } from "models"
import PropTypes from "prop-types"
import React, { useCallback, useEffect, useRef, useState } from "react"
import ReactDOM from "react-dom"
import MARKER_FLAG_BLUE_2X from "resources/leaflet/marker-flag-blue-2x.png"
import MARKER_FLAG_BLUE from "resources/leaflet/marker-flag-blue.png"
import MARKER_ICON_2X from "resources/leaflet/marker-icon-2x.png"
import MARKER_ICON from "resources/leaflet/marker-icon.png"
import MARKER_SHADOW from "resources/leaflet/marker-shadow.png"
Expand Down Expand Up @@ -64,6 +70,14 @@ const icon = new Icon({
shadowSize: [41, 41]
})

const locationIcon = new Icon({
iconUrl: MARKER_FLAG_BLUE,
cemalettin-work marked this conversation as resolved.
Show resolved Hide resolved
iconRetinaUrl: MARKER_FLAG_BLUE_2X,
iconSize: [64, 64],
iconAnchor: [18, 62],
popupAnchor: [2, -58]
})

const addLayers = (map, layerControl) => {
let defaultLayer = null
Settings.imagery.baseLayers.forEach(layerConfig => {
Expand All @@ -85,11 +99,12 @@ const addLayers = (map, layerControl) => {
}
}

const Leaflet = ({
const BaseLeaflet = ({
width,
height,
marginBottom,
markers,
allLocations,
mapId: initialMapId,
onMapClick
}) => {
Expand All @@ -108,6 +123,7 @@ const Leaflet = ({

const [map, setMap] = useState(null)
const [markerLayer, setMarkerLayer] = useState(null)
const [layerControl, setLayerControl] = useState(null)
const [doInitializeMarkerLayer, setDoInitializeMarkerLayer] = useState(false)
const prevMarkersRef = useRef()

Expand All @@ -121,7 +137,8 @@ const Leaflet = ({
icon: icon,
draggable: m.draggable || false,
autoPan: m.autoPan || false,
id: m.id
id: m.id,
zIndexOffset: 1000
})
if (m.name) {
marker.bindPopup(m.name)
Expand Down Expand Up @@ -166,6 +183,7 @@ const Leaflet = ({
const layerControl = new Control.Layers({}, {}, { collapsed: false })
layerControl.addTo(newMap)
addLayers(newMap, layerControl)
setLayerControl(layerControl)

setMap(newMap)

Expand Down Expand Up @@ -238,20 +256,90 @@ const Leaflet = ({
widthPropUnchanged
])

useEffect(() => {
if (!map || !layerControl || !allLocations?.length) {
return
}
const allMarkers = allLocations
.filter(loc => Location.hasCoordinates(loc))
.map(location => {
const popupContent = document.createElement("div")
popupContent.setAttribute("style", "width: 300px;text-align: center")

return new Marker([location.lat, location.lng], {
icon: locationIcon,
draggable: false,
autoPan: false,
id: location.uuid
})
.bindTooltip(location.name, {
direction: "top",
permanent: true,
offset: [0, -58]
})
.bindPopup(popupContent)
.on("popupopen", e => {
ReactDOM.render(
<>
<b>{location.name}</b> @{" "}
<GeoLocation
coordinates={{
lat: location.lat,
lng: location.lng,
displayedCoordinate: convertLatLngToMGRS(
location.lat,
location.lng
)
}}
/>
</>,
e.popup.getContent()
)
})
})

const locationsLayer = new MarkerClusterGroup({
iconCreateFunction: function(cluster) {
return new DivIcon({
className: "all-locations-marker-cluster-icon-container",
html: `
<img src="${MARKER_FLAG_BLUE}" class="alm-cluster-icon" alt="" />
<div class="alm-cluster-text">${cluster.getChildCount()}</div>
`
})
}
}).addLayers(allMarkers)

layerControl.addOverlay(locationsLayer, "All Locations")
locationsLayer.addTo(map) // make "All Locations" selected by default

return () => {
layerControl.removeLayer(locationsLayer)
map.removeLayer(locationsLayer)
}
}, [map, layerControl, allLocations])

return <div id={mapId} style={style} />
}
Leaflet.propTypes = {
BaseLeaflet.propTypes = {
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
marginBottom: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
markers: PropTypes.array,
allLocations: PropTypes.arrayOf(PropTypes.object).isRequired,
mapId: PropTypes.string, // pass this when you have more than one map on a page
onMapClick: PropTypes.func
}
Leaflet.defaultProps = {
BaseLeaflet.defaultProps = {
width: "100%",
height: "500px",
marginBottom: "18px"
}

const Leaflet = props => (
<AppContext.Consumer>
{context => <BaseLeaflet allLocations={context.allLocations} {...props} />}
</AppContext.Consumer>
)

export default Leaflet
33 changes: 33 additions & 0 deletions client/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -1308,3 +1308,36 @@ div[id*='fg-entityAssessment'] {
text-align: left;
}
}

.all-locations-marker-cluster-icon-container {
border: 1px solid #3272cb;
position: relative !important;
border-radius: 6px;
background: white;
}

.all-locations-marker-cluster-icon-container:hover {
z-index: 9999 !important;
}

.all-locations-marker-cluster-icon-container .alm-cluster-icon {
transform-origin: bottom center;
transform: translate(-14px, -54px);
/* Prevents hover on parent container. If the mouse is on the image, parent container is not hovered while
other childs and parent itself remains hoverable. Image is much larger than the container and it overflows parent.
If hover is not prevented on the image then parent receives "z-index: 9999" which makes any marker beneath unclickable. */
pointer-events: none;
}

.all-locations-marker-cluster-icon-container .alm-cluster-text {
width: 34px;
height: 26px;
position: absolute;
bottom: 0;
left: 0;
transform: translate(3px, -36px);
font-size: 14px;
padding: 3px 0 0 5px;
color: white;
font-weight: bold;
}
17 changes: 15 additions & 2 deletions client/src/pages/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ const GQL_GET_APP_DATA = gql`
shortName
}
}

locationList(query: { pageSize: 0 }) {
list {
uuid
name
lat
lng
}
}
}
`

Expand Down Expand Up @@ -187,7 +196,8 @@ const App = ({ pageDispatchers, pageProps }) => {
currentUser: appState.currentUser,
loadAppData: refetch,
notifications: appState.notifications,
connection: { ...connectionInfo }
connection: { ...connectionInfo },
allLocations: appState.allLocations
}}
>
<ResponsiveLayout
Expand Down Expand Up @@ -231,12 +241,15 @@ const App = ({ pageDispatchers, pageProps }) => {

const currentUser = new Person(data.me)
const notifications = getNotifications(currentUser.position)
const allLocations = data?.locationList?.list || []

return {
currentUser,
settings,
advisorOrganizations,
principalOrganizations,
notifications
notifications,
allLocations
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion client/src/pages/locations/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const LOCATION_TYPES_SUPER_USER =
Settings?.fields?.location?.superUserTypeOptions

const LocationForm = ({ edit, title, initialValues, notesComponent }) => {
const { currentUser } = useContext(AppContext)
const { currentUser, loadAppData } = useContext(AppContext)
const history = useHistory()
const [error, setError] = useState(null)
const [showSimilarLocations, setShowSimilarLocations] = useState(false)
Expand Down Expand Up @@ -353,6 +353,8 @@ const LocationForm = ({ edit, title, initialValues, notesComponent }) => {
// reset the form to latest values
// to avoid unsaved changes propmt if it somehow becomes dirty
form.resetForm({ values, isSubmitting: true })
// After successful submit, reload all locations data
loadAppData()
if (!edit) {
history.replace(Location.pathForEdit(location))
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/src/resources/leaflet/marker-flag-blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.