Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

  • if the user make a mistake (for example Paaris instead of Paris), google understands but I don't know the final name (the correct one), so I create (or recreate) an bad entry in the db.
  • If the user enter a country name, it works but I only want cities.

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!

like image 547
ClemFr Avatar asked Jan 21 '23 02:01

ClemFr


2 Answers

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.

like image 60
Galen Avatar answered Jan 23 '23 15:01

Galen


Maybe this can help you:

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

like image 25
kapa Avatar answered Jan 23 '23 15:01

kapa