Google Map API:未定义Gdirections

时间:2011-11-17 05:17:52

标签: javascript google-maps

我的地图主要是渲染,但我不断在firebug中收到错误,声明未定义GDirections。这是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")); 

似乎某种程度上我没有正确引用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>

1 个答案:

答案 0 :(得分:2)

GDirections是API 2的一部分,但其余代码采用API 3语法。您应该使用DirectionsService代替。此外,您也可以参考GEvent,但也无法使用。