如何通过while循环插入谷歌地图? [英] How to insert google maps through a while loop?

查看:126
本文介绍了如何通过while循环插入谷歌地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在通过 while循环生成的 div (html)中插入 google地图。 Google地图从数据库获取坐标。




I want to insert a google map in a div(html) which generates through a while loop. Google map gets coordinates from the database.

It should appear as in this image

Since I need to use a google map api key, I used following code.

But, It shows a map, only in the first div, using the last coordinates of the database, and doesn't show any other map in any other divs.

<?php 
    $connection = mysqli_connect('localhost', 'root', '', 'users');


    function currentUsers($connection){
        $sql = "SELECT * FROM user ";
        $result = mysqli_query($connection, $sql);

        if(mysqli_num_rows($result) > 0){
            while($row = mysqli_fetch_assoc($result)) {
                $userID = $row['id'];
                $firstName = $row['firstname'];
                $country = $row['country'];
                $latitude = $row['latitude'];
                $longitude = $row['longitude'];

                echo '<div> 
                            <div align = "left">
                                <h3>'. $userID. " ". $firstName. " ". $country. '</h3>
                            </div>

                            <div id  = "map" align = "right">
                                <script>    <!--Google map api-->
                                  function initMap() {
                                    var x = '. $latitude. ';
                                    var y = '. $longitude. ';
                                    var myLatLng = {lat: x, lng: y};

                                    var map = new google.maps.Map(document.getElementById(\'map\'), {
                                      center: myLatLng,
                                      scrollwheel: true,
                                      zoom: 4
                                    });

                                    var marker = new google.maps.Marker({
                                      map: map,
                                      position: myLatLng,
                                      title: \'Hello World!\'
                                    });
                                  }
                                </script>
                                <script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>
                            </div>  
                       </div>';
            }

        }else{
            echo "Currently there are no users!";
        }

        mysqli_close($connection);
    }

    currentUsers($connection);


?>

<html>
<head><title></title>
      <style> #map {width: 500px; height: 400px; } </style>  <!--Size of the map-->
</head>
<body></body>
</html>

解决方案

This is 100% working for me i've tested this code

<?php
$arr='';
$connection = mysqli_connect('localhost', 'root', '', 'users');


    function currentUsers($connection){
        $sql = "SELECT * FROM user";
        $result = mysqli_query($connection, $sql);
        $x= 0;
        if(mysqli_num_rows($result) > 0){
            while($row = mysqli_fetch_assoc($result)) {
                $userID = $row['id'];
                $firstName = $row['firstname'];
                $country = $row['country'];
                $arr[] = array($row['latitude'],$row['longitude'],$x);
                echo '<div style="width:100%;"> 
                            <div style="width:250px; height: 150px; float :left;">
                                <h3>'. $userID. ". ". $firstName. " ". $country. '</h3>
                            </div>
                            <div id  = "map_'.$x.'"  style="width:250px;height: 150px;">   </div>
                      </div>';
                       $x++;
            }
        echo '<script> var mymaps ='.json_encode($arr).'</script>';

        }else{
            echo "Currently there are no users!";
        }

        mysqli_close($connection);
    }
    currentUsers($connection);
?>

<script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>

<script>
var maps = []; 

function initialize(id, myLatLng) { 
    geocoder = new google.maps.Geocoder();
    var mapOptions = {
        zoom: 9,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    maps[id] = new google.maps.Map(document.getElementById('map_'+id), 
        mapOptions);

    var marker = new google.maps.Marker({
       map: maps[id],
       position: myLatLng,
       title: 'Hello World!'
    });
}

$(document).ready(function(){
    for( var i = 0; i < mymaps.length; i++)
    {
        var x = parseFloat(mymaps[i][0]);
        var y = parseFloat(mymaps[i][1]);
        var myLatLng = {lat: x, lng: y};
        google.maps.event.addDomListener(window, 'load', initialize(mymaps[i][2],myLatLng)); // two calls
    }

});
</script>

这篇关于如何通过while循环插入谷歌地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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