doudun1934 2015-09-08 05:14
浏览 24
已采纳

谷歌在PHP中映射多个标记不起作用

to put map in my site i follow one example and it will display static markers like.

var locations = [
['Bondi Beach', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],
['Manly Beach', -33.80010128657071, 151.28747820854187],
['Maroubra Beach', -33.950198, 151.259302]
];

i want it dynamic from mysql table. it has lat and lng. i tried this but not working

<?php
    $host = "localhost";
    $db_name = "aarya";
    $user = "root";
    $password = "";

    $con = mysql_connect($host,$user,$password) or die("connection error");

    mysql_select_db($db_name) or die("could't connect to database");

    $query="SELECT * FROM detail";
    $result=mysql_query($query);
    while ($row=mysql_fetch_array($result)) {
    ?>
var locations = [
  ['<?php echo $row['name'];?>','<?php echo $row['lat'];?>','<?php echo $row['lng'];?>'],
<?php
}
?>
];

i think problem in locations array and i am new in google maps please help me.

  • 写回答

5条回答 默认 最新

  • dongyi1111 2015-09-08 05:46
    关注

    Here is a rough example of doing this:

    Your locations array:

    <?php
    $locations[0] = array("lat"=>"-33.890542", "long"=>"151.274856", "info" => "Bondi Beach");
    $locations[1] = array("lat"=>"-33.923036", "long"=>"151.259052", "info" => "Coogee Beach");
    $locations[2] = array("lat"=>"-34.028249", "long"=>"151.157507", "info" => "Cronulla Beach");
    $locations[3] = array("lat"=>"-33.80010128657071", "long"=>"151.28747820854187", "info" => "Manly Beach");
    $locations[4] = array("lat"=>"-33.950198", "long"=>"151.259302", "info" => "Maroubra Beach");
    ?>
    

    Or you could query the database like:

    <?php
    $query="SELECT * FROM detail";
    $result=mysql_query($query);
    while ($row=mysql_fetch_array($result)) {
        $locations[] = array("lat"=>$row['lat'], "long"=>$row['lng'], "info" => $row['name']);
    }
    ?>
    

    You can modify the above array with the values from your database. The map:

    <html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
            <style type="text/css">
                body { font: normal 10pt Helvetica, Arial; }
                #map { width: 350px; height: 300px; border: 0px; padding: 0px; }
            </style>
            <script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
            <script type="text/javascript">
    
            var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
            new google.maps.Size(32, 32), new google.maps.Point(0, 0),
            new google.maps.Point(16, 32));
            var center = null;
            var map = null;
            var currentPopup;
            var bounds = new google.maps.LatLngBounds();
    
            function addMarker(lat, lng, info)
            {
                var pt = new google.maps.LatLng(lat, lng);
                bounds.extend(pt);
                var marker = new google.maps.Marker(
                {
                    position: pt,
                    icon: icon,
                    map: map
                });
                var popup = new google.maps.InfoWindow(
                {
                    content: info,
                    maxWidth: 300
                });
    
                google.maps.event.addListener(marker, "click", function()
                {
                    if (currentPopup != null)
                    {
                        currentPopup.close();
                        currentPopup = null;
                    }
                    popup.open(map, marker);
                    currentPopup = popup;
                });
                google.maps.event.addListener(popup, "closeclick", function()
                {
                    map.panTo(center);
                    currentPopup = null;
                });
            }
    
            function initMap()
            {
                map = new google.maps.Map(document.getElementById("map"), {
                center: new google.maps.LatLng(0, 0),
                zoom: 0,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: false,
                mapTypeControlOptions:
                {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
                },
                navigationControl: true,
                navigationControlOptions:
                {
                    style: google.maps.NavigationControlStyle.SMALL
                }
                });
                <?php foreach($locations AS $loc) { //you could replace this with your while loop query ?>
                    addMarker(<?php echo $loc["lat"]; ?>, <?php echo $loc["long"]; ?>, '<?php echo $loc["info"]; ?>');
                <?php } ?>
                center = bounds.getCenter();
                map.fitBounds(bounds);
            }
            </script>
        </head>
        <body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
        <div id="map"></div>
    </html>
    

    Hope that helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程