Add text inside leaflet rectangle - reactjs

I'm using the following code to create a rectangle in the leaflet map.
const rectangles = [[51.49, -0.08], [51.5, -0.06]]
<Rectangle key={key} bounds={rectangle} color="green">
</Rectangle>
I want to add a text inside the rectangle like a label for the rectangle is there a way to do this?
I'm using react-leaflet library for this.

To write on the map, we can use a DivIcon from the Leaflet library added to a React-Leaflet Marker component.
Create a DivIcon with HTML
A DivIcon is an icon that can contain HTML instead of an image. We'll import the Leaflet library and create a DivIcon with our desired text.
import L from 'leaflet';
const text = L.divIcon({html: 'Your HTML text here'});
Add the DivIcon to a Marker
With the DivIcon created, we'll add it to a Marker placed in the center of a Polygon.
import React from 'react';
import L from 'leaflet';
import { Marker, Polygon } from 'react-leaflet';
const PolygonWithText = props => {
const center = L.polygon(props.coord).getBounds().getCenter();
const text = L.divIcon({html: props.text});
return(
<Polygon color="blue" positions={props.coords}>
<Marker position={center} icon={text} />
</Polygon>
);
}
export default PolygonWithText
Add the Marker to the Map
Finally, we add the Polygon, Marker, and DivIcon to a Map.
import React, { Component } from 'react';
import {Map, TileLayer} from 'react-leaflet';
import PolygonWithText from './PolygonWithText';
class MyMap extends Component {
render () {
return (
<Map center={[20.75, -156.45]} zoom={13}>
<TileLayer
attribution='&copy OpenStreetMap contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<PolygonWithText text="MyText" coords={[...]} />
</Map>
}
}
export default MyMap;

refer the below code, this one uses a rectangle with a tooltip inside it
{zoneLabel}
<Rectangle key={key} bounds={coordinates}>
</Rectangle>

Related

How to add a custom control (legend with buttons) to react-leaflet v.4 map?

I would like to add a custom Legend in react-leaflet v4 map, but i can't understand how to do it correctly. I have already read the https://react-leaflet.js.org/docs/example-react-control/ example but it is quite complicated.
Please check the image
to better understand the issue.
Map.js (Main map component)
import React from 'react'
import { MapContainer, TileLayer, Marker } from 'react-leaflet'
import MapLegendControl from './map/MapLegendControl'; //Custom control i want to add
function Map() {
return (
<>
<MapContainer
center={[37.983810, 23.727539]}
zoom={6}
style={{ height: '100%' }}
>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© OpenStreetMap contributors'
/>
<MapLegendControl map={map} />
</MapContainer>
</>
)
}
Custom Control (Legend)
import React from 'react'
function MapLegendControl({ map }) {
return (
<div className='leaflet-control leaflet-bar map-legend'>
<button> Test </button>
</div>
)
}
export default MapLegendControl;
Thank you in advance.

How to add wms layer on map using react leaflet

I need to highlight a layer on every states of india on map using react leaflet, So I'am adding WMS layer for that but after adding this code my map is not showing
<WMSTileLayer
layers={this.state.bluemarble ? 'nasa:bluemarble' : 'ne:ne'}
url="https://demo.boundlessgeo.com/geoserver/ows"
/>
kindly let me know what mistake am I making, and how to achieve this? I am taking reference from this example
my full code
import React from "react";
import { MapContainer, TileLayer, useMap, Marker, Popup, ZoomControl, FeatureGroup, Map, WMSTileLayer, Polygon } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';
import 'leaflet-draw/dist/leaflet.draw.css';
import L from 'leaflet';
import {EditControl} from 'react-leaflet-draw'
const markerIcon = new L.Icon({
iconUrl: require("../resources/placeholder.png"),
iconSize: [35, 35],
});
function Mymap(){
return(
<div className='mymap'>
<MapContainer center={[21.7679, 78.8718]} zoom={5} scrollWheelZoom={true} zoomControl={false} >
<TileLayer
attribution='© OpenStreetMap contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<ZoomControl position="topright"/>
<Marker position={[21.7679, 78.8718]} icon={markerIcon}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
<WMSTileLayer
layers={this.state.bluemarble ? 'nasa:bluemarble' : 'ne:ne'}
url="https://demo.boundlessgeo.com/geoserver/ows"
/>
</MapContainer>
</div>
);
}
export default Mymap

Map markers in react-leaflet not showing image correctly

I'm confused about creating map markers in react-leaflet; some of the documentation seems to be out of date and I've seen conflicting answers on other sites.
The default <Marker> component from the official documentation's setup, with no icon property described, displays as what looks like a rectangular indent in the map. Essentially it has a shadow but no actual marker icon. If icon is set to a const importing a certain image file, the marker displays as a squashed version of that image. I experimented with setting icon to 'leaflet/dist/images/marker-icon.png' but it looks really odd as the edges are fuzzy and the icon itself looks distorted (compressed horizontally - obviously 25x55 are the dimensions I copied from elsewhere but they don't look right as default).
What's the correct way to render a normal, default, standard map marker in react-leaflet?
My code (with the squashed icon from leaflet/dist) is here:
import './App.css';
import 'leaflet/dist/leaflet.css';
import L, { divIcon } from 'leaflet';
import bear1 from './bulgaria-bear-1.jpg';
import bear2 from './bulgaria-bear-2.jpg';
import bear3 from './bulgaria-bear-3.jpg';
import markerIcon from 'leaflet/dist/images/marker-icon.png';
import React, { useState, useEffect } from 'react';
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
[...]
function PageMap(props) {
const markerIconConst = L.icon({
iconUrl: markerIcon,
iconRetinaUrl: markerIcon,
iconAnchor: [5, 55],
popupAnchor: [10, -44],
iconSize: [25, 55],
});
return (
<div classname="App">
<MapContainer id="mapid" center={[51.505, -0.09]} zoom={13} scrollWheelZoom={false}>
<TileLayer
attribution='© OpenStreetMap contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker icon={markerIconConst} position={[51.505, -0.09]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
<p classname="App-header">
<button onClick={(() => props.navigate('Home'))}>
Home
</button>
</p>
</div>
);
As an additional question, I'm wondering why my other CSS doesn't get loaded on the map page, for example the button set as "App-header" which is meant to align with the centre. The button displays but is aligned at left. Does the MapContainer take over the whole page for its own CSS?
Your code looks correct.
The dimensions for the Leaflet marker are 24x41.
For the button style, add an uppercase "N" to className (instead of classname = "App-header").
You can find a demo here : Stackblitz
I have used in my step by step tutorial to create custom image markers using react and leaflet.
import L from 'leaflet';
export const VenueLocationIcon = L.icon({
iconUrl: require('../assets/venue_location_icon.svg'),
iconRetinaUrl: require('../assets/venue_location_icon.svg'),
iconAnchor: null,
shadowUrl: null,
shadowSize: null,
shadowAnchor: null,
iconSize: [35, 35],
className: 'leaflet-venue-icon'
});

How to add drawing toolbar in Leaflet map(React Application)

This is my Dashnoard.js file, i dont have an html file .My question is how to add the drawing toolbar on the leaflet map.At the moment i have the map only...
Thank you!
import React from 'react'
import { Map, Marker, Popup, TileLayer } from 'react-leaflet'
import { compose } from 'redux'
import "leaflet/dist/leaflet.css";
import L from 'leaflet';
export class Dashboard extends React.Component{
constructor() {
super();
this.state = {
lat: 42.696295,
lng: 23.303643,
zoom: 10,
};
}
render() {
const position = [this.state.lat, this.state.lng]
return(
<div className='dashboard container'>
<Map id="map" style={{ height: "50vh" }} center={position} zoom={13}>
<TileLayer
attribution='© OpenStreetMap contributors'
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
/>
<Marker position={position}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</Map>
</div>
)
} }
export default compose()(Dashboard);
I have successfully initialize the map on the page,but at the next step i cannto initialize na drawing comonent and set to the map.

Material UI: deck gl covers grid

I am working with React and Material-UI. I want to divide the code into two columns. In one of this, I want to render a map using deck.gl.
Apparently, deck.gl is rendered fullscreen and it covers all the screen. I even tried to set the attributes width={80} and height={80} to the DeckGL component but it is still covering the rest of the page.
App.js
import React, { Component } from 'react';
import './App.css';
import Grid from '#material-ui/core/Grid';
import Map from './Map.js';
class App extends Component {
render() {
return (
<div >
<Grid container spacing={24}>
<Grid item xs={3}>
Column text
</Grid>
<Grid item xs={9}>
<Map/>
</Grid>
</Grid>
</div>
);
}
}
export default App;
Map.js
/// app.js
import React from 'react';
import DeckGL, {PolygonLayer} from 'deck.gl';
import {StaticMap} from 'react-map-gl';
// Set your mapbox access token here
const MAPBOX_ACCESS_TOKEN = 'pk.eyJ1IjoibWZvZ2xpbyIsImEiOiJjamt2N2Z2aWkwNXJxM3BxNXo4Mmt1a3MwIn0.IqS5iv1ZmLht4hm-N0cDYg';
// Initial viewport settings
const initialViewState = {
longitude: -87.630259,
latitude: 41.873400,
zoom: 13,
pitch: 35,
bearing: 0
};
// Data to be used by the LineLayer
const data = [{sourcePosition: [-122.41669, 37.7853], targetPosition: [-122.41669, 37.781]}];
class Map extends React.Component {
render() {
const layers = [
];
return (
<DeckGL
initialViewState={initialViewState}
controller={true}
layers={layers}
>
<StaticMap
mapboxApiAccessToken={MAPBOX_ACCESS_TOKEN}
/>
</DeckGL>
);
}
}
export default Map;
9Try this.
<Grid container spacing={24}>
<Grid item xs={3}>
Column text
</Grid>
<Grid item xs={3} lg={9}>
<Map/>
</Grid>
i had the same problem, the problem is that deck.gl by default is 100% width and 100% height and position absolute so it displays on top of your grid, what i did i manually overwrote deck.gl style rules in my css with '!important' rule or the second option you can put deck.gl in another div and give that div with height because deck uses 100% its parent height and width
I use react-container-dimensions to solve this problem.
<Grid key={value} item>
<Paper className={classes.paper}>
<ContainerDimensions>
<Map />
</ContainerDimensions>
</Paper>
</Grid
You just need to give height and width to the div and render that deckGL map into that div. It will show you accordingly. Because deck always follow its parents properties.

Resources