Search results

OpenStreetMap in React Maps component

22 Nov 2022 / 4 minutes to read

The OpenStreetMap (OSM) is the online Maps provider built by a community of developers; it is free to use under an open license. It allows to view geographical data in a collaborative way from anywhere on the earth. The OSM Maps provides small tile images based on our requests and combines those images into a single image to display the Maps area in the Maps component.

Adding OpenStreetMap

The OSM Maps can be rendered using the urlTemplate property.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps">
                <LayersDirective>
                    <LayerDirective urlTemplate='https://tile.openstreetmap.org/level/tileX/tileY.png'/>
                </LayersDirective>
            </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
            <MapsComponent id="maps">
                <LayersDirective>
                    <LayerDirective urlTemplate='https://tile.openstreetmap.org/level/tileX/tileY.png' />
                </LayersDirective>
            </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Changing the tile server of the OpenStreetMap

The OSM tile server can be changed by setting the tile URL in the urlTemplate property. For more details about the OSM tile server, refer here.

Enabling zooming and panning

The OSM Maps layer can be zoomed and panned. Zooming helps to get a closer look at a particular area on a Maps for in-depth analysis. Panning helps to move a Maps around to focus the targeted area.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Zoom, Inject } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps" zoomSettings={{ enable: true, toolbars: ['Zoom', 'ZoomIn', 'ZoomOut', 'Pan', 'Reset'] }}>
            <Inject services={[Zoom]}/>
                <LayersDirective>
                    <LayerDirective urlTemplate='https://tile.openstreetmap.org/level/tileX/tileY.png'/>
                </LayersDirective>
            </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Zoom, Maps, Inject } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
            <MapsComponent id="maps" zoomSettings = { { enable: true, toolbars: ['Zoom', 'ZoomIn', 'ZoomOut', 'Pan', 'Reset']} }>
            <Inject services={[Zoom]} />
                <LayersDirective>
                    <LayerDirective urlTemplate='https://tile.openstreetmap.org/level/tileX/tileY.png' />
                </LayersDirective>
            </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Adding markers and navigation line

Markers can be added to the layers of OSM Maps by setting the corresponding location’s coordinates of latitude and longitude using MarkerDirective tag. Navigation lines can be added on top of an OSM Maps layer for highlighting a path among various places by setting the corresponding location’s coordinates of latitude and longitude in the NavigationLineDirective tag.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, NavigationLineDirective, LayerDirective, Zoom, MarkersDirective, NavigationLine, NavigationLinesDirective, MarkerDirective, Marker, Inject } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps" zoomSettings={{ zoomFactor: 4 }} centerPosition={{ latitude: 29.394708, longitude: -94.954653 }}>
            <Inject services={[Marker, NavigationLine, Zoom]}/>
                <LayersDirective>
                    <LayerDirective urlTemplate='https://tile.openstreetmap.org/level/tileX/tileY.png'>
                        <MarkersDirective>
                            <MarkerDirective visible={true} height={25} width={15} dataSource={[
    {
        latitude: 34.060620,
        longitude: -118.330491,
        name: "California"
    },
    {
        latitude: 40.724546,
        longitude: -73.850344,
        name: "New York"
    }
]}>
                            </MarkerDirective>
                        </MarkersDirective>
                        <NavigationLinesDirective>
                            <NavigationLineDirective visible={true} latitude={[34.060620, 40.724546]} longitude={[-118.330491, -73.850344]} color="blue" angle={90} width={5}/>
                        </NavigationLinesDirective>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, NavigationLineDirective, LayerDirective, Zoom, MarkersDirective, NavigationLine, NavigationLinesDirective, MarkerDirective, Marker, Inject, Maps } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
            <MapsComponent id="maps" zoomSettings = { { zoomFactor: 4 } } centerPosition = {{ latitude: 29.394708, longitude: -94.954653}}>
            <Inject services={[Marker, NavigationLine, Zoom]} />
                <LayersDirective>
                    <LayerDirective urlTemplate='https://tile.openstreetmap.org/level/tileX/tileY.png'>
                        <MarkersDirective>
                            <MarkerDirective visible={true}
                                            height={25}
                                            width={15}
                                            dataSource={[
                                                {
                                                    latitude: 34.060620,
                                                    longitude: -118.330491,
                                                    name: "California"
                                                },
                                                {
                                                    latitude: 40.724546,
                                                    longitude: -73.850344,
                                                    name: "New York"
                                                }
                                            ]}
                                        >
                            </MarkerDirective>
                        </MarkersDirective>
                        <NavigationLinesDirective>
                            <NavigationLineDirective visible={true}
                                                     latitude={[34.060620, 40.724546]}
                                                     longitude={[-118.330491,-73.850344]}
                                                     color="blue"
                                                     angle={90}
                                                     width={5} />
                        </NavigationLinesDirective>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Adding sublayer

Any GeoJSON shape can be rendered as a sublayer on top of the OSM Maps layer for highlighting a particular continent or country in OSM Maps by adding another layer and specifying the type property of Maps layer to SubLayer.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { africa_continent } from 'africa-continent.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps">
                <LayersDirective>
                    <LayerDirective urlTemplate='https://tile.openstreetmap.org/level/tileX/tileY.png'/>
                    <LayerDirective shapeData={africa_continent} type='SubLayer' shapeSettings={{
    fill: 'blue'
}}/>
                </LayersDirective>
            </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { africa_continent } from 'africa-continent.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
            <MapsComponent id="maps">
                <LayersDirective>
                    <LayerDirective urlTemplate='https://tile.openstreetmap.org/level/tileX/tileY.png' />
                    <LayerDirective shapeData= {africa_continent}
                                    type= 'SubLayer'
                                    shapeSettings= {{
                                        fill: 'blue'
                                    }}
                    />
                </LayersDirective>
            </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Enabling legend

The legend can be added to the tile Maps by setting the visible property of legendSettings to true.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { markerDataSource } from 'markerdata.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, MarkersDirective, MarkerDirective, Marker, Legend, Inject } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps" legendSettings={{
    visible: true,
    type: 'Markers',
    useMarkerShape: true,
    toggleLegendSettings: {
        enable: true,
        applyShapeSettings: false,
        border: {
            color: 'green',
            width: 2,
        },
    },
}}>
              <Inject services={[Marker, Legend]}/>
              <LayersDirective>
                <LayerDirective urlTemplate="https://tile.openstreetmap.org/level/tileX/tileY.png">
                  <MarkersDirective>
                    <MarkerDirective visible={true} dataSource={markerDataSource} colorValuePath="color" shapeValuePath="shape" legendText="country"></MarkerDirective>
                  </MarkersDirective>
                </LayerDirective>
              </LayersDirective>
            </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { markerDataSource } from 'markerdata.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, MarkersDirective, MarkerDirective, Marker, Legend, Inject } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
            <MapsComponent
              id="maps"
              legendSettings={{
                visible: true,
                type: 'Markers',
                useMarkerShape: true,
                toggleLegendSettings: {
                  enable: true,
                  applyShapeSettings: false,
                  border: {
                    color: 'green',
                    width: 2,
                  },
                },
              }}
            >
              <Inject services={[Marker, Legend]} />
              <LayersDirective>
                <LayerDirective urlTemplate="https://tile.openstreetmap.org/level/tileX/tileY.png">
                  <MarkersDirective>
                    <MarkerDirective
                      visible={true}
                      dataSource={markerDataSource}
                      colorValuePath="color"
                      shapeValuePath="shape"
                      legendText="country"
                    ></MarkerDirective>
                  </MarkersDirective>
                </LayerDirective>
              </LayersDirective>
            </MapsComponent>,
document.getElementById("maps") as HTMLElement
);