简体   繁体   English

Google Map API:未定义Gdirections

[英]Google Map API: Gdirections is Not defined

I've got my map mostly rendering, but I keep getting an error in firebug stating that GDirections is not defined. 我的地图主要是渲染,但我在firebug中不断收到错误,声明GDirections未定义。 This is the full error in firebug: 这是firebug中的完整错误:

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")); 

It seems like somehow I am not referencing the API right. 看起来好像我没有正确引用API。

Also, don't worry about the weird syntax of {{ addressable.lat }}. 另外,不要担心{{addressable.lat}}的奇怪语法。 I am calling this from a Django template and that just passes in a value, in this case for the latitude. 我从一个Django模板调用它,只传入一个值,在这种情况下为纬度。 This is the code: 这是代码:

 <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>

GDirections is part of API 2, but the rest of your code is in API 3 syntax. GDirections是API 2的一部分,但其余代码采用API 3语法。 You should be using the DirectionsService instead. 您应该使用DirectionsService Also you've got references to GEvent as well which won't work either. 你也有对GEvent的引用,但也无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM