react get lat long of address

// install dependency with npm as seen below
// npm install react-geocode

// IMPORTANT - You will need to insert your Google Maps API on line 15 or this
// will not work, click on the link below to get started with Google Maps Platform
// https://developers.google.com/maps/gmp-get-started

// Don't forget to upvote if this helped

import React, { useState, useEffect } from 'react';
import Geocode from 'react-geocode';

export default function FindLatLong () {

  Geocode.setApiKey('<YOUR API HERE>'); //Insert your Google Maps API here
  Geocode.enableDebug();

  var address = 'Area 51, NV, USA';

  const [lat, setLat] = useState(0)
  const [long, setLong] = useState(0)

  useEffect(() => {
    Geocode.fromAddress(address).then(
      response => {
        setLat(response.results[0].geometry.location.lat);
        setLong(response.results[0].geometry.location.lng);
      }
    )}, []
  )

  return(
    <div>
      <p>Address: {address}</p>
      <p>Lat: {lat}</p>
      <p>long: {long}</p>
    </div>
  )
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source