2011-11-17 61 views
0

我的地图主要是渲染,但我一直在萤火虫中发现一个错误,指出GDirections没有定义。这是萤火虫的完整错误:Google Map API:未指定Gdirections

GDirections is not defined 
initialize()?from=...ions%21 (line 39) 
(?)()?from=...ions%21 (line 89) 
F()jquery.min.js (line 19) 
$(G=[function(), function()], K=function(), F=undefined)jquery.min.js (line 12) 
F()jquery.min.js (line 19) 
F()jquery.min.js (line 19) 
[Break On This Error] gdir = new GDirections(map, document.getElementById("directions")); 

看起来好像不知何故我没有引用API的权利。

另外,不要担心{{addressable.lat}}的奇怪语法。我从一个Django模板中调用这个,并且只传递一个值,在这种情况下是纬度。 这是代码:

<script type="text/javascript" 
     src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 

<script type="text/javascript"> 
    function initialize() { 
    var latlng = new google.maps.LatLng({{ addressable.lat }}, {{ addressable.lon }}); 
    var myOptions = { 
     zoom: 15, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

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

    var marker = new google.maps.Marker({ 
     position: latlng, 
     map: map, 
     title:"{{ addressable.company }}"}); 

     gdir = new GDirections(map, document.getElementById("directions")); 
     GEvent.addListener(gdir, "load", onGDirectionsLoad); 
     GEvent.addListener(gdir, "error", handleErrors); 

     setDirections("Pittsburgh, PA", "{{ addressable.address}}", "en_US"); 
    } 

     function setDirections(fromAddress, toAddress, locale) { 
     gdir.load("from: " + fromAddress + " to: " + toAddress, 
        { "locale": locale }); 
     } 

     function handleErrors(){ 
     if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) 
      alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code); 
     else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) 
      alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code); 

     else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) 
      alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code); 

     else if (gdir.getStatus().code == G_GEO_BAD_KEY) 
      alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code); 

     else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) 
      alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code); 

     else alert("An unknown error occurred."); 

     } 

     function onGDirectionsLoad(){ 
     // Use this function to access information about the latest load() 
     // results. 
     } 
</script> 

回答

2

是GDirections对象2 API的一部分,但你的代码的其余部分是在API 3语法。您应该使用DirectionsService来代替。你也有参考GEvent以及哪些也不起作用。