Skip to content

Commit

Permalink
Use map settings from settings endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-moser committed Jan 17, 2025
1 parent efe69e2 commit bef401e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useTranslation } from 'react-i18next'
import { ERROR_ADDRESS_NOT_FOUND, geoCode } from '@Pimcore/components/geo-map/utils/geocode'
import { useAlertModal } from '@Pimcore/components/modal/alert-modal/hooks/use-alert-modal'
import { type GeoPoint } from '@Pimcore/components/geo-map/types/geo-types'
import { useSettings } from '@Pimcore/modules/app/settings/hooks/use-settings'

interface AddressSearchFieldProps {
onSearch: (geoPoint?: GeoPoint) => void
Expand All @@ -26,13 +27,14 @@ interface AddressSearchFieldProps {
export const AddressSearchField = (props: AddressSearchFieldProps): React.JSX.Element => {
const { t } = useTranslation()
const alertModal = useAlertModal()
const settings = useSettings()

const onSearch = async (value: string): Promise<void> => {
if (value === '') {
props.onSearch(undefined); return
}

await geoCode(value)
await geoCode(value, settings.maps.geocoding_url_template as string)
.then(props.onSearch)
.catch((error: Error) => {
if (error.message === ERROR_ADDRESS_NOT_FOUND) {
Expand Down
7 changes: 4 additions & 3 deletions assets/js/src/core/components/geo-map/geo-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { type GeoPoints, type GeoPoint, type GeoType, type GeoBounds } from '@Pi
import { addGeoPolyLineToolbar } from '@Pimcore/components/geo-map/toolbar/add-geo-poly-line-toolbar'
import { addGeoPolygonToolbar } from '@Pimcore/components/geo-map/toolbar/add-geo-polygon-toolbar'
import { addGeoBoundsToolbar } from '@Pimcore/components/geo-map/toolbar/add-geo-bounds-toolbar'
import { useSettings } from '@Pimcore/modules/app/settings/hooks/use-settings'

L.Icon.Default.mergeOptions({
iconRetinaUrl: '/bundles/pimcorestudioui/img/leaflet/marker-icon-2x.png',
Expand Down Expand Up @@ -64,6 +65,7 @@ const GeoMap = forwardRef<GeoMapAPI, GeoMapProps>((props, ref): React.JSX.Elemen
const [key, setKey] = useState<number>(0)
const [isVisible, setIsVisible] = useState(false)
const containerRef = useRef<HTMLDivElement>(null)
const settings = useSettings()

const geoMapApi: GeoMapAPI = {
reset: () => {
Expand Down Expand Up @@ -94,15 +96,14 @@ const GeoMap = forwardRef<GeoMapAPI, GeoMapProps>((props, ref): React.JSX.Elemen
} else {
map.setView([props.lat ?? 0, props.lng ?? 0], props.zoom ?? 1)
}

L.tileLayer('https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', {
L.tileLayer(settings.maps.tile_layer_url_template as string, {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map)

const featureGroup = L.featureGroup().addTo(map)

if (props.mode === 'geoPoint') {
addGeoPointToolbar(map, featureGroup, value as GeoPoint, props.onChange, props.disabled)
addGeoPointToolbar(map, featureGroup, settings.maps.reverse_geocoding_url_template as string, value as GeoPoint, props.onChange, props.disabled)
} else if (props.mode === 'geoPolyLine') {
addGeoPolyLineToolbar(map, featureGroup, value as GeoPoints, props.onChange, props.disabled)
} else if (props.mode === 'geoPolygon') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { reverseGeocode } from '@Pimcore/components/geo-map/utils/geocode'
import { type GeoPoint } from '@Pimcore/components/geo-map/types/geo-types'
import { convertLatLngToGeoPoint } from '@Pimcore/components/geo-map/utils/lat-lng-convert'

export const addGeoPointToolbar = (leafletMap: L.Map, featureGroup: L.FeatureGroup, geoPoint?: GeoPoint, onChange?: (geoPoint: GeoPoint) => void, disabled?: boolean): void => {
export const addGeoPointToolbar = (leafletMap: L.Map, featureGroup: L.FeatureGroup, reverseGeoCodeUrlTemplate: string, geoPoint?: GeoPoint, onChange?: (geoPoint: GeoPoint) => void, disabled?: boolean): void => {
leafletMap.addLayer(featureGroup)

const marker = geoPoint !== undefined ? L.marker([geoPoint.latitude, geoPoint.longitude]) : undefined
Expand Down Expand Up @@ -53,7 +53,7 @@ export const addGeoPointToolbar = (leafletMap: L.Map, featureGroup: L.FeatureGro
featureGroup.addLayer(layer)

if (featureGroup.getLayers().length === 1) {
await reverseGeocode(layer).catch((error) => {
await reverseGeocode(layer, reverseGeoCodeUrlTemplate).catch((error) => {
console.error(error)
})
onChange?.(convertLatLngToGeoPoint(layer.getLatLng()))
Expand All @@ -63,7 +63,7 @@ export const addGeoPointToolbar = (leafletMap: L.Map, featureGroup: L.FeatureGro
leafletMap.on('draw:editmove', async function (e) {
const layer = e.layer as L.Marker

await reverseGeocode(layer).catch((error) => {
await reverseGeocode(layer, reverseGeoCodeUrlTemplate).catch((error) => {
console.error(error)
})
onChange?.(convertLatLngToGeoPoint(layer.getLatLng()))
Expand Down
10 changes: 5 additions & 5 deletions assets/js/src/core/components/geo-map/utils/geocode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { type GeoPoint } from '@Pimcore/components/geo-map/types/geo-types'

export const ERROR_ADDRESS_NOT_FOUND = 'address_not_found'

export const geoCode = async (address: string): Promise<GeoPoint> => {
const geoCodeUrl = 'https://nominatim.openstreetmap.org/search?q={q}&addressdetails=1&format=json&limit=1'.replace('{q}', encodeURIComponent(address))
export const geoCode = async (address: string, geoCodeUrlTemplate: string): Promise<GeoPoint> => {
const geoCodeUrl = geoCodeUrlTemplate.replace('{q}', encodeURIComponent(address))

const response = await fetch(geoCodeUrl)

Expand All @@ -36,10 +36,10 @@ export const geoCode = async (address: string): Promise<GeoPoint> => {
}
}

export const reverseGeocode = async (layerObj: L.Marker): Promise<void> => {
const reverseGeocodeUrl = 'https://nominatim.openstreetmap.org/reverse?format=json&lat={lat}&lon={lng}'
export const reverseGeocode = async (layerObj: L.Marker, reverseGeoCodeUrlTemplate: string): Promise<void> => {
const reverseGeocodeUrl = reverseGeoCodeUrlTemplate
.replace('{lat}', layerObj.getLatLng().lat.toString())
.replace('{lng}', layerObj.getLatLng().lng.toString())
.replace('{lon}', layerObj.getLatLng().lng.toString())

await fetch(reverseGeocodeUrl).then(async (response: Response | undefined | null) => {
if (response === undefined || response === null) {
Expand Down

0 comments on commit bef401e

Please sign in to comment.