javascript - Google API 热图层异常,为什么?

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

我想从我的 PHP 文件中为我的热图层传递热图数据数组。运行我的代码(如下)时出现以下异常:

Uncaught TypeError: b.lat is not a function

有人知道为什么吗?

.HTML

<!DOCTYPE html>
<html>
<body>
    <form>
    <select name="users" onchange="showString(this.value)">
    <option value="">WholeWorldAllTime:</option>
    <option value="1">Whole World 1 Hour</option>
    </select>
    </form>
    <div id="map"></div>

<script>
     function showString(time) {
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      var heatmapdata = [xmlhttp.responseText];

      var heatmap = new google.maps.visualization.HeatmapLayer({
            data: heatmapdata,
            map: map
            });
    }
  }
  xmlhttp.open("GET","getstring.php?q="+time,true);
  xmlhttp.send();

}
</script>

<script>
var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 3,
    center: {lat: 37.775, lng: -122.434},
    mapTypeId: google.maps.MapTypeId.SATELLITE
  });
}
</script>

<script async defer
        src="https://maps.googleapis.com/maps/api/js?signed_in=true&libraries=visualization&callback=initMap">
    </script>
  </body>
</html>

.PHP

<?php
$q = intval($_GET['q']);

$con = pg_connect("host=localhost port=5432 dbname=Twitter user=postgres password=****");
if (!$con) {
    die('Could not connect: ' . pg_errormessage($con));
}

$sql="SELECT * FROM tswholeworld WHERE createdat > (NOW() - INTERVAL '".$q."hour');";
$result = pg_query($con,$sql);

    $string = "";
while($row = pg_fetch_assoc($result)){
    $lat = $row['latitude'];
    $lon = $row['longitude'];
    $string .= "new google.maps.LatLng($lat, $lon), ";
}
echo $string;
pg_close($con);
?>

最佳答案

heatmapdata 不是 google.maps.LatLng 对象的数组。

var heatmapdata = [xmlhttp.responseText];

关于javascript - Google API 热图层异常,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440719/

相关文章:

php - 在 Laravel 4 中显示图像

php - Laravel 5 - Mercurial 推荐 .hgignore

java - mapFragment.getMapAsync 中的 NullPointerException

android - 在 Android Google Maps API v2 中,交通数据显示在绘制的形状(多段线)上方

javascript - 记录数组中的值不起作用

javascript - 使用 jQuery 创建一个填充了 JSON 的键和值的 UL

php - 选择多次存在且存在变化的行

javascript - 实时数据更新(车辆跟踪)

javascript - 向上翻页、向下翻页、向上和向下键在我的页面上不起作用

javascript - jQuery 如何确保在继续脚本之前打开对话框?