Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get country from latitude longitude

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.

like image 340
aWebDeveloper Avatar asked Dec 21 '10 09:12

aWebDeveloper


People also ask

Can you search Google Maps with latitude and longitude?

To search for a place, enter the latitude and longitude GPS coordinates on Google Maps. You can also find the coordinates of the places you previously found. Besides longitude and latitude, you can use plus codes to share a place without an address.


2 Answers

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.

like image 136
Christian Avatar answered Sep 23 '22 19:09

Christian


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' 
like image 42
Nishad Up Avatar answered Sep 21 '22 19:09

Nishad Up