Skip to content

Commit

Permalink
EditFeatureMap: Add satellite map (#964)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvaclavik authored Feb 27, 2025
1 parent 423c1a7 commit 7a97ba6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useState } from 'react';
import { CircularProgress, Stack, TextField } from '@mui/material';
import React, { useEffect, useState } from 'react';
import { Chip, CircularProgress, Stack, TextField } from '@mui/material';

import styled from '@emotion/styled';
import { t } from '../../../../../../services/intl';
import { useCurrentItem } from '../CurrentContext';
import { useInitEditFeatureMap } from './useInitEditFeatureMap';
import { LngLat } from 'maplibre-gl';
import LayersIcon from '@mui/icons-material/Layers';
import { getMapStyle } from './getMapStyle';

const Container = styled.div`
width: 100%;
Expand All @@ -28,16 +30,34 @@ const Map = styled.div<{ $isVisible: boolean }>`
height: 100%;
width: 100%;
`;
const MapStyle = styled.div`
position: absolute;
top: 4px;
right: 4px;
`;

const EditFeatureMap = () => {
const EditFeatureMap = ({ mapStyle, setMapStyle }) => {
const [isFirstMapLoad, setIsFirstMapLoad] = useState(true);
const { containerRef, isMapLoaded, currentItem, onMarkerChange } =
const { containerRef, isMapLoaded, currentItem, onMarkerChange, mapRef } =
useInitEditFeatureMap(isFirstMapLoad, setIsFirstMapLoad);

useEffect(() => {
const style = getMapStyle(mapStyle);
mapRef.current.setStyle(style);
}, [mapStyle, mapRef]);

const { shortId } = useCurrentItem();
const isNode = shortId[0] === 'n';
if (!isNode) return null;

const switchMapStyle = () => {
if (mapStyle === 'outdoor') {
setMapStyle('satellite');
} else {
setMapStyle('outdoor');
}
};

return (
<>
<Container>
Expand All @@ -47,7 +67,17 @@ const EditFeatureMap = () => {
</LoadingContainer>
)}
<Map $isVisible={isMapLoaded} ref={containerRef} />
<MapStyle>
<Chip
component="button"
label={mapStyle}
icon={<LayersIcon />}
onClick={switchMapStyle}
color="secondary"
/>
</MapStyle>
</Container>

<Stack direction="row" mt={2} gap={1}>
<TextField
label={t('editdialog.location_latitude')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const LocationEditor = () => {
const { shortId } = useCurrentItem();
const osmId = getApiId(shortId);
const isNodeWithoutWay = useNodeWithoutWayCheck(osmId);
const [mapStyle, setMapStyle] = useState<'outdoor' | 'satellite'>('outdoor');

if (osmId.type === 'relation') {
return null;
Expand All @@ -62,7 +63,11 @@ export const LocationEditor = () => {
let content = null;
if (expanded) {
if (osmId.type === 'node') {
content = isNodeWithoutWay ? <EditFeatureMapDynamic /> : <WayWarning />;
content = isNodeWithoutWay ? (
<EditFeatureMapDynamic mapStyle={mapStyle} setMapStyle={setMapStyle} />
) : (
<WayWarning />
);
}
if (osmId.type === 'way') {
content = <WayWarning />;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { outdoorStyle } from '../../../../../Map/styles/outdoorStyle';

const apiKey = process.env.NEXT_PUBLIC_API_KEY_MAPTILER;

export const getMapStyle = (mapStyle) => {
if (mapStyle === 'satellite') {
return `https://api.maptiler.com/maps/hybrid/style.json?key=${apiKey}`;
}
return outdoorStyle;
};
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,9 @@ export function useInitEditFeatureMap(isFirstMapLoad, setIsFirstMapLoad) {
setIsFirstMapLoad(true);
}, [current, setIsFirstMapLoad]);

return { containerRef, isMapLoaded, currentItem, onMarkerChange };
useEffect(() => {
setIsFirstMapLoad(true);
}, [current, setIsFirstMapLoad]);

return { containerRef, isMapLoaded, currentItem, onMarkerChange, mapRef };
}
4 changes: 3 additions & 1 deletion src/components/FeaturePanel/renderers/WebsiteRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const displayForm = (url: string) =>
export const WebsiteRenderer = ({ v }) => (
<>
<Language fontSize="small" />
<a href={fixHttp(v)}>{displayForm(v)}</a>
<a href={fixHttp(v)} target="_blank">
{displayForm(v)}
</a>
</>
);

0 comments on commit 7a97ba6

Please sign in to comment.