<?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: Dhruv Singh</title>
    <description>The latest articles on DEV Community by Dhruv Singh (@dhruv09d).</description>
    <link>https://dev.to/dhruv09d</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%2F504660%2Fc81a3a9e-5a33-45da-b77b-41488692c10f.jpeg</url>
      <title>DEV Community: Dhruv Singh</title>
      <link>https://dev.to/dhruv09d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhruv09d"/>
    <language>en</language>
    <item>
      <title>Know your users' location in React Ts</title>
      <dc:creator>Dhruv Singh</dc:creator>
      <pubDate>Wed, 11 Jan 2023 12:08:39 +0000</pubDate>
      <link>https://dev.to/dhruv09d/know-your-users-location-in-react-ts-fd6</link>
      <guid>https://dev.to/dhruv09d/know-your-users-location-in-react-ts-fd6</guid>
      <description>&lt;p&gt;In this post you will see how to get user location who landed on your website.&lt;/p&gt;

&lt;p&gt;Note: It will only work when the user give their consent to share location which is by default asked by the browser.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Library used:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;react-geolocated:&lt;/strong&gt; To get user location.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;firebase:&lt;/strong&gt; To save user location in firestore.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Setup FireBase&lt;/strong&gt; &lt;br&gt;
On the dashboard of your firebase project Select the type of project here in our case it is Web.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxk285kivqe41g718ohc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxk285kivqe41g718ohc.png" alt="Image description" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Continue the project setup. Here these are the configs that you need to use in your react project to access firebase.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fae9d8cecm8hdu4gylo03.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fae9d8cecm8hdu4gylo03.jpg" alt="Image description" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a config for firebase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
  apiKey: "AIzaXXXXXXXXXXXXXXXX-XXXXXXMCQA",
  authDomain: "projectId.firebaseapp.com",
  projectId: "projectId",
  storageBucket: "projectId.appspot.com",
  messagingSenderId: "12XXXXXXX12",
  appId: "1:XXXXXXXXX:web:XXXXXXXXXXXXXXXXXXX",
  measurementId: "G-XXXXXXXXXX",
};

const app = initializeApp(firebaseConfig);
let analytics;
if (app.name &amp;amp;&amp;amp; typeof window !== 'undefined') {
    analytics = getAnalytics(app);
  }
const db = getFirestore(app);
export { db };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Get user Location&lt;/strong&gt;&lt;br&gt;
Here ddDoc(collection(db, "location"), &lt;strong&gt;location&lt;/strong&gt; is the db name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { FC, useEffect } from "react";
import Image from "next/image";
import Header from "../components/Header";
import { Box } from "@mui/material";
import { useGeolocated } from "react-geolocated";
import { db } from "../config/firebase";
import { collection, addDoc, Timestamp } from "firebase/firestore";

type Location = {
  accuracy: number | null;
  altitude: number | null;
  altitudeAccuracy: number | null;
  heading: number | null;
  latitude: number | null;
  longitude: number | null;
  speed: number | null;
};
type typeChildren = {
  children: React.ReactNode;
};

const Layout: FC&amp;lt;typeChildren&amp;gt; = ({ children }) =&amp;gt; {
// initiate geolocation
  const { coords } = useGeolocated({
    positionOptions: {
      enableHighAccuracy: true,
    },
    userDecisionTimeout: 5000,
  });
// to save location in fireStore
  const saveLocation = async (loc: Location) =&amp;gt; {
    try {
      await addDoc(collection(db, "location"), {
        latitude: loc.latitude,
        longitude: loc.longitude,
        accuracy: loc.accuracy,
        altitude: loc.altitude,
        altitudeAccuracy: loc.altitudeAccuracy,
        heading: loc.heading,
        created: Timestamp.now(),
      });
    } catch (err) {
      console.log(err);
    }
  };

  useEffect(() =&amp;gt; {
    if (!!coords?.latitude) {
      saveLocation(coords);
    }
  }, [coords]);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;Image
        src="/emojiImg.png"
        layout="fill"
        alt="emoji"
      /&amp;gt;
      &amp;lt;Header /&amp;gt;
      &amp;lt;Box&amp;gt;{children}&amp;lt;/Box&amp;gt;
    &amp;lt;/&amp;gt;
  );
};`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;br&gt;
You will be able to see the location of your user. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu13v5w58t6qdtc5u25mz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu13v5w58t6qdtc5u25mz.jpg" alt="Image description" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>architecture</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
