Markers
Mappedin SDK version 6 is currently in a beta state while Mappedin perfects new features and APIs. Open the v6 release notes to view the latest changes.
Using Mappedin JS with your own map requires a Pro license. Try a demo map for free or refer to the Pricing page for more information.
Mappedin JS allows adding and removing Markers on a map. Markers are elements containing HTML that Mappedin JS anchors to a Door, Space, Coordinate or Node. They are automatically rotated and repositioned when the camera moves.
Note that the MapView class implements the Markers interface and exposes it as MapView.Markers. Use MapView.Markers to utilize Markers' methods.
Video Walkthrough
Creating Markers
Markers are added to the map by referencing a target that can be a Door, Space or Coordinate. The following code sample adds a marker to a coordinate.
- Web
- React
mapView.Markers.add(coordinate, '<div>This is a Marker!</div>');
// https://docs.mappedin.com/react/v6/latest/functions/Marker.html
// Get all spaces with names
const spaces = mapData.getByType('space').filter(space => space.name);
// Create markers for all spaces with its space name and make them interactive
return (
<>
{spaces.map(space => (
<Marker
key={space.externalId}
target={space}
text={space.name} // marker text
options={{ interactive: true }}
/>
))}
</>
);
Anchors
Marker anchoring is set using the options
argument of Markers.add(). Options accepts a TAddMarkerOptions value, which has a member named anchor
. When provided, the point of the Marker described by the anchor is placed next to the target. For example, using center
will place the Marker's center at the target. Both a single anchor and an array of anchors can be provided. When an array is provided, the Marker is anchored to the first target that has space available to display the Marker. Anchor positions are defined in TMarkerAnchor which contains 9 values. The anchor points are as follows, with the default being center.
TMarkerAnchor
center
top
left
bottom
right
top-left
top-right
bottom-left
bottom-right
Marker content can be styled using CSS that references the anchor of the marker. This can allow the creation of a tooltip or bubble-style marker that points to its target. This is done by using the CSS Selector:
.mappedin-marker[data-anchor="<ANCHOR>"]
Where <ANCHOR>
is the anchor position. Here is an example of adding a triangle pointer to the Marker’s target of left
:
.marker:before {
content: '';
width: 0;
height: 0;
top: calc(50% - 10px);
left: -10px;
z-index: 1;
position: absolute;
border-bottom: 10px solid transparent;
border-top: 10px solid transparent;
z-index: -1;
}
.mappedin-marker[data-anchor="left"] .marker:before {
left: auto;
right: -5px;
border-left: 10px solid #333333;
}
Here are two CodeSandbox examples that demonstrate how to use markers with custom anchors:
CodeSandbox - Mappedin React
Removing Markers
Markers can be removed individually by using the Markers.remove(marker) method, passing in the marker to be removed as shown below.
- Web
- React
mapView.Markers.remove(marker);
// https://docs.mappedin.com/react/v6/latest/classes/default.Markers.html#remove
useEvent("click", (event) => {
// if a marker is clicked, remove it
if (event.markers.length > 0) {
const clickedMarkerId = event.markers[0].id;
mapView.Markers.remove(event.markers[0]);
}
})
To remove all markers from a map, call Markers.removeAll().
- Web
- React
mapView.Markers.removeAll();
// https://docs.mappedin.com/react/v6/latest/classes/default.Markers.html#removeall
mapView.Markers.removeAll();
The CodeSandbox examples below demonstrate how to add and remove markers interactively. Click on a room to place a marker, and click on an existing marker to remove it.
CodeSandbox - Mappedin JS
Marker Rank
Ranking can be added to markers to control which marker will be shown when more than one marker occupies the same space. The marker with the highest rank will be shown. If markers do not overlap, ranking will have no effect. Rank values medium
, high
and always-visible
as defined in TCollisionRankingTier.
The code below adds markers where a user clicks on the map. The marker rank is cycled with each click. If the user clicks and adds multiple markers in the same location, the marker with the highest rank is shown and lower ranking markers are hidden.
- Web
- React
const RANKS = ['medium', 'high', 'always-visible'] as const;
let rank = 0 as number;
// Act on the click event and add a marker with a cycling rank. Observe how higher ranking markers are shown when they collide with lower ranking markers.
mapView.on('click', async (event) => {
// TCollisionRankingTier contains 3 values. If rank exceeds 2, reset it to 0.
const markerTemplate = `
<div>
<style>
.marker {
display: flex;
align-items: center;
background-color: #fff;
max-height: 64px;
border: 2px solid grey;
padding: 4px 12px;
font-weight: bold;
font-family: sans-serif;
}
.marker img {
max-width: 64px;
max-height: 32px;
object-fit: contain;
margin-right: 12px;
}
</style>
<div class="marker">
<p>Marker Rank: ${RANKS[rank]}</p>
</div>
</div>`;
// Add a marker with the current rank at the clicked coordinate.
mapView.Markers.add(event.coordinate, markerTemplate, {
rank: RANKS[rank],
});
rank++;
if (rank === RANKS.length) {
rank = 0;
}
});
// https://docs.mappedin.com/react/v6/latest/functions/Marker.html
const [markers, setMarkers] = useState<{
coordinate: Mappedin.Coordinate;
text: string;
rank: Mappedin.TCollisionRankingTier;
}[]>([]);
// define available ranks and current rank state
const RANKS: Mappedin.TCollisionRankingTier[] = ['medium', 'high', 'always-visible'];
const [currentRank, setCurrentRank] = useState(0);
// handle map clicks to add a marker with a cycling rank
useEvent('click', (event) => {
if (!mapView) return;
// cycle through rank values
const newRank = (currentRank + 1) % RANKS.length;
setCurrentRank(newRank);
// create a new marker with the current rank
const newMarker = {
coordinate: event.coordinate,
text: `Marker Rank ${RANKS[newRank]}`,
rank: RANKS[newRank],
};
// add new marker to state
setMarkers((prevMarkers) => [...prevMarkers, newMarker]);
});
// render markers using the Marker component
return (
<>
{markers.map((marker, index) => (
// note: <Marker> component generates a .mappedin-marker class
<Marker
key={index}
target={marker.coordinate}
options={{
interactive: true,
rank: marker.rank
}}
>
<div style={{
borderRadius: '10px',
backgroundColor: '#fff',
padding: '5px',
boxShadow: '0px 0px 1px rgba(0, 0, 0, 0.25)',
fontFamily: 'sans-serif',
textAlign: 'center'
}}>
<p style={{
margin: 0,
fontSize: '14px'
}}>
{marker.text}
</p>
<p style={{
margin: 0,
fontSize: '10px',
color: '#666'
}}>
Rank: {marker.rank}
</p>
</div>
</Marker>
))}
</>
);
Moving Markers
Markers can be repositioned after they have been added to a map. There are two ways of doing this:
- Markers.setPosition() instantly repositions a Marker
- Markers.animateTo() animates a Marker to a new position
The Marker's position can be set to a Coordinate, Space or Door.
The animateTo
method takes an options
parameter of TAnimationOptions
that defines the animation duration and EasingCurve, which can be linear
, ease-in
, ease-out
or ease-in-out
.
- Linear: This function implies a constant rate of change. It means the animation proceeds at the same speed from start to end. There's no acceleration or deceleration, giving a very mechanical feel.
- Ease-in: This function causes the animation to start slowly and then speed up as it progresses. Initially, there's gradual acceleration, and as the function moves forward, the rate of change increases.
- Ease-out: Contrary to ease-in, ease-out makes the animation start quickly and then slow down towards the end. It begins with a faster rate of change and gradually decelerates.
- Ease-in-out: This function combines both ease-in and ease-out. The animation starts slowly, speeds up in the middle, and then slows down again towards the end. It offers a balance of acceleration and deceleration.
The code samples below displays a custom Marker when the map is first clicked. When the map is clicked again, the Marker is animated to the clicked location using the default animation options.
- Web
- React
let marker;
mapView.on('click', async (event) => {
if (marker) {
mapView.Markers.animateTo(marker, event.coordinate);
} else {
marker = mapView.Markers.add(
event.coordinate,
'Marker',
{
interactive: true,
anchor: 'right'
}
);
}
});
// https://docs.mappedin.com/react/v6/latest/functions/AnimatedMarker.html
export default function AnimatedMarkers() {
const { mapData } = useMap();
const [markerCoordinate, setMarkerCoordinate] = useState<Mappedin.Coordinate | null>(
mapData?.mapCenter || null
);
// handle map clicks to update the marker's coordinate
useEvent("click", (event) => {
setMarkerCoordinate(event.coordinate);
});
// render <AnimatedMarker/> with updated coordinates
return (
<>
{markerCoordinate && (
<AnimatedMarker
target={markerCoordinate}
duration={1000}
options={{ interactive: true, anchor: "right" }}
>
<img
src="https://k2mgpc.csb.app/logo.png"
alt="Marker"
style={{
width: "25px",
position: "relative",
top: "-10px",
left: "-13px",
}}
/>
</AnimatedMarker>
)}
</>
);
}
The CodeSandbox examples below demonstrate how to display and animate a custom marker. Click around the map to animate the marker to the clicked location using default animation options: