Testing location_type of address geocoder

326 views
Skip to first unread message

Bob Heitzman

unread,
Sep 8, 2011, 11:10:12 AM9/8/11
to google-map...@googlegroups.com
I'm new to Javascript...

I'm try to test the return from a geocoder request. I want to warn the user if the location_type is not ROOFTOP. 

When I try to reference location_top I get "undefined." e.g. (with or w/o the .toString)
console.log("location_type: " + results[0].location_type.toString);
Shouldn't I be able to do this:
    if (results[0].location_type.toString != "ROOFTOP"){
        alert("An exact address match was not found - marker shows approximate location.");
    };
Also... I'm testing the top most, I assume best, response - is that a valid?

And I also assume there will always be at least one response - is that true?

Here is the response I am testing:

        1. resultsArray[1]
          1. 0Object
            1. address_componentsArray[8]
            2. formatted_address"3433 Roberto Ct, San Luis Obispo, CA 93401, USA"
            3. geometryObject
              1. locationP
              2. location_type"ROOFTOP"


Rossko

unread,
Sep 8, 2011, 2:36:42 PM9/8/11
to Google Maps JavaScript API v3
> Also... I'm testing the top most, I assume best, response - is that a valid?

That's up to your definition of 'best'

> And I also assume there will always be at least one response - is that true?

No, garbage addresses will not be found. Check for existence of
results before trying to use them.

>    1. results: Array[1]
>                1. 0: Object
>                   1. address_components: Array[8]
>                   2. formatted_address: "3433 Roberto Ct, San Luis Obispo,
>                   CA 93401, USA"
>                   3. geometry: Object
>                      1. location: P
>                      2. location_type: "ROOFTOP"

Wouldn't you want to be examining results[0].geometry.location_type ?

Bob Heitzman

unread,
Sep 8, 2011, 4:12:51 PM9/8/11
to google-map...@googlegroups.com
Thanks - geometry.location_type was correct - I'll have to watch using code from other posts....

It appears that a totally junk address returns an error ZERO RESULTS, if OK is returned I think I'm safe in that at least one record is present.

"Best" in my case is ROOFTOP so I also think it is semi-safe to assume that if multiple records are return the top most will be ROOFTOP, there may be others I suppose with ROOFTOP. I'm displaying a marker for the user to verify so.....



Andrew Leach

unread,
Sep 8, 2011, 4:59:52 PM9/8/11
to google-map...@googlegroups.com
On 8 September 2011 21:12, Bob Heitzman <rhei...@gmail.com> wrote:
> I also think it is semi-safe to assume that
> if multiple records are return the top most will be ROOFTOP, there may be
> others I suppose with ROOFTOP.

(a) It's not safe at all to assume that.
(b) Only one can be rooftop, rather by definition!

Always test all the data returned.

JKurtock

unread,
Sep 8, 2011, 11:42:37 PM9/8/11
to Google Maps JavaScript API v3
Remember that the geocoder service is asynchronous. If you test a
value and get "undefined," that is almost a Hallmark of testing the
variable before the service has returned.

One other point that might be of interest to you. When I include the
apt number in any address, I do not get "Rooftop". But if I simply
peel the apartment number off, I get "Rooftop." So don't be too quick
to assume that your "best" responses are the ones with location_type =
"Rooftop".

- Jeff

On Sep 8, 1:59 pm, Andrew Leach <andrew.leac...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages