2012-07-13 51 views
1

我正在使用Google地图,我有一个问题涉及到时间安排问题,我想......特别是何时加载Google地图API脚本的问题。加载脚本的时间/顺序

我越来越:

Uncaught ReferenceError: google is not defined 

不过,如果我看在控制台中谷歌的对象,它显示了就好了。

在尝试使用API​​之前API的脚本尚未完全加载吗?

此外,我有这个主要脚本(下面)加载'延期'属性,但我不认为这应该是一个问题。

我错过了什么?

在此先感谢。

var inrMap = { 
    map : null, 
    markers : [], 

    addCoords : function(){ 
     var regex1 = /\s/; 
     var regex2 = /\./; 
     for(var i in inrMap.locations){ 
      var address = inrMap.locations[i].address; 
      address = address.replace(regex1, '+'); 
      address = address.replace(regex2, ''); 
      $.ajax({ 
       data : 'http://maps.googleapis.com/maps/api/geocode/json?address=' + address + '&sensor=false', 
       dataType : 'json', 
       type : 'get', 
       success : function(data, textStatus, xhr){ 
        inrMap.locations[i].lat = data.geometry.location.lat; 
        inrMap.locations[i].lng = data.geometry.location.lng; 
       }, 
       error : function(xhr, textStatus, errorThrown){ 
        //console.error('problem with geocoding service...'); 
       } 
      });  
     } 
    }, 

    generateMap : function(){ 
     var map_options = { center : new google.maps.LatLng(-34.397, 150.644), zoom : 8, mapTypeId : google.maps.MapTypeId.ROADMAP }; 
     inrMap.map = new google.maps.Map(document.getElementById("map_canvas"), map_options); 
     //load markers 
     for(var i in inrMap.locations){ 
      var position = new google.maps.LatLng(inrMap.locations[i].lat, inrMap.locations[i].lng); 
      inrMap.markers[i] = new google.maps.marker(position, inrMap.locations[i].title);   
      inrMap.markers[i].setMap({ map : inrMap.map, draggable : false, animation : google.maps.Animation.DROP }); 
     } 
    }, 

    bindMarkers : function(){ 
     // $(inrMap.markers).each(function(){ 
     // this.bind('click', function(){ 
     //  //create new info window 
     //  var location_name = this.id; // ***** this needs to be fixed ****** 
     //  var iw = new google.maps.InfoWindow({ content : this.id.title, maxWidth : '300' }); 
     // }) 
     // }); 
    }, 

    bindForm : function(){ 

    } 
} 

inrMap.locations = { 
      jacksonville : { 
       title : 'jacksonville', 
       address : '4651 Salisbury Rd. Suite 170, Jacksonville FL 32256', 
       phone : '(904) 398-0155', 
       link : '/location/florida-regional-office', 
       marker : null 
      }, 
      miami : { 
       title : 'Miami', 
       address : '15050 NW 79 Court Suite 104, Miami Lakes FL 33016', 
       phone : '(305) 403-0594', 
       link : '/location/florida-regional-office', 
       marker : null 
      } 

etc... 




} 

//load google maps js 
var gmaps_script = document.createElement('script'); 
gmaps_script.type = 'text/javascript'; 
gmaps_script.src = 'http://maps.googleapis.com/maps/api/js?key=*****&sensor=false'; 
$('body').append(gmaps_script); 

$(function(){ 

for(var i = 0; i < 4000; i++){ 
    var foo = Math.sin(i); 
} 

//check if locations object is not in local storage (and thus does not have coordinates) 
if(!localStorage.getItem('inr_locations')){ 
    //get lat & long, add to locations obj 
    inrMap.addCoords(); 
    //save object 
    //localStorage.setItem('inr_locations', inrMap.locations); 
} 
else{ 
    //pull locations object from local storage 
    //inrMap.locations = localStorage.getItem('inrMap.locations'); 
} 

//create element to place map in 
var map_canvas = document.createElement('div'); 
$(map_canvas).attr('id', 'map_canvas').appendTo('#content'); 

//generate map 
inrMap.generateMap(); 

//bind events to map markers 
//inrMap.bindMarkers(); 

//bind events to form/links 
//inrMap.bindForm(); 

}); 
+1

是否有一个原因,你循环找到'Math.sin(i)'4000次? – jbabey 2012-07-13 19:23:35

+0

你的html中包含脚本/源代码的顺序? – 2012-07-13 19:26:25

+0

@jbabey哎呦,我拿出来了。只是搞乱了时机,试图弄清楚事情的真相。 – 2012-07-13 19:50:05

回答

0

对于API的脚本尚未完全加载尚未之前,我尝试 使用它?

是的,就是这个问题。

相反的:

var gmaps_script = document.createElement('script'); 
gmaps_script.type = 'text/javascript'; 
gmaps_script.src = 'http://maps.googleapis.com/maps/api/js?key=*****&sensor=false'; 
$('body').append(gmaps_script); 

使用jQuery.getScript()和尝试是这样的:

//Temporary creating global function,for handling loading of the Google Maps Library 
window.__googlemapscallback = function(){ 
    delete window.__googlemapscallback; 

    //do your map initialization here 
}; 

$.getScript('http://maps.googleapis.com/maps/api/js?key=*****&sensor=false&&callback=__googlemapscallback'); 

装载Google Maps V3 API的主要问题是,脚本加载partially.By http://maps.googleapis.com/maps/api/js?key=*****&sensor=false网址,您加载该库的第一个块,然后加载其他块。所有块完全加载后,库调用URL中提供的callback函数。

+0

我已将所有内容移至$ .getScript回调函中,但仍然存在相同的问题。 – 2012-07-13 19:48:51

+0

@ Tim76所以你已经把'inrMap.generateMap();'回调的内部?我对吗? – Engineer 2012-07-13 19:56:15

+0

是的,没错。我已经将generateMap()中的所有内容都移动了。 – 2012-07-13 20:12:25