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

Add load prop to Map #28

Merged
merged 1 commit into from
Mar 26, 2023
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
4 changes: 3 additions & 1 deletion src/components/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function forwardMapkitEvent<E>(
const Map = React.forwardRef<mapkit.Map | null, React.PropsWithChildren<MapProps>>(({
children = undefined,

load: customLoad,
token,

colorScheme = ColorScheme.Light,
Expand Down Expand Up @@ -94,7 +95,8 @@ const Map = React.forwardRef<mapkit.Map | null, React.PropsWithChildren<MapProps

// Load the map
useEffect(() => {
load(token).then(() => {
const loadMap = typeof customLoad === 'function' ? customLoad : load;
loadMap(token).then(() => {
if (exists.current) return;
const options = initialRegion
? { region: toMapKitCoordinateRegion(initialRegion) }
Expand Down
5 changes: 5 additions & 0 deletions src/components/MapProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
} from '../util/parameters';

export default interface MapProps {
/**
* Custom load method for MapKit JS.
*/
load?: (token: string) => Promise<void>;

/**
* The token provided by MapKit JS.
*/
Expand Down
40 changes: 40 additions & 0 deletions src/stories/Map.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,43 @@ export const PointOfInterestFilters = () => {
);
};
PointOfInterestFilters.storyName = 'Point of Interest Filter';

export const CustomLoadFunction = () => {
const initialRegion: CoordinateRegion = useMemo(() => ({
centerLatitude: 40.7538,
centerLongitude: -73.986,
latitudeDelta: 0.03,
longitudeDelta: 0.03,
}), []);

return (
<Map
load={(customLoadToken) => (
new Promise((resolve) => {
const element = document.createElement('script');
// @ts-ignore-next-line
window.initMapKit = () => {
// @ts-ignore-next-line
delete window.initMapKit;
window.mapkit.init({
authorizationCallback: (done) => {
done(customLoadToken);
},
});
resolve();
};
element.src = 'https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.core.js';
element.dataset.callback = 'initMapKit';
element.dataset.initialToken = customLoadToken;
element.dataset.libraries = 'map';
element.crossOrigin = 'anonymous';
document.head.appendChild(element);
})
)}
token={token}
initialRegion={initialRegion}
showsMapTypeControl={false}
/>
);
};
CustomLoadFunction.storyName = 'Custom `load` Function';