How to use Leaflet control geocoder with React
prasanna-emmadi
Posted on July 15, 2022
Hi
This is my first post. I just wanted to write about how to use leaflet-control-geocoder with Open street Map view in react.
My requirement was to get latitude and longitude from a given address.
After browsing through stackoverflow and lot of googling. I stumbled upon the https://www.liedman.net/leaflet-control-geocoder/docs/classes/nominatim.html, which seemed to solve my issue.
The packages needs to be added to the create react app are
npm install leaflet react-leaflet leaflet-control-geocoder
The gist of code which worked for me is
import { useEffect } from "react";
import { useMap } from "react-leaflet";
import "leaflet-control-geocoder/dist/Control.Geocoder.css";
import "leaflet-control-geocoder/dist/Control.Geocoder.js";
import L from "leaflet";
import icon from "./constants";
// shape of the props
// {
// positionInfos: [{address: "some address"}]
// }
export default function LeafletControlGeocoder(props) {
const map = useMap();
const { positionInfos } = props;
useEffect(() => {
// creaet Geocoder nominatim
var geocoder = L.Control.Geocoder.nominatim();
// for every positionInfo
// get the geocordinates of the address in the positionInfo
// use the latitude and longitude to create a marker
// and add it the map
positionInfos.map((positionInfo) => {
const address = positionInfo.address;
if (address) {
geocoder.geocode(address, (resultArray) => {
if (resultArray.length > 0) {
const result = resultArray[0];
const latlng = result.center;
L.marker(latlng, { icon }).addTo(map).bindPopup(result.name);
map.fitBounds(result.bbox);
}
});
}
});
}, [positionInfos]);
return null;
}
The corresponding usage in Map
import { useState, useEffect } from "react";
import { MapContainer, TileLayer } from "react-leaflet";
import LeafletControlGeocoder from "./LeafletControlGeodecoder";
const Map = (props) => {
const { positionInfos } = props;
// get the location from geolocation
const [latLng, setLatLng] = useState({
lat: 0.0,
lng: 0.0,
isLoaded: false,
});
useEffect(() => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
setLatLng({
lat: position.coords.latitude,
lng: position.coords.longitude,
isLoaded: true,
});
},
(error) => {
alert(error);
}
);
}
}, [setLatLng]);
return (
<MapContainer center={[latLng.lat, latLng.lng]} zoom={13}>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<LeafletControlGeocoder positionInfos={positionInfos} />
</MapContainer>
);
};
export default Map;
💖 💪 🙅 🚩
prasanna-emmadi
Posted on July 15, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.