开发者

Mapping multiple address on googlemaps - javascript API and PHP

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> 


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/


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
  }
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜