dev-resources.site
for different kinds of informations.
useGeoPosition Hook - A Custom React Hook to grab latitude and longitude from a given address.
Published at
11/13/2019
Categories
customhooks
reacthooks
react
geocodingapi
Author
shimjudavid
Author
11 person written this
shimjudavid
open
You might have encountered this requirement commonly where you want to display a google map on a listing details page and show a marker on it based on the address of the listing or a location. Here we want to pass the address of a listing or a location and you want the latitude and longitude as a return. Using Google's Geocoding API, we can extract this logic as a custom hook called useGeoPosition.
useGeoPosition hook-
This hook accepts two parameters -
- key - that's your Google Geocoding API key
- address- that's the address of your location where you want its latitude and longitude in return
import React, { useState, useEffect} from 'react'
import axios from 'axios';
const useGeoPosition = (key, address) => {
const [position, setPosition] = useState({ lat: null, lng: null });
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
const fetchLatandLng = async () => {
try {
setLoading(true);
const res = await axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${key}`);
const result = res.data.results[0].geometry.location;
if (result.lat !== null && result.lng !== null) {
setPosition({lat: result.lat, lng: result.lng})
}
else {
setError(true);
}
setLoading(false);
}
catch (error) {
setLoading(false);
setError(true);
}
}
useEffect(() => {
fetchLatandLng();
}, [address])
return [position, loading, error]
}
export default useGeoPosition;
You can now call this hook in your React component as
const [position, geoloading, geoerror] = useGeoPosition(<PassYourGoogleKey>,'Boyd St,Long Beach');
customhooks Article's
29 articles in total
Mastering Navigation Control in React with useCallbackPrompt and useBlocker 🚦
read article
Custom Hooks in React: Reusing Logic Across Components
read article
Fixing UI Update Issues with Custom Hooks in React
read article
Custom Hooks in React: A Guide to Creation and Usage
read article
React: leveraging custom hooks to extract reusable logic
read article
Custom Hooks
read article
Custom Hooks in React
read article
Mobile Orientation (React-Native)
read article
Reusing Logic in React with Custom Hooks: a Practical guide
read article
Harnessing Firebase in React with Custom Hooks: A Practical Guide
read article
Building Powerful React Components with Custom Hooks
read article
React Custom Hooks (useNetworkStatus)
read article
React Custom Hooks (useMediaPermission)
read article
React Custom Hooks (useDebounce)
read article
How to Create a Counter App with useReducer and Custom Hooks
read article
How to create custom Hooks in React.Js?
read article
React Slideshow Component with autoplay, fullscreen mode and expand all
read article
React Portals: create and open modals with keyboard keys
read article
Learn Something on React JSX and Hooks
read article
How to create Tabs in ReactJs using Hooks ?
read article
Simple hook to handle featue flags
read article
React custom hooks to update form data
read article
useIpAdrress()
read article
Custom hook useScroll.tsx :: React TypeScript
read article
useQuery() with React Router Dom
read article
React Custom Hooks
read article
useGeoPosition Hook - A Custom React Hook to grab latitude and longitude from a given address.
currently reading
`useBackButton` hook to handle back button behavior in React Native
read article
Form in Modal using React hooks – mistakes and lesson learnt
read article
Featured ones: