ClemFr
ClemFr

Reputation: 75

Google Map API - get informations about one coordinate

I am using the javascript googlemap API. I want the user to define a city name. Then I store the name + coordinate in a database.

It is easy to do, but the problems are :

So can I know the name of the city under one specific coordinates ? Idem for country ? Can I know if there is no city or country under it ? Do you have other ideas for my problems ?

Thanks!

Upvotes: 4

Views: 4221

Answers (3)

Galen
Galen

Reputation: 30180

You do not need reverse geocoding.

Use Google's geocoder to get the lat/lng of the city. Google will return the name of the city it found in its response:

Here's a geocoding request with Paris spelled incorrectly: http://maps.googleapis.com/maps/api/geocode/json?address=Paaris&sensor=false

Google gives you the correct place in the response...

{
  "status": "OK",
  "results": [ {
    "types": [ "locality", "political" ],
    "formatted_address": "Paris, France",
    "address_components": [ {
      "long_name": "Paris",
      "short_name": "Paris",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Paris",
      "short_name": "75",
      "types": [ "administrative_area_level_2", "political" ]
    }, {

SO...

  1. User types in a city
  2. You geocode it with Google's geocoding service
  3. If geocoding is successful get the actual city from the geocode response and add it to the database with the coordinates
  4. If geocoding fails alert the user.

As a intermediate step you could also check if the entered city is different than the city google found and verify with the user.

Upvotes: 3

Philar
Philar

Reputation: 3897

I think reverse geocoding is what you are looking for. You will find details of obtaining the country, city and other location details by passing the co-ordinates here.

Upvotes: 3

kapa
kapa

Reputation: 78751

Maybe this can help you:

http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding

Upvotes: 3

Related Questions