如何使用数据库中的坐标动态地在一个页面上添加多个地图

时间:2018-01-03 12:22:13

标签: javascript php google-maps google-maps-api-3

我有谷歌地图Api我想使用并在一个页面上显示多个地图,每个地图都有自己的| key | ts | trunc | |-----|----------------------|------------| | 1 | 2017-01-01T08:30:01Z | 1483259400 | | 2 | 2017-01-01T08:30:05Z | 1483259400 | | 3 | 2017-01-01T08:30:09Z | 1483259400 | | 4 | 2017-01-01T08:30:10Z | 1483259410 | | 5 | 2017-01-01T08:30:15Z | 1483259410 | | 6 | 2017-01-01T08:30:20Z | 1483259420 | ,坐标来自数据库使用coordinate and marker但在我的页面上我得到只有一个地图有一个制造商,我需要的地图等于我获取的坐标(纬度和经度)的数量。 请尽可能帮助我。

PHP HTML代码:

PHP

Javascript代码:

<?php while($lakeRow=mysql_fetch_array($res)) { ?>

    <div class="Coordinates">
        <input type="hidden" class="a" id="latitude" value="<?php echo $lakeRow['latitude'];?>">
        <input type="hidden" class="b" id="longitude" value="<?php echo $lakeRow['longitude'];?>">
    </div>
    <div class="col-md-4">
        <div class="thumbnail">
            <div class="more">
                <h4><?php echo $lakeRow['photo_caption_one'];?></h4>
            </div>
            <a href="" target="_blank">
                <?php echo '<img src="data:photo/jpeg;base64,'.base64_encode($lakeRow['photo'] ).'" height="300"/><br/>'  ; ?>
                <div class="caption">
                    <p><?php echo $lakeRow['photo_caption_one'] ; ?> .</p>
                </div>
                <div class="more">
                    <div id ="map" style="height: 253px" >
                    </div>
                </div>
            </a>
        </div>
    </div>    

<?php }?>

1 个答案:

答案 0 :(得分:0)

它没有经过测试,但您可以尝试这些方法

<?php
    /*
        before the html is begun select the recordset
        and store lat/lng to an array


    */

    $lakes=array();
    while( $lakeRow=mysql_fetch_array( $res ) ) {
        $lakes[]=array( 
            'lat' => $lakeRow['latitude'],
            'lng' => $lakeRow['longitude'],
            'caption' => $lakeRow['photo_caption_one'],
            'photo' => $lakeRow['photo']
        );
    }

    $json=json_encode( $lakes );
?>
<!doctype html>
<html>
    <head>
        <title>multiple maps</title>
        <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAGMLRwrop1EGkgdzQ9a7zzREq4uw8l63k&callback=initMap"></script>
        <script>

            var lakes=<?php echo $json;?>;

            function initMap() {

                for( var n in lakes ){
                    var obj=lakes[ n ];

                    var lat=obj.lat;
                    var lng=obj.lng;
                    var container=document.getElementById('map_'+n);
                    var latlng=new google.maps.LatLng(lat,lng);

                    var map=new google.maps.Map( container,{
                        zoom:10,
                        center:latlng
                    });

                    var marker = new google.maps.Marker({
                        position:latlng,
                        map: map
                    });
                }
            }
        </script>
    </head>
    <body>
        <?php
            foreach( $lakes as $index => $lake ){

                echo "
                  <div class='col-md-4'>
                    <div class='thumbnail'>
                        <div class='more'>
                            <h4>{$lake['caption']}</h4>
                        </div>
                        <a href='#' target='_blank'>
                            <img src='data:image/jpeg;base64," . base64_encode( $lake['photo'] ) . "' height='300'/>
                            <br/>
                            <div class='caption'>
                                <p>{$lake['caption']}</p>
                            </div>
                            <div class='more'>
                                <div id='map_{$index}' style='height:253px'></div>
                            </div>
                        </a>
                    </div>
                </div>";
            }
        ?>
    </body>
</html>

我做的测试页面用于说明上述内容:

<?php
    /* a db connection */
    $dbhost =   'localhost';
    $dbuser =   'root'; 
    $dbpwd  =   'xxx'; 
    $dbname =   'xxx';
    $db =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );

    /* a query to select lat,lng and other fields */
    $sql='select 
            venue_name as `photo_caption_one`, 
            null as `photo`, venue_map_centre_Latitude as `latitude`, 
            venue_map_centre_Longitude as `longitude` 
            from fishing_venue
            where venue_id > 1
            limit 10';
    $result=$db->query( $sql );

    /* store results in $lakes */
    $lakes=array();

    /* process recordset - store items */
    while( $lakeRow=$result->fetch_array() ) {
        $lakes[]=array( 
            'lat'       => $lakeRow['latitude'],
            'lng'       => $lakeRow['longitude'],
            'caption'   => $lakeRow['photo_caption_one'],
            'photo'     => $lakeRow['photo']
        );
    }

    /* encode data for use by javascript */
    $json=json_encode( $lakes );
?>
<!doctype html>
<html>
    <head>
        <title>multiple maps</title>
        <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAGMLRwrop1EGkgdzQ9a7zzREq4uw8l63k&callback=initMap"></script>
        <script>

            var lakes=<?php echo $json;?>;

            function initMap() {

                for( var n in lakes ){
                    var obj=lakes[ n ];

                    var lat=obj.lat;
                    var lng=obj.lng;
                    var container=document.getElementById('map_'+n);
                    var latlng=new google.maps.LatLng(lat,lng);

                    var map=new google.maps.Map( container,{
                        zoom:10,
                        center:latlng
                    });

                    var marker = new google.maps.Marker({
                        position:latlng,
                        title:obj.caption,
                        map: map
                    });
                }
            }
        </script>
    </head>
    <body>
        <?php
            foreach( $lakes as $index => $lake ){

                echo "
                  <div class='col-md-4'>
                    <div class='thumbnail'>
                        <div class='more'>
                            <h4>{$lake['caption']}</h4>
                        </div>
                        <a href='#' target='_blank'>
                            <img src='data:image/jpeg;base64," . base64_encode( $lake['photo'] ) . "' height='300'/>
                            <br/>
                            <div class='caption'>
                                <p>{$lake['caption']}</p>
                            </div>
                            <div class='more'>
                                <div id='map_{$index}' style='width:400px;height:300px'></div>
                            </div>
                        </a>
                    </div>
                </div>";
            }
        ?>
    </body>
</html>

enter image description here