未捕获的引用错误
Heatmaps.js Uncaught ReferenceError
我使用heatmaps.js库和Google Maps API在顶部显示地图和热图。我已经能够显示地图以及查询数据库,以获得我需要的数据。我遇到的问题是让热图显示。我得到以下两个错误:
gmaps-heatmap.js:27 Uncaught ReferenceError: google is not defined
HeatMapsTest.php:41 Uncaught ReferenceError: HeatmapOverlay is not defined
第一个错误发生在库本身,第二个错误发生在下面的代码中:
<html>
<head>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map-canvas {
height: 100%;
}
</style>
</head>
<body>
<div id="map-canvas"></div>
<?php
//parameters for connecting to database
$servername = '';
$username = '';
$password = '';
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br> ";
$sql = "SELECT LATITUDE,LONGITUDE FROM `crashes`";
$test = $conn->query($sql);
if ($test == true) {
echo "Query Worked!";
} else {
echo "Query Did Not Work";
}
try{
$db = $conn;
$result = $db->query($sql);
while ($row = $result->fetch_array(MYSQLI_ASSOC) ) {
$data[] = $row;
}
$db = NULL;
$data_json = json_encode($data);
/* echo "<br>";
echo $data_json; */
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
?>
<script>
function initMap() {
var myLatlng = new google.maps.LatLng(-37.8, 144.9);
var myOptions = {
zoom: 8,
center: myLatlng
};
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
heatmap = new HeatmapOverlay(map,
{
// radius should be small ONLY if scaleRadius is true (or small radius is intended)
"radius": 17,
"maxOpacity": 1,
// scales the radius based on map zoom
"scaleRadius": false,
// if set to false the heatmap uses the global maximum for colorization
// if activated: uses the data maximum within the current map boundaries.
// (there will always be a red spot with useLocalExtremas true)
"useLocalExtrema": true,
// which field name in your data represents the lat - default "lat"
latField: 'LATITUDE',
// which field name in your data represents the lng - default "lng"
lngField: 'LONGITUDE',
// which field name in your data represents the data value - default "value"
valueField: 'value'
}
);
var testData = {
max: 8,
data : <?php echo $data_json; ?>
};
heatmap.setData(testData);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&callback=initMap">
</script>
<script src="heatmap.js-develop/build/heatmap.js"></script>
<script src="heatmap.js-develop/plugins/gmaps-heatmap.js"></script>
</body>
</html>
帮助将是非常感激的。谢谢!
你调用initMap()
回调,只要谷歌地图脚本加载,这可能是在Heatmap.js库加载之前。同步加载Google地图,并添加一个DOM加载的事件监听器,以便在所有脚本加载后调用initMap()
函数。
所以替换这个:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&callback=initMap"></script>
<script src="heatmap.js-develop/build/heatmap.js"></script>
<script src="heatmap.js-develop/plugins/gmaps-heatmap.js"></script>
与这个:<script src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY"></script>
<script src="heatmap.js-develop/build/heatmap.js"></script>
<script src="heatmap.js-develop/plugins/gmaps-heatmap.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
initMap();
});
</script>
相关文章:
- gmaps4rails 2.4.6未捕获引用错误:未定义Gmaps
- Wordpress中的Javascript出现匿名函数/引用错误
- 使用dc.js、d3.js和crossfilter引用错误
- 未捕获引用错误:未定义validateform
- 为什么我会出现此错误"未捕获引用错误:未定义标题;
- $未定义-未捕获引用错误
- Sitecore”;未捕获引用错误:未定义MultilistWithSearch;
- Wordpress:未捕获引用错误:未定义速度
- Node.js引用错误:未定义类名
- 我该如何解决“;未捕获引用错误:谷歌没有定义"?(谷歌地图API)
- 调用setTimeout内部的函数时发生引用错误
- 未捕获的引用错误:未定义<映射键>
- Javascript错误:未捕获引用错误:未定义编辑
- Google可视化addListener调用函数引用错误
- Cordova:未捕获引用错误:chbrody没有使用SQLitePlugin.js定义PhoneGap
- 未捕获引用错误:未定义PrintThisPage
- 引用错误:在尝试使用ajax发送数据时未定义
- 包含的文件中引发引用错误..除非调用文件包含慢速代码
- 在表单提交按钮的点击事件中调用函数时发生引用错误
- 未捕获引用错误:幻灯片未定义