Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a PHP variable that contains an array of addresses. I am using the Googlemaps v3 javascript API. I have a function that can geocode an address and place the marker but I am confused how to run that function for each address stored in the php variable.

Any ideas guys?

Edit: The geodecoding function is a javascript function.

Javascript function:

<script type="text/javascript"> 
          var geocoder;
          var map;
          function initialize() {
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(58.813742, -98.789062);
            var myOptions = {
              zoom: 4,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.HYBRID
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
          }

          //Change Address to Latlng and show on Map
          function codeAddress() {
            var address = "10 my house street, city, state";
            geocoder.geocode( { 'address': address}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                map.setZoom(14);
                var marker = new google.maps.Marker({
                    map: map, 
                    position: results[0].geometry.location,
                });
              } else {
                alert("Geocode was not successful for the following reason: " + status);
              }
            });
          }
        </script> 
share|improve this question
add comment

2 Answers

Use a foreach loop:

<?php

foreach ( $array_of_addresses as $address ) {

// Do Whatever

}

?>

Are you using the PHP library from Monte Ohrt? If not you should it's really good, you can download it here http://www.phpinsider.com/php/code/GoogleMapAPI/

share|improve this answer
add comment

Define the JavaScript function once. Then, loop thru the PHP array and run the javascript function on each address. Here is an example:

function geocode() {
...
}

<?php
  foreach($arrAddress as $address) {
?>
    geocode('<?php echo $address; ?>');
<?php
  }
?>
share|improve this answer
 
Allright. But, now I m facing another issue. I am now getting that the geocoder is undefined, even though I have defined geocode. Check original post for the javascript function. –  Blueboye Jun 30 '11 at 15:47
 
Ok. I got it. I am calling the geocoder function inside the body and I have set initalize() on body load. So, the geocoder function is being called before the initialize. So, now I need to find a way to call the geocoder function after the body initializes. Let's see what we I can do. Any thought guys? –  Blueboye Jun 30 '11 at 16:00
 
Just drop the PHP code into the body after the codeAddress function. –  Detect Jun 30 '11 at 16:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.