How to Create Markers with the React Maps Component?
This article explains how to create markers with the React Maps component.
The Syncfusion React Maps component supports adding markers to represent specific geographic locations on the map. Each marker is associated with a pair of latitude and longitude coordinates, allowing users to highlight places of interest, such as cities, landmarks, or other location-based data. This section explains how to create maps with markers in a React application using the Syncfusion Maps component.
Markers are added to the map using the dataSource property within the MarkerDirective tag. This property contains a list of objects that represent the marker locations with latitude and longitude values, along with any additional data. To enable this feature, set the visible property to true in the MarkerDirective tag. The rendered markers can be customized using the properties within the MarkerDirective tag.
The following code sample demonstrates how to add a marker in React Maps.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Syncfusion React Chart-Add DataSource</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 for React Components" />
<meta name="author" content="Syncfusion" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<style>
#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
#container {
height : 500px;
display: block;
}
</style>
</head>
<body style="margin-top: 100px;">
<div id='container'>
<div id='loader'>Loading....</div>
</div>
</body>
</html>
index.js
import { world_map } from './world-map.ts';
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import {
MapsComponent,
LayersDirective,
LayerDirective,
MarkersDirective,
MarkerDirective,
Marker,
Inject,
MapsTooltip,
} from '@syncfusion/ej2-react-maps';
export function App() {
return (
<MapsComponent>
<Inject services={[Marker, MapsTooltip]} />
<LayersDirective>
<LayerDirective shapeData={world_map}>
<MarkersDirective>
<MarkerDirective
visible={true}
height={20}
width={20}
animationDuration={0}
tooltipSettings={{
visible: true,
valuePath: 'name',
}}
dataSource={[
{
latitude: 37.7749,
longitude: -122.4194,
name: 'San Francisco',
},
{
latitude: 34.0522,
longitude: -118.2437,
name: 'Los Angeles',
},
{
latitude: 59.88893689676585,
longitude: -109.3359375,
name: 'Canada',
},
{
latitude: -6.64607562172573,
longitude: -55.54687499999999,
name: 'Brazil',
},
]}
></MarkerDirective>
</MarkersDirective>
</LayerDirective>
</LayersDirective>
</MapsComponent>
);
}
const root = createRoot(document.getElementById('container'));
root.render(<App />);
The following screenshot illustrates the output of the above code snippet.
Refer to the working sample for additional details and implementation: View Sample in Stackblitz
Conclusion
We hope you enjoyed learning how to create markers with the React Maps component.
You can refer to our React Maps feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Maps example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!