11'use client' ;
2-
2+ import '../components/components-css/PropertyMap.css' ;
33import {
44 FC ,
55 useEffect ,
@@ -26,6 +26,7 @@ import Map, {
2626} from 'react-map-gl/maplibre' ;
2727import maplibregl , {
2828 Map as MaplibreMap ,
29+ IControl ,
2930 PointLike ,
3031 MapGeoJSONFeature ,
3132 ColorSpecification ,
@@ -50,6 +51,13 @@ import { centroid } from '@turf/centroid';
5051import { Position } from 'geojson' ;
5152import { toTitleCase } from '../utilities/toTitleCase' ;
5253import { ThemeButton } from '../components/ThemeButton' ;
54+ import MapStyleSwitcher from './MapStyleSwitcher' ;
55+
56+ type MapStyle = {
57+ url : string ;
58+ } ;
59+
60+ type MapStyles = Record < string , MapStyle > ;
5361
5462type SearchedProperty = {
5563 coordinates : [ number , number ] ;
@@ -105,15 +113,30 @@ const layerStylePoints: CircleLayerSpecification = {
105113 } ,
106114} ;
107115
116+ const mapStyles : MapStyles = {
117+ DataVisualization : {
118+ url : `https://api.maptiler.com/maps/dataviz/style.json?key=${ maptilerApiKey } ` ,
119+ } ,
120+ Hybrid : {
121+ url : `https://api.maptiler.com/maps/hybrid/style.json?key=${ maptilerApiKey } ` ,
122+ } ,
123+ Street : {
124+ url : `https://api.maptiler.com/maps/streets/style.json?key=${ maptilerApiKey } ` ,
125+ } ,
126+ } ;
127+
108128// info icon in legend summary
109129let summaryInfo : ReactElement | null = null ;
110130
111- const MapControls = ( ) => {
131+ const MapControls : React . FC < {
132+ handleStyleChange : ( styleName : string ) => void ;
133+ } > = ( { handleStyleChange } ) => {
112134 const [ smallScreenToggle , setSmallScreenToggle ] = useState < boolean > ( false ) ;
113135 return (
114136 < >
115137 < NavigationControl showCompass = { false } position = "bottom-right" />
116138 < GeolocateControl position = "bottom-right" />
139+ < MapStyleSwitcher handleStyleChange = { handleStyleChange } />
117140 { smallScreenToggle || window . innerWidth > 640 ? (
118141 < MapLegendControl
119142 position = "bottom-left"
@@ -162,7 +185,10 @@ const PropertyMap: FC<PropertyMapProps> = ({
162185 const { appFilter } = useFilter ( ) ;
163186 const [ popupInfo , setPopupInfo ] = useState < any | null > ( null ) ;
164187 const [ map , setMap ] = useState < MaplibreMap | null > ( null ) ;
165- const [ mapController , setMapController ] = useState ( ) ;
188+ const [ mapController , setMapController ] = useState < IControl > ( ) ;
189+ const [ currentStyle , setCurrentStyle ] = useState < string > (
190+ 'Data Visualization View'
191+ ) ;
166192 const [ searchedProperty , setSearchedProperty ] = useState < SearchedProperty > ( {
167193 coordinates : [ - 75.1628565788269 , 39.97008211622267 ] ,
168194 address : '' ,
@@ -181,6 +207,10 @@ const PropertyMap: FC<PropertyMapProps> = ({
181207 handleMapClick ( e . lngLat ) ;
182208 } ;
183209
210+ const handleStyleChange = ( styleName : string ) => {
211+ setCurrentStyle ( styleName ) ;
212+ } ;
213+
184214 const moveMap = ( targetPoint : LngLatLike ) => {
185215 if ( map ) {
186216 map . easeTo ( {
@@ -387,7 +417,7 @@ const PropertyMap: FC<PropertyMapProps> = ({
387417 if ( map ) {
388418 updateFilter ( ) ;
389419 }
390- } , [ map , appFilter ] ) ;
420+ } , [ map , appFilter , currentStyle ] ) ;
391421
392422 const changeCursor = ( e : any , cursorType : 'pointer' | 'default' ) => {
393423 e . target . getCanvas ( ) . style . cursor = cursorType ;
@@ -399,7 +429,7 @@ const PropertyMap: FC<PropertyMapProps> = ({
399429 < Map
400430 mapLib = { maplibregl as any }
401431 initialViewState = { initialViewState }
402- mapStyle = { `https://api.maptiler.com/maps/dataviz/style.json?key= ${ maptilerApiKey } ` }
432+ mapStyle = { mapStyles [ currentStyle ] ?. url }
403433 onMouseEnter = { ( e ) => changeCursor ( e , 'pointer' ) }
404434 onMouseLeave = { ( e ) => changeCursor ( e , 'default' ) }
405435 onClick = { onMapClick }
@@ -423,8 +453,20 @@ const PropertyMap: FC<PropertyMapProps> = ({
423453 onSourceData = { ( e ) => {
424454 handleSetFeatures ( e ) ;
425455 } }
456+ onStyleData = { ( e ) => {
457+ const layerIds = e . target
458+ . getStyle ( )
459+ . layers . map ( ( layer : any ) => layer . id ) ;
460+ const layersApplied = layers . every ( ( layer ) =>
461+ layerIds . includes ( layer )
462+ ) ;
463+ if ( layersApplied ) {
464+ setHasLoadingError ( false ) ;
465+ }
466+ } }
426467 onMoveEnd = { handleSetFeatures }
427468 >
469+ < MapControls handleStyleChange = { handleStyleChange } />
428470 < div
429471 className = "geocoding"
430472 style = { {
@@ -467,7 +509,6 @@ const PropertyMap: FC<PropertyMapProps> = ({
467509 } }
468510 />
469511 </ div >
470- < MapControls />
471512 { popupInfo && (
472513 < Popup
473514 className = "customized-map-popup"
0 commit comments