Created
June 1, 2021 09:29
-
-
Save ambientlight/d186d2435c2399eb9f41c9c3fcd38947 to your computer and use it in GitHub Desktop.
AccessiblePopups.tsx example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { memo, useMemo, useState } from 'react'; | |
import { | |
AzureMap, | |
AzureMapDataSourceProvider, | |
AzureMapFeature, | |
AzureMapLayerProvider, | |
AzureMapsProvider, | |
IAzureMapOptions, | |
AzureMapPopup, | |
IAzureMapFeature | |
} from 'react-azure-maps'; | |
import { AuthenticationType, data, PopupOptions } from 'azure-maps-control'; | |
import { key } from '../key'; | |
const points = Array.from({ length: 20 }).map(() => { | |
const randomLongitude = Math.floor(Math.random() * (-80 - -120) + -120); | |
const randomLatitude = Math.floor(Math.random() * (30 - 65) + 65); | |
return new data.Position(randomLongitude, randomLatitude); | |
}); | |
const renderPoint = (coordinates: data.Position): IAzureMapFeature => { | |
const rendId = Math.random(); | |
return ( | |
<AzureMapFeature | |
key={rendId} | |
id={rendId.toString()} | |
type="Point" | |
coordinate={coordinates} | |
properties={{ | |
id: rendId, | |
prop: 'My Feature Prop', | |
}} | |
/> | |
); | |
}; | |
const MarkersExample: React.FC = () => { | |
const option: IAzureMapOptions = useMemo(() => { | |
return { | |
authOptions: { | |
authType: AuthenticationType.subscriptionKey, | |
subscriptionKey: key, | |
}, | |
center: [-100.01, 45.01], | |
zoom: 2, | |
view: 'Auto', | |
}; | |
}, []); | |
return ( | |
<AzureMapsProvider> | |
<div style={styles.map}> | |
<MapController options={option}/> | |
</div> | |
</AzureMapsProvider> | |
); | |
}; | |
const MapController = (props: { options: IAzureMapOptions}) => { | |
const [markers, setMarkers] = useState([...points]); | |
const [popupOptions, setPopupOptions] = useState<PopupOptions>({}); | |
const memoizedMarkerRender: IAzureMapFeature[] = useMemo( | |
(): IAzureMapFeature[] => markers.map((marker) => renderPoint(marker)), | |
[markers], | |
); | |
return (<AzureMap options={props.options}> | |
<AzureMapDataSourceProvider id={'MultiplePoint AzureMapDataSourceProvider'}> | |
<AzureMapLayerProvider | |
id={'MultiplePoint AzureMapLayerProvider'} | |
options={{ | |
iconOptions: { | |
image: 'pin-red', | |
}, | |
}} | |
events={{}} | |
type="SymbolLayer" | |
/> | |
{memoizedMarkerRender} | |
</AzureMapDataSourceProvider> | |
<> | |
{points.map((point, idx) => | |
<AzureMapPopup | |
key={idx} | |
isVisible={!idx} | |
options={{ | |
...popupOptions, | |
position: point, | |
pixelOffset: [0, -18], | |
}} | |
popupContent={ | |
<div style={styles.popupStyles}>{JSON.stringify({lng: point[0], lat: point[1]})}</div> // Inject your JSX | |
} | |
/> | |
)} | |
</> | |
</AzureMap>) | |
} | |
const styles = { | |
map: { | |
height: "50vh", | |
}, | |
buttonContainer: { | |
display: 'grid', | |
gridAutoFlow: 'column', | |
gridGap: '10px', | |
gridAutoColumns: 'max-content', | |
padding: '10px 0', | |
alignItems: 'center', | |
}, | |
button: { | |
height: 35, | |
width: 80, | |
backgroundColor: '#68aba3', | |
'text-align': 'center', | |
}, | |
popupStyles: { | |
padding: '20px', | |
color: 'black', | |
}, | |
}; | |
export default memo(MarkersExample); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment