【问题标题】:unable to create multiple marker using the data from my query无法使用我的查询中的数据创建多个标记
【发布时间】:2016-07-12 03:46:51
【问题描述】:

为什么当我尝试查询数据时,我的谷歌地图上只有一个标记?

最好的方法应该是什么?请帮助新手编码。

<?php
$query= mysqli_query($con,"SELECT imdb.STATION,imdb.PEIS,imdb.PGA,imdb.PGV, station.LAT, station.LON  FROM imdb, station WHERE imdb.MAC = station.MAC and  YYYYMMDD between '$from' and '$to'and HHMMSS between '$from1' and '$to1';");

while($row = mysqli_fetch_array($query)){

                                $station = $row['STATION'];
                                $pei = $row['PEIS'];
                                $pga= $row['PGA'];
                                $pgv= $row['PGV'];
                                $lat = $row['LAT'];
                                $lon = $row['LON'];}
?>

<script type="text/javascript">

      function initMap() {
    var myLatLng = {lat: <?php echo $lat; ?>, lng: <?php echo $lon; ?>};
    var map = new google.maps.Map(document.getElementById('map'), 
        {zoom: 4,center: myLatLng});

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

【问题讨论】:

  • 您正在覆盖您在while 循环中定义的变量。在启动循环之前定义它们并改用数组。我所做的是,我将它们编码为 JSON,然后使用 JSON.parse 在 JavaScript 中对其进行解码,迭代循环并放置标记。
  • 您好先生,您能给我一些例子吗,我是编程新手,非常感谢它
  • 请检查我发布的答案,如果它解决了您的问题,请将其标记为回答。

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


【解决方案1】:

正如 OP 所要求的,我提供了一个工作示例,可以适当地插入代码以使其工作:

<?php
$result = array();
while($row = mysqli_fetch_array($query)){
    $result[]['lat'] = $row['LAT'];
    $result[]['lon'] = $row['LON'];
}
?>

使用以下脚本初始化和创建地图:

<script type="text/javascript">

    var locationsJSON = '<?php echo json_encode($result) ?>';
    var locations = JSON.parse(locationsJSON);

    var bounds = new google.maps.LatLngBounds();
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 2,
        center: new google.maps.LatLng(0, 0),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var marker, i;
    for (i = 0; i < locations.length; i++) {
        console.log(locations[i]);
        var myLatLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
        bounds.extend(myLatLng);
        map.fitBounds(bounds);
        marker = new google.maps.Marker({
            position: myLatLng,
            map: map
        });
    }
</script>

【讨论】:

  • 我可以看到查询中的一些数据,但地图没有加载您可以看到完整的代码并检查需要更正它的内容
  • 将脚本放入initMap,因为它应该被调用。
  • 先生,我已经把脚本放在了 initMap 函数中,然后我把这个 但地图仍然没有加载。谢谢你
  • “不显示”我无能为力。你检查浏览器控制台了吗?有没有错误?地图以前可以用吗?
  • 是的,更改之前的地图工作正常,甚至可以在地图上获得 1 个标记,但是当我更改地图时,地图无法正常工作,没有错误,只是地图无法正常工作
【解决方案2】:
Hi can you check on this code is this correct and i place the google map script at the top before the body part of the page

<script type="text/javascript">

      function initMap() {
var locationsJSON = '<?php echo json_encode($result) ?>';
var locations = JSON.parse(locationsJSON);

var bounds = new google.maps.LatLngBounds();
var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 2,
    center: new google.maps.LatLng(0, 0),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker, i;
for (i = 0; i < locations.length; i++) {
    console.log(locations[i]);
    var myLatLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
    bounds.extend(myLatLng);
    map.fitBounds(bounds);
    marker = new google.maps.Marker({
        position: myLatLng,
        map: map
    });
  }
  }
</script>

【讨论】:

    【解决方案3】:

    你能检查我为什么不断收到这个错误

    警告:array_map():参数 #2 应该是一个数组

    $the_array  = array();               
                  while($row = mysqli_fetch_array($query)){
    
                  $station = $row['STATION'];
                  $pei = $row['PEIS'];
                  $pga= $row['PGA'];
                  $pgv= $row['PGV'];
                  //$array_lat_lon[] = $lat = $row['LAT'];
                  //$array_lat_lon[] = $lon = $row['LON'];
                  $the_array[] = array($row['LAT'] . "," . $row['LON']) ;      
                  //$timestamp = strtotime()
    
                  echo '<tr><td>'.$station.'</td><td>'.$pei.'</td><td>'.$pga.'</td><td>'.$pgv.'</td></tr>';
                              }
                  //echo $lat.'<br>';
                  //echo $lon.'<br>';
                  //echo json_encode($the_array);
    
                  $location_array = json_decode('$the_array');
                  $location_array = array_map('json_decode', $location_array[$the_array]);
                  echo $location_array;
                              }
                              }
                      ?>
    

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 2021-01-22
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2019-05-17
      相关资源
      最近更新 更多