gpt4 book ai didi

javascript - Heatmaps.js 未捕获的 ReferenceError

转载 作者:可可西里 更新时间:2023-11-01 01:15:47 24 4
gpt4 key购买 nike

我正在使用 heatmaps.js 库和 Google Maps API 在顶部显示 map 和热图。我已经能够显示 map 以及查询数据库以获得我需要的数据。我遇到的问题是让热图显示。我收到以下两个错误:

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>

帮助将不胜感激。谢谢!

最佳答案

一旦加载 Google map 脚本,您就会调用 initMap() 回调,这可能是在加载 Heatmap.js 库之前。同步加载 Google map 并添加一个 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>

关于javascript - Heatmaps.js 未捕获的 ReferenceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39181254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com