簡體   English   中英

調用document.ready之外的ajax函數

[英]calling ajax function that is outside document.ready

我正在嘗試填充Google地圖。 這是我的一些代碼。 為什么這個ajax請求不起作用? 當我將所有內容作為匿名函數粘貼在document.ready()中時,它可以正常工作,但是我想重用此代碼,因此我需要能夠對其進行調用。

$(document).ready(function(){

    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 49.105, lng: -97.568},
        zoom: 4
      });

getTornadoes("test", "test", "yes");            

});

// Get the tornado JSON file
    function getTornadoes (test, test2, test3) {//These are not my real parameters

        $.getJSON('water_pollutants.php', function(data){

            $.each(data.features, function(index, feature){

                var longitude = feature.properties.LONGITUDE;
                var latitude = feature.properties.LATITUDE;
                ...

我收到錯誤InvalidValueError:setMap:不是Map的實例; 而不是StreetViewPanorama的實例。 但是,我認為這不是Google Maps的問題。 過去嘗試引用在document.ready()外部聲明的ajax函數時,我遇到過類似的問題。

我也使用google maps api,這是一個建議:

更改

$(document).ready(function(){

      var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 49.105, lng: -97.568},
        zoom: 4
      });

至:

var map;

$(document).ready(function(){

      map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 49.105, lng: -97.568},
        zoom: 4
      });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM