首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Magento中使用Google地图方向时出错(未定义ReferenceError: google未定义)

在Magento中使用Google地图方向时出错(未定义ReferenceError: google未定义)
EN

Stack Overflow用户
提问于 2013-03-19 22:27:13
回答 1查看 2.1K关注 0票数 0

我已经创建了一个方向模板文件,专门利用谷歌的方向。我让剧本在Magento外面工作。不幸的是,当我将脚本移动到page/directions.phtml控制台时,会返回以下错误:

“未定义的ReferenceError:谷歌没有定义”

当我尝试单击触发calcRoute()函数的test时,会得到以下错误:

“未定义TypeError:不能调用未定义的方法‘路由’”

我认为这些错误与Google调用有关,但是图形地图在页面上显示和运行都很好。这些错误具体涉及调用google.maps.DirectionsService()来定义下面所示的javascript第2行中的directionsService,并在最后调用directionsService.route。两者都与方向有关。

为什么地图会显示,但当脚本在我的html测试中工作时,方向会抛出错误?我真的很困惑。

Javascript

代码语言:javascript
复制
  var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;

  function directionsMap() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(38.77627, -95.910965);
    var mapOptions = {
      zoom:4,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }
    map = new google.maps.Map(document.getElementById('map-directions'), mapOptions);
    directionsDisplay.setMap(map);

    //Place Markers
    var baldwinsvilleMarker = new google.maps.Marker({
        position: new google.maps.LatLng(00.00000, 00.00000),
        map: map,
        title:"store location 1"
    });
    var sacramentoMarker = new google.maps.Marker({
        position: new google.maps.LatLng(00.00000, 00.00000),
        map: map,
        title:"store location 2"
    });
    var stLouisMarker = new google.maps.Marker({
        position: new google.maps.LatLng(000.00000, -000.00000),
        map: map,
        title:"store location 3"
    });
    var catersvilleMarker = new google.maps.Marker({
        position: new google.maps.LatLng(00.000000, -84.000000),
        map: map,
        title:"store location 4"
    });

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("get-directions"));

  }

  function calcRoute() {
    console.log("calcRoute ran")
    var start = document.getElementById('start').value;
    var end = document.getElementById('end').value;
    console.log(start + ' ' + end)
    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  }

代码语言:javascript
复制
           #I am including this line in the document head
           <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
           <style onload="directionsMap()">
            #map-directions{width: 65%; height: 300px; float: left; border:5px solid #EEEEEE;}
            #get-directions{width: 65%; float: left;}
            .map-sidebar{width: 32%; height: 300px; float: right;}
            </style>

            <div id="map-directions"></div>
            <div class="map-sidebar">
                <h2>Get Directions</h2>

                <input type="text" id="start" value="chicago, il"/>
                <b>End: </b>
                <select id="end" onchange="calcRoute();">

                  <option value="value 1">
                      store location 1
                  </option>

                  <option value="value 2">
                      store location 2
                  </option>

                  <option value="value 3">
                      store location 3
                  </option>

                  <option value="value 4">
                      store location 4
                  </option>

                </select>
                <button onclick="calcRoute();"> test</button>


            </div>
            <div id="get-directions"></div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-19 22:49:29

您应该一步一步地进行调用,例如:

  1. 首先调用
  2. 然后给你自己的JS打电话
  3. body onload=directionsMap()中发射style,而不是装载style

我认为你的主要问题是,在谷歌完成加载之前,你正在调用directionsMap()函数。

因此,我认为在directionsMap()加载事件中调用jQuery更好的选择如下:

代码语言:javascript
复制
$(function() {
   directionsMap()
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15511713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档