Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不一致的未捕获类型错误_Javascript_Html_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 不一致的未捕获类型错误

Javascript 不一致的未捕获类型错误,javascript,html,google-maps,google-maps-api-3,Javascript,Html,Google Maps,Google Maps Api 3,我收到以下错误: 未捕获类型错误:无法读取未定义的属性“Map”:第18行 未捕获类型错误:google.visualization.LineChart不是构造函数:第43行 第一个错误,我不断收到,但所有应该加载的东西都加载了。我不知道这是否是个问题 然而,第二个错误有点奇怪。它非常不一致,导致图形无法显示 <!DOCTYPE html> <html lang="en"> <head> <title>Elevation Profiles&

我收到以下错误:

  • 未捕获类型错误:无法读取未定义的属性“Map”:第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>
    
    
    高程剖面
    load('current',{packages:['corechart','line']});
    setOnLoadCallback(initMap());
    函数initMap(){
    变量路径=[
    {lat:36.579,lng:-118.292},
    {lat:36.24,lng:-116.832}];
    var map=new google.maps.map(document.getElementById('map'){
    缩放:8,
    中心:路径[1],
    mapTypeId:'地形'
    });
    var电梯=新的google.maps.ElevationService;
    显示路径高程(路径、电梯、地图);
    }
    功能显示路径高程(路径、电梯、地图){
    新的google.maps.Polyline({
    路径:路径,
    strokeColor:“#0000CC”,
    笔划不透明度:0.4,
    地图:地图
    });
    电梯。GetElevationOngPath({
    “路径”:路径,
    “样本”:256
    },标绘高程);
    }
    功能图高程(高程、状态){
    var chart=new google.visualization.LineChart(document.getElementById('elevation_chart'));
    var data=new google.visualization.DataTable();
    data.addColumn('string','Sample');
    data.addColumn('编号','标高');
    对于(变量i=0;i
  • 删除
    ,您已经包含它了
  • 添加脚本以在
    头部
    中包含maps api,并在
    正文
    中包含其余执行,以便只有在maps api和DOM就绪后才能调用该脚本
  • 更改
    google.charts.setOnLoadCallback(initMap())
    to
    google.charts.setOnLoadCallback(initMap)。您只需指定在GoogleCharts加载完库时应调用的回调
    initMap()
    宁愿调用该方法
  • JSFiddle-

    
    高程剖面
    * {
    框大小:边框框;
    }
    身体{
    字体系列:Arial、Helvetica、无衬线字体;
    }
    /*标题样式*/
    标题{
    背景色:#ffffff;
    文本对齐:居中;
    字体大小:35px;
    }
    /*创建两个相邻浮动的列/框*/
    导航{
    浮动:左;
    宽度:25%;
    背景:#ffffff;
    }
    文章{
    浮动:左;
    宽度:75%;
    背景:#ffffff;
    }
    /*清除列后的浮动*/
    章节:之后{
    内容:“;
    显示:表格;
    明确:两者皆有;
    }
    /*响应式布局-使两列/框在小屏幕上相互堆叠,而不是相邻*/
    @介质(最大宽度:600px){
    导航,文章{
    宽度:100%;
    高度:自动;
    }
    }
    待定
    load('current',{packages:['corechart','line']});
    google.charts.setOnLoadCallback(initMap);
    函数initMap(){
    变量路径=[
    {lat:36.579,lng:-118.292},
    {lat:36.24,lng:-116.832}];
    var map=new google.maps.map(document.getElementById('map'){
    缩放:8,
    中心:路径[1],
    mapTypeId:'地形'
    });
    var电梯=新的google.maps.ElevationService;
    显示路径高程(路径、电梯、地图);
    }
    功能显示路径高程(路径、电梯、地图){
    新的google.maps.Polyline({
    路径:路径,
    strokeColor:“#0000CC”,
    笔划不透明度:0.4,
    
    window.addEventListener("load", function(){
    
        // Your code here
    
    });