views:

642

answers:

3

I'm using the google geocoding service to validate that a city name (plus region and country) that has been entered in our system exists, and to get the lat/long.

However, I'm finding that it seems to 'guess' if you make a typo, and returns an response even if you made an error.

For instance, a request for "Beverton, Ontario, Canada" returns the lat/long for Beaverton, with no indication that you provided the wrong city name.

I'm using the CSV response type, and am getting the 200 response code.

Can I either prevent the service from doing this, or, better yet, find out if it has?

Edit: to clarify ... Google is correcting the input (when I would expect it to just fail) and I need to know if it has done this.

A: 

Is it responding with a "200 Success Code"? There's a chance that it might be giving you a different status code.

Here are the different status codes google returns: http://code.google.com/apis/maps/documentation/reference.html#GGeoStatusCode

mga911
Yes, I am getting 200. Updated question.
Buzz
A: 

NOTE: See the answer by @Chris B above. As Chris points out, this may be impossible to implement.

Do you have to use the CSV response type? If not, the other response types such as KML provide enough details to determine the location that the coordinates refer to. You could verify your input against the response's LocalityName element.

<kml xmlns="http://earth.google.com/kml/2.0"&gt;
  <Response>
    <name>1600 amphitheatre mountain view ca</name>
    <Status>
      <code>200</code>
      <request>geocode</request>
    </Status>
    <Placemark>
      <address> 
        1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA
      </address>
      <AddressDetails Accuracy="8">
        <Country>
          <CountryNameCode>US</CountryNameCode>
          <AdministrativeArea>
            <AdministrativeAreaName>CA</AdministrativeAreaName>
           <SubAdministrativeArea>
             <SubAdministrativeAreaName>Santa Clara</SubAdministrativeAreaName>
             <Locality>
               <LocalityName>Mountain View</LocalityName>
               <Thoroughfare>
                 <ThoroughfareName>1600 Amphitheatre Pkwy</ThoroughfareName>
               </Thoroughfare>
               <PostalCode>
                 <PostalCodeNumber>94043</PostalCodeNumber>
               </PostalCode>
             </Locality>
           </SubAdministrativeArea>
         </AdministrativeArea>
       </Country>
     </AddressDetails>
     <Point>
       <coordinates>-122.083739,37.423021,0</coordinates>
     </Point>
   </Placemark>
  </Response>
</kml>
Saul Dolgin
Additionally, you can use a JSON or XML response type to get the same data.
Chris B
+3  A: 

There isn't any way for the geocoder to let you know if it thinks you had a typo. I agree with Saul's answer, that your best bet is to check your query against the response.

I just wanted to point out that you'll have to check several elements of your input against several of the response values, in order to find the elements that should match up. In this case, "Beaverton" was found inside of "DependentLocalityName".

<?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"&gt;&lt;Response&gt;
  <name>Beverton, Ontario, Canada</name>
  <Status>
    <code>200</code>
    <request>geocode</request>
  </Status>
  <Placemark id="p1">

    <address>Beaverton, Brock, ON, Canada</address>
    <AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>CA</CountryNameCode><CountryName>Canada</CountryName><AdministrativeArea><AdministrativeAreaName>ON</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Durham Regional Municipality</SubAdministrativeAreaName><Locality><LocalityName>Brock</LocalityName><DependentLocality><DependentLocalityName>Beaverton</DependentLocalityName></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>
    <ExtendedData>
      <LatLonBox north="44.4502166" south="44.4183470" east="-79.1199562" west="-79.1839858" />
    </ExtendedData>

    <Point><coordinates>-79.1519710,44.4342840,0</coordinates></Point>
  </Placemark>
</Response></kml>

Update:

This may be impossible to actually implement. If your input is "Beverton, Ontario, Canada", how do you know which of those three words to check for? Two of them will match up just fine. What if they're entered in a different order?

Chris B
This turns out to be true. Personally, I think this strange for the API to return a response for a totally different request than you provided, without telling you!
Buzz
It sounds to me like Google's geocoding service has room for improvement. Perhaps they could somehow indicate that a "fuzzy" response has been provided in the location data.
Saul Dolgin