I'm still new to Maps and JS but I've read up on JS variables and scope. I've 
tried to store a result (location_type) as a global but I can't seem to get it 
to work outside of the geocoder callback.  Here is my code; the location_type 
variable is set as a global up top and the value is set inside the callback.  
It evaluates just fine in the conditional statements and prints in the first 
Alert (inside the callback).  But the second alert prints 'undefined' and 
conditionals outside the geocoder don't evaluate.

I would really appreciate it if someone can tell me where my error is, rather 
than pointing me to reference docs.  Thanks very much.

        <script type="text/javascript">
                var geocoder;
                var map;
                var msg;
                var location_type;
                function initialize() {
                        geocoder = new google.maps.Geocoder();
                        geocoder.geocode( { 'address': address}, 
function(results, status) {
                                if (status == google.maps.GeocoderStatus.OK) {
                                        
map.setCenter(results[0].geometry.location);
                                        location_type = 
results[0].geometry.location_type;
                                        if (location_type == 'ROOFTOP') {msg = 
"here it is: " + address; }
                                        else if (location_type == 
'RANGE_INTERPOLATED') {msg = 'Not an exact match for '+address+' but this is 
probably it';}
                                        else if (location_type == 
'GEOMETRIC_CENTER') {msg = 'Sorry, could not find '+address+' but here is the 
street';}
                                        else if (location_type == 
'APPROXIMATE') {msg = 'Sorry, could not find '+address+' but this is close';}
                                        else {msg = 'Sorry, no luck at all';}
                                        
document.getElementById("map2").innerHTML = msg;
                                        var marker = new google.maps.Marker({
                                                map: map, 
                                                position: 
results[0].geometry.location
                                        });
                                } else {
                                        alert("Geocode was not successful for 
the following reason: " + status);
                                }
alert(location_type);
                        });
                        var myzoom = 16;
                        var myOptions = {
                                zoom: myzoom,
                                mapTypeId: google.maps.MapTypeId.ROADMAP,
                                scrollwheel: false,
                        }
                        map = new 
google.maps.Map(document.getElementById("map_canvas"), myOptions);
alert(location_type);
                }
        </script>

On Mar 30, 2010, at 3:28 AM, Marcelo wrote:

> On Mar 30, 1:19 am, stacef <[email protected]> wrote:
>> The geocoder results seem to
>> contain a ton of useful information but I can't figure out how to
>> access it elsewhere in the function; it seems to die at the completion
>> of the geoocode call.  There must be a way to access this data.
> 
> Correct! That's because the call to the geocoder is asynchonous:
> http://econym.org.uk/gmap/async.htm
> 
> Use a global variable to store the data, or pass it to another
> function before the callback function finishes executing.
> This is not really a Maps API topic.
> 
> --
> Marcelo - http://maps.forum.nu
> --
> 
> 
> 
> 
>> 
>> If possible, a code example would be of much more help than links to
>> the reference section.
>> 
>> Thanks,
>> Stace
>> 
>> On Mar 28, 9:02 am, Marcelo <[email protected]> wrote:
>> 
>> 
>> 
>>> On Mar 28, 5:32 pm, stacef <[email protected]> wrote:
>> 
>>>> Those values would work just fine for me if I weren't such a Maps/JS
>>>> newb so I apologize.  If I were writing this in PHP, it would go
>>>> something like
>> 
>>> This is not PHP.
>> 
>>>> But then I'm not sure how to set the zoom in myOptions from that
>>>> variable or how to echo out the message from the $msg variable.
>> 
>>> You don't set them in myOptions.
>> 
>>> You use them to construct a LatLngBounds 
>>> object:http://code.google.com/apis/maps/documentation/v3/reference.html#LatL...
>> 
>>> and then you use that object with the Map.fitBounds() 
>>> method:http://code.google.com/apis/maps/documentation/v3/reference.html#Map
>> 
>>> Give it a try, and if you have problems post a link to the updated
>>> page that is not working.
>> 
>>> --
>>> Marcelo -http://maps.forum.nu
>>> --
>> 
>>>> Thanks for any help and for your patience.
>> 
>>>> On Mar 26, 3:12 am, Marcelo <[email protected]> wrote:
>> 
>>>>> On Mar 24, 8:40 pm, stacef <[email protected]> wrote:
>> 
>>>>>> I'm trying to figure out how to set some variables based on geocode
>>>>>> accuracyand can't seem to find a solution.
>> 
>>>>> The invalid address returns an aproximate match on the street, and it
>>>>> has the fields:
>>>>> <partial_match>true</partial_match>
>>>>> <location_type>GEOMETRIC_CENTER</location_type>http://maps.google.com/maps/api/geocode/xml?address=6600%20Valencia%2...
>> 
>>>>> The valid address has the fields:
>>>>> <type>street_number</type>
>>>>> <location_type>ROOFTOP</location_type>http://maps.google.com/maps/api/geocode/xml?address=400%20Valencia%20...
>> 
>>>>> Aside from that, both results have a suggested viewport. Are those
>>>>> geometry values not suitable for your 
>>>>> purposes?http://code.google.com/apis/maps/documentation/v3/services.html#Geoco...
>> 
>>>>> --
>>>>> Marcelo -http://maps.forum.nu
>>>>> --
>> 
>>>>>>  I'm also new to the
>>>>>> Google Maps API.
>> 
>>>>>> For my situation, if theaccuracyis high, I would like to return a
>>>>>> high zoom level and text to indicate 'here is what you're looking
>>>>>> for.'  And if theaccuracyis low, return a low zoom setting and text
>>>>>> to indicate 'couldn't find what you wanted but here is something
>>>>>> close.'
>> 
>>>>>> I have a test site here:http://www.stacefelder.com/maptest
>> 
>>>>>> Entering "480 Valencia St, San Francisco"  - a valid address - returns
>>>>>> the map with a marker at that point and my desired zoom.  But entering
>>>>>> "4800 Valencia St, San Francisco" - an invalid address - currently
>>>>>> returns the map with the same zoom and a marker in the middle of the
>>>>>> length of Valencia St., making it appear to be the location of that
>>>>>> address. In this case I would prefer to set the zoom to a lower number
>>>>>> and somehow indicate that the marker is not at 4800 Valencia (or 480
>>>>>> Valencia, for that matter) because it doesn't exist and that the map
>>>>>> is for the next best match (in this case, just Valencia St).
>> 
>>>>>> Here is the script I'm currently using:
>>>>>>         <script type="text/javascript">
>>>>>>                 var geocoder;
>>>>>>                 var map;
>>>>>>                 function initialize() {
>>>>>>                         geocoder = new google.maps.Geocoder();
>>>>>>                         geocoder.geocode( { 'address': address}, 
>>>>>> function(results, status)
>>>>>> {
>>>>>>                                 if (status == 
>>>>>> google.maps.GeocoderStatus.OK) {
>>>>>>                                         
>>>>>> map.setCenter(results[0].geometry.location);
>>>>>>                                         var marker = new 
>>>>>> google.maps.Marker({
>>>>>>                                                 map: map,
>>>>>>                                                 position: 
>>>>>> results[0].geometry.location
>>>>>>                                         });
>>>>>>                                 } else {
>>>>>>                                         alert("Geocode was not 
>>>>>> successful for the following reason: " +
>>>>>> status);
>>>>>>                                 }
>>>>>>                         });
>>>>>>                         var myOptions = {
>>>>>>                                 zoom: 16,
>>>>>>                                 mapTypeId: google.maps.MapTypeId.ROADMAP
>>>>>>                         }
>>>>>>                         map = new 
>>>>>> google.maps.Map(document.getElementById("map_canvas"),
>>>>>> myOptions);
>>>>>>                 }
>>>>>>         </script>
>> 
>>>>>> All thoughts and suggestions welcomed.
>>>>>> Thanks very much,
>>>>>> Stace
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Google Maps JavaScript API v3" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-maps-js-api-v3?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply via email to