从数据库中获取多个纬度和经度,并在谷歌地图上显示它们的标记

fetch multiple latitude and longitude from database and display their marker on google map

本文关键字:显示 谷歌地图 获取 数据库 经度 纬度      更新时间:2023-09-26

我有一个代码,应该从数据库中获取数据(纬度和经度(并将其与标记一起显示在谷歌地图上,问题是它只获取和显示一行的纬度和经度,但它应该显示所有的地方。如果有人能帮助我,将不胜感激。

<script>
    <?php 
    require 'connection.php'; 
    $parent_id = $_GET['country'];
    $fid = $_GET['fid']; 
        $sql = "SELECT * FROM features_for_office WHERE parent_id='".$parent_id."' and fid='".$fid."' ";
        $result = mysqli_query($con, $sql);
        if (mysqli_num_rows($result) > 0) 
            {
              while($row = mysqli_fetch_assoc($result)) 
               {
                 $officeid= $row["officeid"];
                 $sql1 = "SELECT * FROM register_office WHERE id='".$officeid."' ";
                 $result1 = mysqli_query($con, $sql1);
                  if (mysqli_num_rows($result1) > 0) 
                  {
                   while($row1 = mysqli_fetch_assoc($result1)) 
                   {
                    $officelat= $row1["lat"];
                    $officelon= $row1["lon"];
                    $officetitle= $row1["officetitle"];
                     //echo $officelat; 
                     //echo $officelon;
                     //echo $officetitle;
                  ?>    
                   var myCenter=new google.maps.LatLng(<?php echo $officelat;?>,<?php echo $officelon;?>);
                   function initialize()
                   {
                    var mapProp = {
                    center:myCenter,
                    zoom:5,
                    mapTypeId:google.maps.MapTypeId.ROADMAP
                    };
                    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
                    var marker=new google.maps.Marker({
                    position:myCenter,
                    });
                    marker.setMap(map);
                    var infowindow = new google.maps.InfoWindow({
                    content:"<?php echo $officetitle;?>"
                    });
                   infowindow.open(map,marker);}
                   google.maps.event.addDomListener(window, 'load', initialize);
                 <?}
                 }
              }
          } 
    mysqli_close($con);     
 ?>
   </script>
   <div id="googleMap" style="width:1145px;height:380px;"></div>

错误是您每次都在初始化地图,直到循环完成,它每次都会重新创建地图,并且最后一个 latlon 标记将显示在地图上。在页面加载时调用 initialize 方法,并将以下代码放在循环中。

var myCenter=new google.maps.LatLng(<?php echo $officelat;?>,<?php echo $officelon;?>);
                  var marker=new google.maps.Marker({
                    position:myCenter,
                    });
                    marker.setMap(map);

使用 SQL,您可以使用以下代码从数据库中获取纬度和经度:

protected void googleMap_Load(object sender, EventArgs e) 
    {
        //Establishing SQL connection
        SqlConnection connStr = new
        SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
        connStr.Open();//Open the connection to Database
        //Generate the database query to fetch the content details
        SqlCommand cmd = new SqlCommand("select LAT,LON from DATA where ID=9", connStr);
        SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
    }

问题仍然存在,因为如何使用此数据表在谷歌地图上设置标记......可以将问题重新构建为从数据库中获取 LatLng我使用脚本显示地图

<script>
    var myLatLon = new google.maps.LatLng(13, 77);//Defining Latitude and Longitude 
    var mapProp = {
        center: myLatLon, //    Center of display page.
        zoom: 8, //Zoom level
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById('dvMap'), mapProp);
    var marker = new google.maps.Marker({
        position: myLatLon,
        map: map,
    });
</script>