get location name from latitude and longitude javascript

get location name from latitude and longitude javascript – get city name from latitude and longitude in php using geocode api Example With demo.

get location name from latitude and longitude javascript

In this best Js Post, i will learn how to get the city name of the user using reverse geocoding. How to convert getCurrentPosition results to city name for free using ReverseGeocoding API?

Implementing BigDataCloud’s Free Client-side Reverse GeoLocation Client API

HTML Code





    

Click the button to get Location.

Try with and without providing a permission to access your location.



JavaScript Code

    var result = document.getElementById("json-result");
    const Http = new XMLHttpRequest();
    function getLocation() {
        var bdcApi = "https://api.bigdatacloud.net/data/reverse-geocode-client"

        navigator.geolocation.getCurrentPosition(
            (position) => {
                bdcApi = bdcApi
                    + "?latitude=" + position.coords.latitude
                    + "&longitude=" + position.coords.longitude
                    + "&localityLanguage=en";
                getApi(bdcApi);

            },
            (err) => { getApi(bdcApi); },
            {
                enableHighAccuracy: true,
                timeout: 5000,
                maximumAge: 0
            });
    }
    function getApi(bdcApi) {
        Http.open("GET", bdcApi);
        Http.send();
        Http.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                result.innerHTML = this.responseText;
            }
        };
    }

CSS Code

p {font-family: Arial, Helvetica, sans-serif;}

Don’t Miss : Get User Location Javascript

Output

{
  "latitude": 22.309999465942383,
  "longitude": 70.83000183105469,
  "lookupSource": "ip geolocation",
  "plusCode": "7JJG8R5J+X2",
  "localityLanguageRequested": "en",
  "continent": "Asia",
  "continentCode": "AS",
  "countryName": "India",
  "countryCode": "IN",
  "principalSubdivision": "Gujarat",
  "principalSubdivisionCode": "IN-GJ",
  "city": "",
  "locality": "Rajkot Taluka",
  "postcode": "",
  "localityInfo": {
    "administrative": [
      {
        "order": 2,
        "adminLevel": 2,
        "name": "India",
        "description": "country in South Asia",
        "isoName": "India",
        "isoCode": "IN",
        "wikidataId": "Q668",
        "geonameId": 1269750
      },
      {
        "order": 4,
        "adminLevel": 4,
        "name": "Gujarat",
        "description": "state of India",
        "isoName": "Gujarāt",
        "isoCode": "IN-GJ",
        "wikidataId": "Q1061",
        "geonameId": 1270770
      },
      {
        "order": 6,
        "adminLevel": 5,
        "name": "Rajkot district",
        "description": "district of Gujarat, India",
        "wikidataId": "Q1815245",
        "geonameId": 1258849
      },
      {
        "order": 7,
        "adminLevel": 6,
        "name": "Rajkot Taluka",
        "description": "Taluka in Rajkot district, Gujarat, India",
        "wikidataId": "Q25565187"
      }
    ],
    "informative": [
      {
        "order": 1,
        "name": "Asia",
        "description": "continent on Earth, mainly on the Earth's northeastern quadrant",
        "isoCode": "AS",
        "wikidataId": "Q48",
        "geonameId": 6255147
      },
      {
        "order": 3,
        "name": "Indian subcontinent",
        "description": "peninsular region in south-central Asia below the Himalayas",
        "wikidataId": "Q60140",
        "geonameId": 6269134
      },
      {
        "order": 5,
        "name": "Kathiawar",
        "description": "peninsula",
        "wikidataId": "Q1408929"
      }
    ]
  }
}

I hope you get an idea about get location name from latitude and longitude javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment