我想从我的 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/