Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.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 Google Map InfoBox异步集成不起作用_Javascript_Jquery_Google Maps_Google Maps Api 3_Infobox - Fatal编程技术网

Javascript Google Map InfoBox异步集成不起作用

Javascript Google Map InfoBox异步集成不起作用,javascript,jquery,google-maps,google-maps-api-3,infobox,Javascript,Jquery,Google Maps,Google Maps Api 3,Infobox,在我的网站上,我一直在寻找通过谷歌的InfoBox插件实现异步谷歌地图的方法。这是我的代码: <script type="text/javascript"> jQuery(document).ready(function() { loadScript(); }); function initialize() { mapCenter = new google.maps.LatLng(-34.770215, 149.706806)

在我的网站上,我一直在寻找通过谷歌的InfoBox插件实现异步谷歌地图的方法。这是我的代码:

<script type="text/javascript">
    jQuery(document).ready(function() {
        loadScript();
    });

    function initialize() {
        mapCenter = new google.maps.LatLng(-34.770215, 149.706806);
        var mapProp = {
            center: mapCenter,
            zoom:18,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scrollwheel: false,
            draggable: false
        };
        var map = new google.maps.Map(document.getElementById("map"),mapProp);
        var marker=new google.maps.Marker({
          position:mapCenter,
        });
        marker.setMap(map);
        google.maps.event.addListener(map, 'click', function(event){
            this.setOptions({scrollwheel:true});
             this.setOptions({draggable:true});
        });
        google.maps.event.addListener(map, 'mouseout', function(event){
            this.setOptions({scrollwheel:false});  
            this.setOptions({draggable:false});  
        });
    }


    function loadScript() {
        var script = document.createElement("script");
        script.src = "http://maps.googleapis.com/maps/api/js?callback=initialize";
        document.body.appendChild(script);
        script.onload = function() {
            var scriptInfoBox = document.createElement("script");
            scriptInfoBox.type = "text/javascript";
            scriptInfoBox.src = "{{ 'assets/javascript/googlemap-infobox.js?callback=initialize2'|theme }}";
            document.body.appendChild(scriptInfoBox);
            scriptInfoBox.onload = function() {
                infobox = new InfoBox({
                     content: document.getElementById("infobox"),
                     disableAutoPan: false,
                     maxWidth: 150,
                     pixelOffset: new google.maps.Size(-140, 0),
                     zIndex: null,
                     boxStyle: {
                        background: "url('http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/examples/tipbox.gif') no-repeat",
                        opacity: 0.75,
                        width: "280px"
                    },
                    closeBoxMargin: "12px 4px 2px 2px",
                    closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
                    infoBoxClearance: new google.maps.Size(1, 1)
                });
                 google.maps.event.addListener(marker, 'click', function() {
                    infobox.open(map, this);
                    map.panTo(loc);
                });
            }
        };
    }
</script>
这是我的地图

<div id="map"></div>
这是我在信息箱的Div

<div class="infobox-wrapper">
    <div id="infobox">
        The contents of your info box. It's very easy to create and customize.
    </div>
</div>
错误说明:未捕获引用错误:未定义标记
救命啊

您正在初始化函数中将marker创建为局部变量。它对loadScript函数不可用。将其设为全局变量,错误将停止

更新:事实上,您可能会遇到与map变量完全相同的问题。因此,我将其添加到下面的代码中

var marker, map;

function initialize() {
    ...
    map = new google.maps.Map(...);
    marker=new google.maps.Marker({
        position:mapCenter,
    });
}
请尝试以下代码:

<script type="text/javascript">
jQuery(document).ready(function() {
    var marker ;
    loadScript();
});

function initialize() {
    mapCenter = new google.maps.LatLng(-34.770215, 149.706806);
    var mapProp = {
        center: mapCenter,
        zoom:18,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false,
        draggable: false
    };
    var map = new google.maps.Map(document.getElementById("map"),mapProp);
    marker =new google.maps.Marker({
      position:mapCenter,
    });
    marker.setMap(map);
    google.maps.event.addListener(map, 'click', function(event){
        this.setOptions({scrollwheel:true});
         this.setOptions({draggable:true});
    });
    google.maps.event.addListener(map, 'mouseout', function(event){
        this.setOptions({scrollwheel:false});  
        this.setOptions({draggable:false});  
    });
}


function loadScript() {
    var script = document.createElement("script");
    script.src = "http://maps.googleapis.com/maps/api/js?callback=initialize";
    document.body.appendChild(script);
    script.onload = function() {
        var scriptInfoBox = document.createElement("script");
        scriptInfoBox.type = "text/javascript";
        scriptInfoBox.src = "{{ 'assets/javascript/googlemap-infobox.js?callback=initialize2'|theme }}";
        document.body.appendChild(scriptInfoBox);
        scriptInfoBox.onload = function() {
            infobox = new InfoBox({
                 content: document.getElementById("infobox"),
                 disableAutoPan: false,
                 maxWidth: 150,
                 pixelOffset: new google.maps.Size(-140, 0),
                 zIndex: null,
                 boxStyle: {
                    background: "url('http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/examples/tipbox.gif') no-repeat",
                    opacity: 0.75,
                    width: "280px"
                },
                closeBoxMargin: "12px 4px 2px 2px",
                closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
                infoBoxClearance: new google.maps.Size(1, 1)
            });
             google.maps.event.addListener(marker, 'click', function() {
                infobox.open(map, this);
                map.panTo(loc);
            });
        }
    };
}