Annotations in React Maps component

3 Mar 202316 minutes to read

Annotations are used to mark the specific area of interest in the Maps with texts, shapes, or images. Any number of annotations can be added to the Maps component.

Annotation

By using the content property of AnnotationsDirective, text content or id of an element or an HTML string can be specified to render a new HTML element in Maps.

<script id='annotation' type="text/x-template">
    <div id='template'>
        <img style="width:50px;height:50px" src='https://ej2.syncfusion.com/react/demos/src/maps/images/weather-clear.png'>
    </div>
</script>

import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';

export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content="#annotation" x="0%" y="50%"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

Annotation customization

Changing the z-index

The stack order of an annotation element can be changed using theĀ zIndex property in the AnnotationsDirective.

import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';

export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content='<div id="first"><h1>Maps</h1></div>' x="0%" y="50%" zIndex="-1"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';

export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content='<div id="first"><h1>Maps</h1></div>' x="0%" y="50%" zIndex="-1"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

Positioning an annotation

Annotations can be placed anywhere in the Maps by specifying pixel or percentage values to the x and y properties in the AnnotationsDirective.

import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';
export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content='<div id="first"><h1>Maps</h1></div>' x="20%" y="50%" zIndex="-1"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';
export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content='<div id="first"><h1>Maps</h1></div>' x="20%" y="50%" zIndex="-1"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

Alignment of an annotation

Annotations can be aligned using the horizontalAlignment and verticalAlignment properties in the AnnotationsDirective. The possible values can be Center, Far, Near and None.

import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';
export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content='<div id="first"><h1>Maps</h1></div>' x="20%" y="50%" zIndex="-1"
                                     verticalAlignment="Center" horizontalAlignment="Center"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';
export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content='<div id="first"><h1>Maps</h1></div>' x="20%" y="50%" zIndex="-1"
                                     verticalAlignment="Center" horizontalAlignment="Center"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

Multiple Annotation

Multiple annotations can be added to the Maps by adding multiple AnnotationDirective in the AnnotationsDirective and customization for the annotations can be done with the AnnotationDirective.

import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';
export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content='<div id="first"><h1>Maps-Annotation</h1></div>' x="50%" y="0%" zIndex="-1"/>
                <AnnotationDirective content='<div id="first"><h1>Maps</h1></div>' x="20%" y="50%" zIndex="-1"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject } from '@syncfusion/ej2-react-maps';
import { AnnotationsDirective, AnnotationDirective, Annotations} from '@syncfusion/ej2-react-maps';
export function App() {
    return(
        <MapsComponent >
        <Inject services={[Annotations]}/>
            <AnnotationsDirective>
                <AnnotationDirective content='<div id="first"><h1>Maps-Annotation</h1></div>' x="50%" y="0%" zIndex="-1"/>
                <AnnotationDirective content='<div id="first"><h1>Maps</h1></div>' x="20%" y="50%" zIndex="-1"/>
            </AnnotationsDirective>
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);