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

Expose the MapsIndoors Instance so it can be used from the outside #366

Merged
merged 18 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
3 changes: 1 addition & 2 deletions packages/map-template/.github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,4 @@ To make this even easier (mostly for ourselves), there's a `deploy-to-gcloud` sc
$ npm run deploy-to-gcloud -- BUCKET_NAME
```

This script will only work if you've already authenticated using the `gsutil` CLI on your machine, and have the proper access rights to deploy to the specified bucket.

This script will only work if you've already authenticated using the `gsutil` CLI on your machine, and have the proper access rights to deploy to the specified bucket.
6 changes: 6 additions & 0 deletions packages/map-template/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.54.0] - 2024-07-04

### Added

- Expose the MapsIndoors instance so it can be used from outside.

## [1.53.1] - 2024-07-02

### Fixed
Expand Down
50 changes: 49 additions & 1 deletion packages/map-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,52 @@ Example of URL:

`directionsTo` + `directionsFrom` + `locationId` → the `directionsTo` + `directionsFrom` have priority over the `locationId`

`directionsTo` + `directionsFrom` + `externalIDs` → the `directionsTo` + `directionsFrom` have priority over the `externalIDs`
`directionsTo` + `directionsFrom` + `externalIDs` → the `directionsTo` + `directionsFrom` have priority over the `externalIDs`


## External customization of the Map Template

When using the Map Template as a React Component or as a Web Component, you can control the map and the data on it by accessing the MapsIndoors instance. To do this, listen for the `mapsIndoorsInstanceAvailable` event on the `window` object.

You can read about all the methods that can be used on the MapsIndoors Instance [here]. (https://app.mapsindoors.com/mapsindoors/js/sdk/latest/docs/mapsindoors.MapsIndoors.html)

### 1. React Component

To use the MapsIndoors instance within a React Component, you can create a `useEffect` hook that listens for the `mapsIndoorsInstanceAvailable` event on the `window` object.

```
import MapsIndoorsMap from '@mapsindoors/map-template/dist/mapsindoors-react.es.js';
import { useEffect } from 'react';

function App() {

useEffect(() => {
window.addEventListener('mapsIndoorsInstanceAvailable', () => {
window.mapsIndoorsInstance.setDisplayRule('yourLocationId', { 'polygonFillColor': '#ff69b4' })
})
}, [])

return (
<div>
<MapsIndoorsMap apiKey="yourApiKey" mapboxAccessToken="yourMapboxAccessToken" />
</div>
)
}

export default App;
```

### 2. Web Component

To use the MapsIndoors instance within a Web Component, include a script tag that listens for the `mapsIndoorsInstanceAvailable` event on the `window` object.

```
<body>
<mapsindoors-map api-key="yourApiKey" mapbox-access-token="yourMapboxAccessToken"></mapsindoors-map>
<script>
window.addEventListener('mapsIndoorsInstanceAvailable', () => {
window.mapsIndoorsInstance.setDisplayRule('yourLocationId', { 'polygonFillColor': '#ff69b4' })
})
</script>
</body>
```
9 changes: 8 additions & 1 deletion packages/map-template/src/components/Map/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ function Map({ onLocationClick, onVenueChangedOnMap, useMapProviderModule, onMap

setMapsIndoorsInstance(miInstance);

// Assign the miInstance to the mapsIndoorsInstance on the window interface.
window.mapsIndoorsInstance = miInstance;

// Create a custom event that is dispatched from the window interface.
const event = new CustomEvent('mapsIndoorsInstanceAvailable');
window.dispatchEvent(event);

// Initialize a Directions Service
const directionsService = new window.mapsindoors.services.DirectionsService(externalDirectionsProvider);
setDirectionsService(directionsService);
Expand Down Expand Up @@ -268,6 +275,6 @@ function Map({ onLocationClick, onVenueChangedOnMap, useMapProviderModule, onMap
{mapType === mapTypes.Google && <GoogleMapsMap onMapView={onMapView} onPositionControl={onPositionControlCreated} />}
{mapType === mapTypes.Mapbox && <MapboxMap onMapView={onMapView} onPositionControl={onPositionControlCreated} />}
</>)
}
};

export default Map;
Loading