Uncaught ReferenceError:在加载时未定义init

问题描述 投票:-1回答:2

这里有一个类似的问题,但它不允许我解决我的问题。我正在使用Google Maps JavaScript API创建网页。我已经用客户端将要从我的mysql数据库获取地图坐标/数据的路由制作了自己的Restfull服务器。

我能够加载地图,但是在将makeRequest代码添加到浏览器后,我仍然收到此错误。我当前遇到的错误是Uncaught ReferenceError:未定义init在加载时

我相信它指的是正文onload =“ init();”在我的HTML中。

这是我的代码,由于我的JS知识有限,因此非常感谢您的协助。预先谢谢你

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Team Locations</title>
    <style type="text/css">
        body {
            font: normal 14px Verdana;
        }

        h1 {
            font-size: 24px;
        }

        h2 {
            font-size: 18px;
        }

        #sidebar {
            float: right;
            width: 30%;
        }

        #main {
            padding-right: 15px;
        }

        .infoWindow {
            width: 220px;
        }

    </style>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDNMFE1XBzVnhPOI5IH90FrwumFfLo5x40&callback=initMap"></script>
    <script type="text/javascript">
        //<![CDATA[

        var map;

        // Centre of York
        var center = new google.maps.LatLng(53.957741, -1.082260);

        var geocoder = new google.maps.Geocoder();
        var infowindow = new google.maps.InfoWindow();

        function init() {

            var mapOptions = {
                zoom: 13,
                center: center,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }

            map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);


            makeRequest('http://localhost:5032/', function(data) {

                    var data = JSON.parse(data.responseText);

                    for (var i = 0; i < data.length; i++) {
                        displayLocation(data[i]);
                    });
            },

                function makeRequest(url, callback) {
                    var request;
                    if (window.XMLHttpRequest) {
                        request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
                    } else {
                        request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
                    }
                    request.onreadystatechange = function() {
                        if (request.readyState == 4 && request.status == 200) {
                            callback(request);
                        }
                    }
                    request.open("GET", url, true);
                    request.send();
                }
            });




            //]]>

    </script>
</head>

<body onload="init();">

    <h1>Team Locations</h1>

    <section id="sidebar">
        <div id="directions_panel"></div>
    </section>

    <section id="main">
        <div id="map_canvas" style="width: 70%; height: 500px;"></div>
    </section>

</body>

</html>
javascript google-maps-api-3 client google-maps-markers
2个回答
0
投票

您正在渲染而不是在加载时调用init()

尝试传递函数的引用。

<body onload="init">

0
投票

简单的语法问题。您的init函数以逗号,结尾,并且在代码末尾还有一个附加的});,因此您似乎从对象模式复制了一些代码,但是错过了开头({或需要删除关闭})init()函数后的逗号。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.