Search code examples
google-mapsgoogle-maps-api-3google-maps-autocomplete

Is there any way to detect country phone code from google maps autocomplete api?


I am building a web app that requires to store the location of a client along with the phone number. I want the phone code to be auto-detected after location is selected. I have gone through the documentation of google maps provided in the following link but with no luck. Google Maps Documentation . Is there any way to detect phone code from google maps location?


Solution

  • Once you have identified the country you can go to an external API like https://restcountries.eu and retrieve data (including phone code) about any country.

    For example https://restcountries.eu/rest/v2/name/nepal?fullText=true returns this JSON, where you can see the callingCodes = 997.

    [{
        "name": "Nepal",
        "topLevelDomain": [".np"],
        "alpha2Code": "NP",
        "alpha3Code": "NPL",
        "callingCodes": ["977"],
        "capital": "Kathmandu",
        "altSpellings": ["NP", "Federal Democratic Republic of Nepal", "Loktāntrik Ganatantra Nepāl"],
        "region": "Asia",
        "subregion": "Southern Asia",
        "population": 28431500,
        "latlng": [28.0, 84.0],
        "demonym": "Nepalese",
        "area": 147181.0,
        "gini": 32.8,
        "timezones": ["UTC+05:45"],
        "borders": ["CHN", "IND"],
        "nativeName": "नेपाल",
        "numericCode": "524",
        "currencies": [{
            "code": "NPR",
            "name": "Nepalese rupee",
            "symbol": "₨"
        }],
        "languages": [{
            "iso639_1": "ne",
            "iso639_2": "nep",
            "name": "Nepali",
            "nativeName": "नेपाली"
        }],
        "translations": {
            "de": "Népal",
            "es": "Nepal",
            "fr": "Népal",
            "ja": "ネパール",
            "it": "Nepal",
            "br": "Nepal",
            "pt": "Nepal",
            "nl": "Nepal",
            "hr": "Nepal",
            "fa": "نپال"
        },
        "flag": "https://restcountries.eu/data/npl.svg",
        "regionalBlocs": [{
            "acronym": "SAARC",
            "name": "South Asian Association for Regional Cooperation",
            "otherAcronyms": [],
            "otherNames": []
        }],
        "cioc": "NEP"
    }]
    

    EDIT: Code added on request.

    <html>
    <body>
    <h2>Get the phone code for Napal</h2>
    
    <button type="button" onclick="loadDoc()">Get Phone Code</button>
    
    <p id="demo"></p>
    
    <script>
    function loadDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          var locObj =  JSON.parse(this.responseText);
          document.getElementById("demo").innerHTML = "Phone Code for Napal = "+locObj[0].callingCodes;
        }
      };
      xhttp.open("GET", "https://restcountries.eu/rest/v2/name/nepal?fullText=true", true);
      xhttp.send();
    }
    </script>
    
    </body>
    </html>
    

    Produces this output:

    Phone Code for Napal = 977