简体   繁体   English

从数据库中获取多个经度并在Google地图上显示其标记

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

i have a code that is supposed to fetch data (latitude and longitude) from database and display it on google map along with markers, the problem is that it fetches and displays latitude and longitude of only one row, but it is supposed to show all the places. 我有一个应该从数据库中获取数据(经度和纬度)并将其与标记一起显示在Google地图上的代码,问题是它仅获取并显示了一行的纬度和经度,但是应该显示所有的地方。 Would appreciate if anyone could help me. 如果有人可以帮助我,将不胜感激。

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

The error is that you are initializing the map every time until the loop completes,it will recreate the map every time and the last latlon marker will be shown on the map.Call the initialize method on page load and place the following code inside the loop. 错误是您每次都在初始化地图直到循环完成,它将每次都重新创建地图,最后一个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);

using SQL you can fetch Latitude and Longitude from Database using this code: 使用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);

    }

The Problem still remains as how to use this dataTable to set marker on google map... The problem can be re-framed as to fetch the LatLng from database I used the script to display map 问题仍然存在,如何使用此dataTable在谷歌地图上设置标记...问题可以重新构造为从数据库中获取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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 从数据库中获取经度和纬度并在谷歌地图上显示标记 - Laravel fetch longitude and latitude from database and display markers on Google Map 从谷歌获取经纬度 map - Fetch latitude and longitude from google map 如何在Google地图上显示多个经度和纬度 - How to display multiple Latitude and Longitude on Google Map 使用数据库中的多个纬度和经度值在Google地图上标记路线 - Marking route on Google map with multiple latitude and longitude values from database 使用 SQL 数据库和 Leaflet Map 在 Z9E0DA8438E1E38B81CDD3 - Display Marker Using Latitude And Longitude From SQL Database With Leaflet Map In ASP.NET CORE MVC 使用标记从Google地图获取纬度/经度坐标 - Get latitude/longitude coordinates from a Google map using a marker 如何在一个 map 中显示 3 个纬度和经度(标记)? - how to display 3 latitude & longitude (Marker) in one map? 在按钮上单击显示数据库中单个用户的纬度和经度,并在谷歌地图上显示 - On button click display Latitude and longitude of single user from database and display on google map 如何在Google地图上将标记的像素位置映射到经度和纬度 - How to map the pixel location of a marker on Google Map to Latitude and Longitude Spring + Thymeleaf - 显示数据库中的多个标记(经度和纬度) - Spring + Thymeleaf - Display multiple markers from database (Longitude and latitude)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM