<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: prasanna-emmadi</title>
    <description>The latest articles on DEV Community by prasanna-emmadi (@prasannaemmadi).</description>
    <link>https://dev.to/prasannaemmadi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F892374%2F968540b8-cf6b-4acb-850b-e677b02b72e2.png</url>
      <title>DEV Community: prasanna-emmadi</title>
      <link>https://dev.to/prasannaemmadi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prasannaemmadi"/>
    <language>en</language>
    <item>
      <title>How to use Browser location in React application</title>
      <dc:creator>prasanna-emmadi</dc:creator>
      <pubDate>Fri, 15 Jul 2022 15:43:37 +0000</pubDate>
      <link>https://dev.to/prasannaemmadi/how-to-use-browser-location-in-react-application-4cb0</link>
      <guid>https://dev.to/prasannaemmadi/how-to-use-browser-location-in-react-application-4cb0</guid>
      <description>&lt;p&gt;Hi&lt;/p&gt;

&lt;p&gt;How to get user location from Browser navigator location api in a react component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Map = (props) =&amp;gt; {
  // get the location from geolocation
  const [latLng, setLatLng] = useState({
    lat: 0.0,
    lng: 0.0,
    isLoaded: false,
  });

  useEffect(() =&amp;gt; {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        (position) =&amp;gt; {
          setLatLng({
            lat: position.coords.latitude,
            lng: position.coords.longitude,
            isLoaded: true,
          });
        },
        (error) =&amp;gt; {
          alert(error);
        }
      );
    }
  }, [setLatLng]);

  const lat = "latitude is : " + latLng.lat;
  const long = "longitude is : " + latLng.lng; 
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;{lat}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;{lng}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to use Leaflet control geocoder with React</title>
      <dc:creator>prasanna-emmadi</dc:creator>
      <pubDate>Fri, 15 Jul 2022 09:16:37 +0000</pubDate>
      <link>https://dev.to/prasannaemmadi/how-to-use-leaflet-control-geocoder-with-react-5e21</link>
      <guid>https://dev.to/prasannaemmadi/how-to-use-leaflet-control-geocoder-with-react-5e21</guid>
      <description>&lt;p&gt;Hi&lt;/p&gt;

&lt;p&gt;This is my first post. I just wanted to write about how to use leaflet-control-geocoder with Open street Map view in react.&lt;/p&gt;

&lt;p&gt;My requirement was to get latitude and longitude from a given address. &lt;/p&gt;

&lt;p&gt;After browsing through stackoverflow and lot of googling. I stumbled upon the &lt;a href="https://www.liedman.net/leaflet-control-geocoder/docs/classes/nominatim.html"&gt;https://www.liedman.net/leaflet-control-geocoder/docs/classes/nominatim.html&lt;/a&gt;, which seemed to solve my issue. &lt;/p&gt;

&lt;p&gt;The packages needs to be added to the create react app are&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install leaflet react-leaflet leaflet-control-geocoder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gist of code which worked for me is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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(() =&amp;gt; {
    // 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) =&amp;gt; {
      const address = positionInfo.address;
      if (address) {
        geocoder.geocode(address, (resultArray) =&amp;gt; {
          if (resultArray.length &amp;gt; 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;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The corresponding usage in Map&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from "react";
import { MapContainer, TileLayer } from "react-leaflet";
import LeafletControlGeocoder from "./LeafletControlGeodecoder";

const Map = (props) =&amp;gt; {
  const { positionInfos } = props;
  // get the location from geolocation
  const [latLng, setLatLng] = useState({
    lat: 0.0,
    lng: 0.0,
    isLoaded: false,
  });

  useEffect(() =&amp;gt; {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        (position) =&amp;gt; {
          setLatLng({
            lat: position.coords.latitude,
            lng: position.coords.longitude,
            isLoaded: true,
          });
        },
        (error) =&amp;gt; {
          alert(error);
        }
      );
    }
  }, [setLatLng]);

  return (
    &amp;lt;MapContainer center={[latLng.lat, latLng.lng]} zoom={13}&amp;gt;
      &amp;lt;TileLayer
        attribution='&amp;amp;copy; &amp;lt;a href="http://osm.org/copyright"&amp;gt;OpenStreetMap&amp;lt;/a&amp;gt; contributors'
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      /&amp;gt;
      &amp;lt;LeafletControlGeocoder positionInfos={positionInfos} /&amp;gt;
    &amp;lt;/MapContainer&amp;gt;
  );
};

export default Map;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>javascript</category>
      <category>map</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
