javascript - JavaScript 中函数未定义

标签 javascript jquery google-maps

我正在尝试在 javascript 中使用计时器,每 5 秒从 cshtml 中的其他对象轮询某些数据,并将其更新到 javascript 中的函数中。

我已经探索了以下代码,但它似乎抛出了初始化未定义的错误

function initialize() {

    var mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: parseInt(5),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("googleMap")
        , mapProp);


}

$(document).ready(function () {
    initialize();
});
window.setInterval(function () {
    /// call your function here
    initialize();
}, 5000);

但是,这工作得很好

function initialize() {

    var mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: parseInt(5),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("googleMap")
        , mapProp);


}

$(document).ready(function () {
    initialize();
});

由于我想让变量成为全局变量,以便我可以每 5 秒更新一次 map 上的焦点,因此我尝试将其设为全局变量

    var mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: parseInt(5),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

但是,脚本似乎一直给我一个 google is undefined 的错误。

无论如何我可以解决我的问题吗?

例如,我想每 5 秒更新一次 new google.maps.LatLng(51.508742, -0.120850) 中的参数。

最佳答案

这是工作代码,

<!DOCTYPE html>
<html>
    <head>
        <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
        </script>
        <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
        <script>
            var mapProp;
            $(document).ready(function(){initialize();})
            function initialize()
            {
                mapProp = changeMapProp();
                var map=new google.maps.Map(document.getElementById("googleMap")
                ,mapProp);
            }
            function changeMapProp() {
                mapProp = {
                    center:new google.maps.LatLng(52.508742,-0.120850),
                    zoom:5,
                    mapTypeId:google.maps.MapTypeId.ROADMAP
                };
                return mapProp;
            }
            window.setInterval(function () {
                /// call your function here
                initialize();
            }, 5000);

        </script>
    </head>

    <body>
        <div id="googleMap" style="width:500px;height:380px;"></div>

    </body>
</html> 

关于javascript - JavaScript 中函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19267269/

相关文章:

android - 标记消失

google-maps - Google Directions API 公交返回总是 "ZERO_RESULTS"

javascript - 将复选框链接到 Vue.js 中的列表项

javascript - Vue.js:无法为动态创建的 HTML 元素编译 SASS 样式

javascript - 让用户在深色和浅色模式之间进行选择(保存每个页面的设置,cookies?)

javascript - jQuery - 为每个对象创建新的 div?

javascript - 系列 Ajax 请求不等待完成

javascript - google.maps.places 及其自动完成方法未定义

javascript - 带有 NestJS + Typescript + Webpack + node_modules 的单个文件包

javascript - 根据 Angular 中的 Ng-Repeat 中当前选定的菜单项显示元素