views:

297

answers:

3

I'm working with the google maps API, and testing a section which geocodes all the addresses in a database which do not have coordinates, then generates a report of which were successful, and which failed.

If any of the locations couldn't be found, it should put a form at the top of the page to allow the user to either modify the address or specify coordinates.

I know that the geocoder can call back my code with the G_GEO_UNKNOWN_ADDRESS status code, but it never uses it. I put fake addresses into the database and got this:

The coordinates of 1337 Rawrmander Rd.
Were successfully set to:
    latitude:   49.170171
    longtitude: -123.136579

The coordinates of 5678 Imaginary Lane
Were successfully set to:
    latitude:   49.170171
    longtitude: -123.136579

It just gave back the center of the city (and continues to do so no matter what the address is) instead of calling it an unknown address.

This is how I call the geocoder:

//in a for loop
setTimeout( "geocoder.getLocations(\"" +
    //address to send to google
    values[i]['house'] + " " +
    values[i]['street'] + " " +
    values[i]['streetType'] + ", " +
    "Richmond, BC, Canada\", " +
    //google returns to this function asynchronously
    "function(reply) { handleReply(processItem++, reply); } );"
    , 
    callTimeout);
callTimeout += 220;

I tried changing "Richmond, BC, Canada" to just "Richmond" in hopes that it would not know which city to pick and then return an unknown address, but instead it just picked the middle of Richmond, VA.

Does anyone know a way to "hint" at google to give me an unknown address instead of just picking anything?

+1  A: 

Check the coordinates of the address you have on hand, eg: "1234 Imaginary St, Vancouver" and then check the coordinates of just the city: "Vancouver". If they're the same, there's a problem. Unless they are living right in the very centre of town, that'd be a good indication the address doesn't work.

nickf
there are no coordinates for the address though, the point is I need google to tell me that, instead of just picking the middle of the city.
Carson Myers
I know. I meant that when you search for Imaginary St, it'll tell you the centre of town. Those will be the same coords as if you search for "Vancouver", so do two lookups and if the two results are the same, then you know Imaginary St doesn't exist. Of course this is moot, because dustmachine's answer is clearly better!
nickf
Oh, right, I see. I just put the result coordinates into maps and it took me to the center of richmond. Also, all of my fake addresses (there were more than two) produced exactly the same coordinates (unfortunately so did one of the non-fake ones that should have worked!)
Carson Myers
+4  A: 

I think what you are looking for is the Accuracy Value. When you run a geocode, look at the accuracy value that comes back. Or are we talking about different things?

For example: Using the address you provide (5678 Imaginary Lane) I get the following:

200,4,44.9774820,-93.2643510

versus using a real address (701 1st Ave N) the result is:

200,8,44.9782860,-93.2760830

Note the second value there (4=TownLevel vs 8=AddressLevel). Looking for Imaginary,MN I'm able to get the Accuracy down to 2=RegionLevel. I was able to get a 602=G_GEO_UNKNOWN_ADDRESS response (accuracy of 0=Unknown) when I looked for Imaginary,XX.

602,0,0,0
dustmachine
perfect, this is just what I was looking for. Is there a way to tell the geocoder which accuracy level I'm looking for, or is it best to just count everything below 8 or 7 to be "unknown?"
Carson Myers
oh I see, the status code just changes when the accuracy level is zero.
Carson Myers
Keep in mind that this will work most of the time - but not always.
Chris B
A: 

The Geocoder will return the status code of 602 when it really can't find a location. More often than not though - it will just take a shot in the dark and give you a result. The accuracy value will not tell you that you just got a shot in the dark. It only tells you whether your result is a country, city, address, etc. Unfortunately, there doesn't seem to be a way around this.

Chris B
The accuracy tells you the resolution of the result. I can just count anything below address level resolution as a shot in the dark
Carson Myers
Yes and that will usually work - but just keep in mind that it won't always work. For instance, if you search for "100 Pa Rd, California" the geocoder will return an address in Pennsylvania with accuracy 8(address). "Py Rd, California" gets an address in Brazil (accuracy 8). But, depending on the nature of the addresses you're geocoding, this may happen rarely enough that you'll be fine.
Chris B