66

I would like to know how I can get the country name from latitude & longitude using JavaScript. I am open to use of Google Maps’ JavaScript API. Can I also get city and zip?

Edit: My aim is to fill up the address field automatically, not to display it on a map.

aWebDeveloper
  • 33,798
  • 37
  • 161
  • 232
  • http://code.google.com/apis/maps/documentation/geocoding/ – deceze Dec 21 '10 at 09:18
  • 2
    See also **[Given the lat/long coordinates, how can we find out the city/country?](http://stackoverflow.com/questions/6159074/given-the-lat-long-coordinates-how-can-we-find-out-the-city-country)** – hippietrail Jul 31 '12 at 12:35

6 Answers6

77

I don't know if it works with google maps, but there is a web service that returns a country code and takes as parameters the lat and long.

Here is an example:
http://api.geonames.org/countryCodeJSON?lat=49.03&lng=10.2&username=demo

Returns a JSON data: {"languages":"de","distance":"0","countryCode":"DE","countryName":"Germany"}

I also found a little description:

The iso country code of any given point.

  • Webservice Type: REST
  • Url: ws.geonames.org/countryCode?
  • Parameters: lat, lng, type, lang, radius (buffer in km for closest country in coastal areas)
  • Result: returns the iso country code for the given latitude/longitude

With the parameter type=xml this service returns an xml document with iso country code and country name. The optional parameter lang can be used to specify the language the country name should be in. JSON output is produced with type=JSON

See the docs

Edit: Please note that demo is just a demonstration user and you should create a user account at http://www.geonames.org/login in order to use the service.

topher
  • 1,199
  • 4
  • 15
  • 34
Christian
  • 4,285
  • 5
  • 40
  • 69
16

Google Geocoding API provides these results in JSON format. It has a free tier but you will need to pay for unlimited requests to the API.

The API link to the request will look like this:

'https://maps.googleapis.com/maps/api/geocode/json?latlng=11.2742848,75.8013801&key=YOUR_API_KEY_HERE'
Dr. Younes Henni
  • 1,386
  • 1
  • 13
  • 20
Nishad Up
  • 3,057
  • 1
  • 25
  • 30
  • What do you mean by unlimited access? – CENT1PEDE Jan 27 '16 at 13:23
  • 1
    The Google Maps Geocoding API has some limitations for free usage. such as maximum free requests per day is 2,500. and maximum free request per second is 10. for more details you can visit https://developers.google.com/maps/documentation/geocoding/usage-limits – Nishad Up Jan 28 '16 at 06:23
  • 1
    Does not work unless there are points of interest near the coordinate. If not, then nothing is returned. – tar Sep 13 '16 at 19:47
  • It doesn't work anymore. See: { "error_message" : "You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account", "results" : [], "status" : "REQUEST_DENIED" } – Yunhe Oct 10 '19 at 21:37
  • I updated the response as of November 2019. You must add your API KEY to the URL to hit the API endpoint. – Dr. Younes Henni Nov 09 '19 at 12:10
12

If a self-contained library (i.e. no server / internet connection, and fast) is desirable, and only country information is required, the following library based on openstreetmap data can be useful - https://github.com/hlaw/codegrid-js

<script type="text/javascript" src="[path]/codegrid.js"></script>
grid = codegrid.CodeGrid(); // initialize

grid.getCode (lat, lng, function (err, code) { ... });

The callback is called with code, the country code for the latlng.

headuck
  • 2,753
  • 15
  • 19
8
<form id="form1" runat="server">
<div>
    <script type="text/javascript" src="http://j.maxmind.com/app/geoip.js" ></script>
    <br />Country Code:
    <script type="text/javascript">document.write(geoip_country_code());</script>
    <br />Country Name:
    <script type="text/javascript">document.write(geoip_country_name());</script>
    <br />City:
    <script type="text/javascript">document.write(geoip_city());</script>
    <br />Region:
    <script type="text/javascript">document.write(geoip_region());</script>
    <br />Region Name:
    <script type="text/javascript">document.write(geoip_region_name());</script>
    <br />Latitude:
    <script type="text/javascript">document.write(geoip_latitude());</script>
    <br />Longitude:
    <script type="text/javascript">document.write(geoip_longitude());</script>
    <br />Postal Code:
    <script type="text/javascript">document.write(geoip_postal_code());</script>

</div>
</form>
JJJ
  • 32,246
  • 20
  • 88
  • 102
Krishna Patel
  • 137
  • 1
  • 2
  • 2
    Doesn't seem to be working, since the http://j.maxmind.com/app/geoip.js script isn't loading anymore. – xarlymg89 Aug 03 '18 at 09:46
5

Yes, you can use google maps to find the country for given latitudes and longitudes. Use the reverse geocoding service.

You can get the country, city, zip code and the complete address from the response you get from the server. Check the example from the link for details ;)

aniri
  • 1,835
  • 1
  • 18
  • 29
0

If you are using golang you can use one of these 2 libraries to get the country and you don't need to even need make an api call as the data set is inbuilt so its unlimited.

https://github.com/SocialHarvest/geobed

https://github.com/bradfitz/latlong