Get Zipcode/Pincode From Latitude and Longitude Javascript and Google Maps API

  • 30-07-2022
  • 2336
  • Jquery
  • Haresh Chauhan

Get Zipcode/Pincode From Latitude and Longitude javascript and Google Maps API; Hello friends in this post I will teach you how to get zip code or Pincode from a given lat long using google Maps API. You can also say postal code. we will take the help of google Maps API to get Pincode. Sometimes the user does not define their perfect location address or does not have their address and only we have their last long. but we must need their address Pincode for communication purposes. So, in this case, we must need the full Pincode of their area. The second is if you do not have the proper address you can find it from Pincode so in this case, we must require their Pincode or zip code, or postal code. So in this post, we will find Pincode from lat long.

Using the javascript function I will take the help of google Maps and get Pincode to form the given latitude and longitude. I will make a javascript function and in this function, I will pass two arguments and these two argument is latitude and longitude.

To use google map API we must need google map API. So first go to "GOOGLE DEVELOPER" and "ENABLE" google Maps API, then create a map API key from there. there to generate a google API key will ask for some basic details like domina name and billing method etc, Fille all the details and generate your API key.

Get Zipcode/Pincode From Latitude and Longitude PHP and Google Maps API

Once You generate your API key put in the below-given function "key={YOUR API KEY}" where you see the URL at the end. that's it not you are ready to get zip or Pincode from this API.

Just call this given function, This function will return you six digits of Pincode. apart from this, you can modify this function there are many details also provided from this API you can take using pointer sign according to your need.

async getZipCode () {

    var lat = 22.5797
    var long = 88.4143

    var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + long + "&key={YOUR API KEY}";
    
    let response = await fetch(url)
    let data = await response.json()
    var results = data.results

    if (results[0]) {

        for (let j = 0; j < results[0].address_components.length; j++) {

            if (results[0].address_components[j].types[0] == 'postal_code'){

                  alert("Zip Code: " + results[0].address_components[j].short_name);
            }
        }
    }
},

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :