不一致的TypeError错误

时间:2018-06-13 17:54:55

标签: javascript html google-maps google-maps-api-3

我收到以下错误:

  1. 未捕获的TypeError:无法读取未定义的属性“Map”:第18行
  2. 未捕获的TypeError:google.visualization.LineChart不是构造函数:第43行
  3. 第一个错误,我经常收到,但是应该加载的所有内容都会加载。我不知道这是不是一个问题。

    然而,第二个错误有点奇怪。它非常不一致,导致图形无法显示。

    <!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>
    

2 个答案:

答案 0 :(得分:1)

  1. 删除<script src="https://www.google.com/jsapi"></script>,您已将其包括在内。
  2. 添加脚本以在head中包含地图api,并在body中包含执行的其余部分,以便只有在地图api和DOM准备就绪后才能调用它。
  3. google.charts.setOnLoadCallback(initMap());更改为google.charts.setOnLoadCallback(initMap);。您只需指定在谷歌图表加载库时应调用的回调。 initMap()宁可调用该方法。
  4. JSFiddle - http://jsfiddle.net/g4yd6wfp/

    &#13;
    &#13;
    <!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>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

确保在从他们调用任何内容之前加载了外部库。尝试使用

包装代码执行
window.addEventListener("load", function(){

    // Your code here

});

这可能会消除你所拥有的这些错误