javascript - 谷歌地图API : Gdirections is Not defined

标签 javascript google-maps

我的 map 主要是渲染的,但我在 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>

最佳答案

GDirections 是 API 2 的一部分,但其余代码采用 API 3 语法。您应该使用 DirectionsService反而。此外,您还引用了 GEvent,但这也不起作用。

关于javascript - 谷歌地图API : Gdirections is Not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8162464/

相关文章:

javascript - 将 CSS 样式应用于子节点

javascript - 尝试清除 Google map 标记时出现范围问题

google-maps - 街景全景完全灰色

google-maps - 在使用 Twitter Bootstrap 创建的模式中显示 Google map

javascript - KML 图层未在 Google map 上加载

javascript - Selenium 无法在浏览器 DOM 中定位元素

javascript - 如何通过单击和拖动动态移动 Div

javascript - 如何通过用户脚本/同步加载 google maps api?

javascript - jQuery Animate 无限变换

javascript - 是否可以使用 D3 在圆环图的弧形中插入图标​​?