doucai1901 2015-08-18 11:35
浏览 61

标记不显示在谷歌地图上,但在控制台中没有错误

Have been pulling my hair out about this for a while: trying to use json to get lat/lng values from my database and then use these values to output markers onto a google map. Currently the map is displaying without the markers and there are no errors in the console. Any help would be greatly appreciated!

 <script type="text/javascript">
       var map = null;

       function addMarker(lat,lng) {
             marker = new google.maps.Marker({
             position: new google.maps.LatLng(lat,lng),
             map: map,
           });
     }


      function initialize() {
      var mapOptions = {
      center: {lat: 54.872128, lng: - 6.284874},
      zoom: 13
     };
    map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

   $(document).ready(function(){

   $.getJSON('MarkersController.php', function(data){


         var locations = JSON.parse(data);

         for(var i =0; i<locations.length; i++){
               addMarker(locations['lat'], locations['lng']);
      }
});
});


}

 google.maps.event.addDomListener(window, 'load', initialize);

The php script for retrieving from mysql:

<?php

include('DB_INFO.php');

// Opens a connection to a MySQL server.

$connection = mysqli_connect($server, $username, $password);

if (!$connection) {
    die('Not connected : ' . mysqli_error());
}
// Sets the active MySQL database.
$db_selected = mysqli_select_db($database, $connection);

if (!$db_selected) {
    die('Can\'t use db : ' . mysqli_error());
}

// Selects all the rows in the markers table.
$query = 'SELECT * FROM tester';
$result = mysqli_query($query);

if (!$result) {
    die('Invalid query: '. mysqli_error());
}

$list = array();
while ($row = mysqli_fetch_assoc($result)) {
    $list[] = $row;
}
header('Content-Type: application/json');
echo json_encode($list);
?>
  • 写回答

4条回答 默认 最新

  • douding_1073 2015-08-18 11:51
    关注

    If you have a collection of marker i think you should use this way iteranting on the collection and assignd the i element :

    for(var i =0; i<locations.length; i++){
      addMarker(locations[i].lat, locations[i].lng);
    }
    

    proof of concept fiddle

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大