How can I help you?
Print in React Maps component
6 Feb 202624 minutes to read
The rendered map can be printed directly from the browser by calling the print method. To use the print functionality, the Print module must be injected into the Maps using the Inject services={[]} tag and set the allowPrint property must be set to true.
<MapsComponent id="maps">
<Inject services={[Print]}/>
<MapsComponent>import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, Inject, Print, DataLabel } from '@syncfusion/ej2-react-maps';
export function App() {
var mapsInstance;
function clickHandler(){
mapsInstance.print();
}
return (<div>
<ButtonComponent onClick= { clickHandler}>print</ButtonComponent>
<MapsComponent allowPrint={true} ref={g => mapsInstance = g}>
<Inject services={[DataLabel, Print]} />
<LayersDirective>
<LayerDirective shapeData={world_map}
dataLabelSettings={ {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
} }>
</LayerDirective>
</LayersDirective>
</MapsComponent></div>);
}
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 { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, Inject, Print, DataLabel } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance : MapsComponent;
function clickHandler(){
mapsInstance.print();
}
return (<div>
<ButtonComponent onClick= { clickHandler}>print</ButtonComponent>
<MapsComponent allowPrint={true} ref={g => mapsInstance = g}>
<Inject services={[DataLabel, Print]} />
<LayersDirective>
<LayerDirective shapeData={world_map}
dataLabelSettings={ {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
} }>
</LayerDirective>
</LayersDirective>
</MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);Export
Image Export
To use the image export functionality in Maps, ImageExport module must be injected into the Maps using Inject services={[ImageExport]} tag and set the allowImageExport property to true.
<MapsComponent id="maps">
<Inject services={[ImageExport]}/>
<MapsComponent>The rendered Maps can be exported as an image using the export method. This method requires two parameters: image type and file name. The map can be exported as an image in the following formats.
- JPEG
- PNG
- SVG
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
var mapsInstance;
function clickHandler(){
mapsInstance.export('PNG', 'Maps');
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<MapsComponent allowImageExport={true} ref={g => mapsInstance = g}>
<Inject services={[DataLabel, ImageExport]} />
<LayersDirective>
<LayerDirective shapeData={world_map}
dataLabelSettings={ {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
} }>
</LayerDirective>
</LayersDirective>
</MapsComponent></div>);
}
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 { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance: MapsComponent;
function clickHandler(){
mapsInstance.export('PNG', 'Maps');
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<MapsComponent allowImageExport={true} ref={g => mapsInstance = g}>
<Inject services={[DataLabel, ImageExport]} />
<LayersDirective>
<LayerDirective shapeData={world_map}
dataLabelSettings={ {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
} }>
</LayerDirective>
</LayersDirective>
</MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);Exporting Maps as base 64 string of the file
The map can be exported as a base64 string for the JPEG and PNG formats. The rendered map can be exported as a base64 string using the export method. This method requires four parameters: image type, file name, orientation (set to null for image export), and allowDownload (set to false to return the base64 string).
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
var mapsInstance;
function clickHandler(){
mapsInstance.export('PNG', 'Maps', null, false).then((data)=>{
document.getElementById('data').innerHTML = data;
})
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<MapsComponent allowImageExport={true} ref={g => mapsInstance = g}>
<Inject services={[DataLabel, ImageExport]} />
<LayersDirective>
<LayerDirective shapeData={world_map}
dataLabelSettings={ {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
} }>
</LayerDirective>
</LayersDirective>
</MapsComponent><div id="data"></div></div>);
}
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 { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance: MapsComponent;
function clickHandler(){
mapsInstance.export('PNG', 'Maps', null, false).then((data)=>{
document.getElementById('data').innerHTML = data;
})
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<MapsComponent allowImageExport={true} ref={g => mapsInstance = g}>
<Inject services={[DataLabel, ImageExport]} />
<LayersDirective>
<LayerDirective shapeData={world_map}
dataLabelSettings={ {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
} }>
</LayerDirective>
</LayersDirective>
</MapsComponent><div id="data"></div></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);PDF Export
To use the PDF export functionality, PdfExport module must be injected into the Maps using Inject services={[PdfExport]} method and set the allowPdfExport property to true.
<MapsComponent id="maps">
<Inject services={[PdfExport]}/>
<MapsComponent>The rendered map can be exported as a PDF using the export method. The export method requires three parameters: file type, file name, and orientation of the PDF document. The orientation setting is optional, where 0 indicates portrait orientation and 1 indicates landscape orientation.
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, PdfExport } from '@syncfusion/ej2-react-maps';
export function App() {
var mapsInstance;
function clickHandler(){
mapsInstance.export('PDF', 'Maps');
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<MapsComponent allowPdfExport={true} ref={g => mapsInstance = g}>
<Inject services={[DataLabel, PdfExport]} />
<LayersDirective>
<LayerDirective shapeData={world_map}
dataLabelSettings={ {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
} }>
</LayerDirective>
</LayersDirective>
</MapsComponent></div>);
}
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 { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, PdfExport } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance : MapsComponent;
function clickHandler(){
mapsInstance.export('PDF', 'Maps');
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<MapsComponent allowPdfExport={true} ref={g => mapsInstance = g}>
<Inject services={[DataLabel, PdfExport]} />
<LayersDirective>
<LayerDirective shapeData={world_map}
dataLabelSettings={ {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
} }>
</LayerDirective>
</LayersDirective>
</MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);The exporting of the Maps as base64 string is not supported for the PDF export.
Export the tile Maps
The rendered map with providers such as OSM, Bing, and Google static Maps can be exported using the export method. It supports the following export formats.
- JPEG
- PNG
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
var mapsInstance;
function clickHandler(){
mapsInstance.export('PNG', 'Maps');
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<MapsComponent allowImageExport={true}
ref={g => mapsInstance = g}
titleSettings={ { text: 'OSM' } }>
<Inject services={[ImageExport]} />
<LayersDirective>
<LayerDirective urlTemplate="https://tile.openstreetmap.org/level/tileX/tileY.png">
</LayerDirective>
</LayersDirective>
</MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance : MapsComponent;
function clickHandler(){
mapsInstance.export('PNG', 'Maps');
}
return (<div>
<ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
<MapsComponent allowImageExport={true}
ref={g => mapsInstance = g}
titleSettings={ { text: 'OSM' } }>
<Inject services={[ImageExport]} />
<LayersDirective>
<LayerDirective urlTemplate="https://tile.openstreetmap.org/level/tileX/tileY.png">
</LayerDirective>
</LayersDirective>
</MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);