使用Google Map API与MySQL和PHP显示多个位置最简单的方法是什么? [英] What's the easiest way to display multiple locations using Google Map API with MySQL and PHP?

查看:101
本文介绍了使用Google Map API与MySQL和PHP显示多个位置最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此类型:

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中:



<$ p (){


函数初始化(){
var myLatlng = new google .maps.LatLng(您的中心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);


$ b $ 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
});


//在这里添加弹出窗口:

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


}

initialize();


}); < /脚本>

您的html应该是这样的:

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

我可能会缺少一些括号,但您明白了这一点;)
希望它甚至可以帮助您8个月后:)

I would like to display over 100 locations each with detailed bubble contents (ie Company Name, Address, Phone Number, Website, Industry) on a custom Google Map. How can I fetch these locations from a MySQL database using Google Maps API 3 and PHP?

解决方案

say you have an sql database of this type:
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>";

and then in your 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>

your html should be something like:

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

i might be missing some parentheses but you get the point ;) hope it helps even 8 months later :)

这篇关于使用Google Map API与MySQL和PHP显示多个位置最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆