Geocoding Multiple Addresses

2,934 views
Skip to first unread message

Narin

unread,
Sep 22, 2009, 3:19:01 PM9/22/09
to Google Maps JavaScript API v3
Hello,

I'm trying to use a for loop to loop through an array of addresses,
geocode them, place markers on a map, and also display an infowindow.

I have an array of names and an array of addresses that I'd like to
display in the info windows.

The geocoding seems to be successful and plot the markers at the
correct points, but for some reason, it is not recognizing the update
of my global name and address variables that I am trying to place in
the info window. An example of my code can be found here:

http://personal.psu.edu/nyr5004/sample.html

I've tried several ways to try to fix this but nothing seems to work.
It doesn't make sense to me why it would not recognize the newly
assigned strings to place in the info window. I'd appreciate any
help. Thanks!

sfdude

unread,
Sep 22, 2009, 3:38:00 PM9/22/09
to Google Maps JavaScript API v3
Maybe it's because you have
the statement below, repeated twice
in your code?

" google.maps.event.addListener(marker, 'mouseover', function()
{ infowindow.open(map,marker);}); "

SFdude

Narin

unread,
Sep 22, 2009, 3:40:05 PM9/22/09
to Google Maps JavaScript API v3
Thanks for your input, but one of those is for mouseover, and one is
for mouseout.

Saxan Sartar

unread,
Sep 22, 2009, 4:33:43 PM9/22/09
to google-map...@googlegroups.com
Try this:

    function plotProviders() {
           
            var name = new Array();
            name[0] = "Arlington Foot and Ankle";
            name[1] = "Health Choices: Schum-Brady Marie MD";
            name[2] = "Metropolitan Pediatrics";
           
            var address = new Array();
            address[0] = "3500 14th St N Arlington, VA";
            address[1] = "4040 Fairfax Dr # 140 Arlington, VA";
            address[2] = "3801 Fairfax Dr. #44 Arlington, VA";
                   
              for(x=0; x < name.length; x++)
              {
                    currentProviderName = name[x];
                  currentProviderAddress = address[x];
                  geocode(currentProviderName, currentProviderAddress);
              }
        }
     
      function geocode(provider, address) {
            geocoder.geocode({
                  address: address,
                  partialmatch: true
              },
            function geocodeResult(results, status) {
            if (status == 'OK' && results.length > 0) {       

                  var contentString = ('<p style="font-family: arial; font-size: 12px; margin: 0px;"><b>' + provider +
                '</b></p><p style="font-family: arial; font-size: 11px;">' + address);
               
                var infowindow = new google.maps.InfoWindow({
                content: contentString,
                noCloseOnClick: false
                });
                 
                var marker = new google.maps.Marker({
                      map: map,
                      position: results[0].geometry.location,
                      title: provider});

                     
                google.maps.event.addListener(marker, 'mouseover', function() { infowindow.open(map,marker);});
                google.maps.event.addListener(marker, 'mouseout', function() { infowindow.close(map.marker);});

            }
            else {
              alert("Geocode was not successful for the following reason: " + status);
            }
          });

Narin

unread,
Sep 22, 2009, 5:05:31 PM9/22/09
to Google Maps JavaScript API v3
Saxan,

Thanks, this worked! Was it just a matter of passing the name
variable to the geocode function?

Saxan Sartar

unread,
Sep 22, 2009, 5:29:38 PM9/22/09
to google-map...@googlegroups.com
i think it was more of a 'function closure' issue.

[[

partialmatch: true
              },
            function geocodeResult(results, status) {
]]

versus

[[
partialmatch: true
              },
            foo();
]]

where foo() is not really a lambda expression, and cannot see the provider or name vars due to scope...

Someone correct me if this is incorrect please.

Thanks,

-Tim
Reply all
Reply to author
Forward
0 new messages