使用带有MySQL和PHP的Google Map API显示多个位置的最简单方法是什么?

时间:2011-08-01 19:05:00

标签: php mysql google-maps google-maps-api-3 google-maps-markers

我想在自定义Google地图上显示100多个位置,每个位置都有详细的气泡内容(即公司名称,地址,电话号码,网站,行业)。如何使用Google Maps API 3和PHP从MySQL数据库中获取这些位置?

1 个答案:

答案 0 :(得分:2)

假设你有一个这种类型的SQL数据库:
id / lat / lon / comment

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key= **your key** &sensor=true">
</script>


    $sqlprep = "SELECT * FROM table_name";

    $doselect = mysql_query($sqlprep);

    echo "<script> var locations = [";

        while ($loc = mysql_fetch_assoc($doselect)){

        echo "['" .$loc[comment] ."', " . $loc['lat'] . ", " . $loc['lon'] . "";

        }

    echo " ];</script>";

然后在你的java:

<script>jQuery(window).ready(function(){


          function initialize() {
                var myLatlng = new google.maps.LatLng( YOUR CENTER LAT,LAN HERE );
               var myOptions = {
                 center: myLatlng,
                 zoom: 15,
                 mapTypeId: google.maps.MapTypeId.ROADMAP
               };


  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);



             var infowindow = new google.maps.InfoWindow()


    var marker, i;

    for (i = 0; i < locations.length; i++) {  
            marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),


              map: map
            });


//adding the pop up windows here:

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                      infowindow.setContent(locations[i][0]);
                      infowindow.open(map, marker);
                    }
            })(marker, i));


      }

  initialize() ; 


   });  </script>

你的HTML应该是这样的:

<html>
<body >
<div id="map_canvas" >
</div>
</body>
</html>     

我可能会遗漏一些括号,但你明白了这一点;) 希望它在8个月后有所帮助:))

相关问题