我收到以下错误:
未- 捕获的类型错误:无法读取未定义的属性"映射":第 18 行
- 未捕获的类型错误:google.visualization.LineChart 不是构造函数:第 43 行
第一个错误,我不断收到,但所有应该加载的东西都会加载。我不知道这是否是一个问题。
但是,第二个错误有点奇怪。它非常不一致,导致图形不显示。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Elevation Profiles</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(initMap());
function initMap() {
var path = [
{lat: 36.579, lng: -118.292},
{lat: 36.24, lng: -116.832}];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: path[1],
mapTypeId: 'terrain'
});
var elevator = new google.maps.ElevationService;
displayPathElevation(path, elevator, map);
}
function displayPathElevation(path, elevator, map) {
new google.maps.Polyline({
path: path,
strokeColor: '#0000CC',
strokeOpacity: 0.4,
map: map
});
elevator.getElevationAlongPath({
'path': path,
'samples': 256
}, plotElevation);
}
function plotElevation(elevations, status) {
var chart = new google.visualization.LineChart(document.getElementById('elevation_chart'));
var data = new google.visualization.DataTable();
data.addColumn('string', 'Sample');
data.addColumn('number', 'Elevation');
for (var i = 0; i < elevations.length; i++) {
data.addRow(['', elevations[i].elevation*3.28084]);
}
var options = {
hAxis: {
title: 'ABC Miles'
},
vAxis: {
title: 'Elevation (ft)'
},
legend: {
position: 'none'
}
};
chart.draw(data, options);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=*****API_KEY_HERE*****&callback=initMap">
</script>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Style the header */
header {
background-color: #ffffff;
text-align: center;
font-size: 35px;
}
/* Create two columns/boxes that floats next to each other */
nav {
float: left;
width: 25%;
background: #ffffff;
}
article {
float: left;
width: 75%;
background: #ffffff;
}
/* Clear floats after the columns */
section:after {
content: "";
display: table;
clear: both;
}
/* Responsive layout - makes the two columns/boxes stack on top of each other instead of next to each other, on small screens */
@media (max-width: 600px) {
nav, article {
width: 100%;
height: auto;
}
}
</style>
</head>
<body>
<header>
<div id="map" style="height:400px;"></div>
</header>
<section>
<nav>
<h2><center>TBD</center></h2>
</nav>
<article>
<div id="elevation_chart"></div>
</article>
</section>
</body>
</html>
- 删除
<script src="https://www.google.com/jsapi"></script>
,您已经包含它。 - 添加脚本以在
head
中包含 maps api,在body
中包含其余的执行,以便只有在 maps api 和 DOM 准备就绪后才能调用它。 - 将
google.charts.setOnLoadCallback(initMap());
更改为google.charts.setOnLoadCallback(initMap);
。您只需要指定在 google 图表完成加载库时应调用的回调。initMap()
宁愿调用该方法。
JSFiddle - http://jsfiddle.net/g4yd6wfp/
<!DOCTYPE html>
<html lang="en">
<head>
<title>Elevation Profiles</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script language="javascript" src="https://maps.googleapis.com/maps/api/js"></script> <!-- add your js api key etc in this file only -->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Style the header */
header {
background-color: #ffffff;
text-align: center;
font-size: 35px;
}
/* Create two columns/boxes that floats next to each other */
nav {
float: left;
width: 25%;
background: #ffffff;
}
article {
float: left;
width: 75%;
background: #ffffff;
}
/* Clear floats after the columns */
section:after {
content: "";
display: table;
clear: both;
}
/* Responsive layout - makes the two columns/boxes stack on top of each other instead of next to each other, on small screens */
@media (max-width: 600px) {
nav, article {
width: 100%;
height: auto;
}
}
</style>
</head>
<body>
<header>
<div id="map" style="height:400px;"></div>
</header>
<section>
<nav>
<h2><center>TBD</center></h2>
</nav>
<article>
<div id="elevation_chart"></div>
</article>
</section>
<script>
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(initMap);
function initMap() {
var path = [
{lat: 36.579, lng: -118.292},
{lat: 36.24, lng: -116.832}];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: path[1],
mapTypeId: 'terrain'
});
var elevator = new google.maps.ElevationService;
displayPathElevation(path, elevator, map);
}
function displayPathElevation(path, elevator, map) {
new google.maps.Polyline({
path: path,
strokeColor: '#0000CC',
strokeOpacity: 0.4,
map: map
});
elevator.getElevationAlongPath({
'path': path,
'samples': 256
}, plotElevation);
}
function plotElevation(elevations, status) {
var chart = new google.visualization.LineChart(document.getElementById('elevation_chart'));
var data = new google.visualization.DataTable();
data.addColumn('string', 'Sample');
data.addColumn('number', 'Elevation');
for (var i = 0; i < elevations.length; i++) {
data.addRow(['', elevations[i].elevation*3.28084]);
}
var options = {
hAxis: {
title: 'ABC Miles'
},
vAxis: {
title: 'Elevation (ft)'
},
legend: {
position: 'none'
}
};https://stackoverflow.com/questions/50843425/inconsistent-uncaught-typeerror#
chart.draw(data, options);
}
</script>
</body>
</html>
在调用外部库的任何内容之前,请确保已加载外部库。尝试
使用window.addEventListener("load", function(){
// Your code here
});
这可能会摆脱您遇到的这些错误