Categories
JavaScript Answers

How to fix geocoding from address to latitude and longitude numbers not working with JavaScript?

Spread the love

Sometimes, we want to fix geocoding from address to latitude and longitude numbers not working with JavaScript.

In this article, we’ll look at how to fix geocoding from address to latitude and longitude numbers not working with JavaScript.

How to fix geocoding from address to latitude and longitude numbers not working with JavaScript?

To fix geocoding from address to latitude and longitude numbers not working with JavaScript, we use the Google Maps API.

For instance, we write

<script
  type="text/javascript"
  src="http://maps.google.com/maps/api/js?sensor=false"
></script>

to add the Google Maps API script.

Then we write

const geocoder = new google.maps.Geocoder();
const address = "new york";

geocoder.geocode({ address }, (results, status) => {
  if (status === google.maps.GeocoderStatus.OK) {
    const latitude = results[0].geometry.location.lat();
    const longitude = results[0].geometry.location.lng();
    console.log(latitude);
  }
});

to call geocode with the address to geocode the address and get the resulting latitude and longitude with lat and lng respectively.

Conclusion

To fix geocoding from address to latitude and longitude numbers not working with JavaScript, we use the Google Maps API.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *