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

Use map settings from settings endpoint #881

Merged
merged 2 commits into from
Jan 17, 2025
Merged
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
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
12 changes: 0 additions & 12 deletions public/build/3dc417f0-e327-4bc2-aaf8-77d1ea6f2ddf/entrypoints.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"entrypoints": {
"vendor": {
"js": [
"/bundles/pimcorestudioui/build/8e3f61a4-5f5b-4758-b7d9-a5e3651538a1/vendor.js"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bundles/pimcorestudioui/build/8e3f61a4-5f5b-4758-b7d9-a5e3651538a1/vendor.js": "/bundles/pimcorestudioui/build/8e3f61a4-5f5b-4758-b7d9-a5e3651538a1/vendor.js"
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions public/build/9047379e-01ae-4baf-a8d5-068f8abb8b74/entrypoints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"entrypoints": {
"core-dll": {
"css": [
"/bundles/pimcorestudioui/build/9047379e-01ae-4baf-a8d5-068f8abb8b74/core-dll.css"
],
"js": [
"/bundles/pimcorestudioui/build/9047379e-01ae-4baf-a8d5-068f8abb8b74/core-dll.js"
]
}
}
}
Loading
Loading