javascript - 谷歌地图 API : Map width gets resized onclick but route is still loaded with original width

标签 javascript google-maps google-maps-api-3

我有一张通过 Google Maps API v3 加载的 map 。 我有一个 onclick 执行 2 个操作的表单:1)调用函数来通过谷歌交通计算路线,2)减少 map 宽度。

<form>
    <input type="text" id="start" name="start">
    <input type="button" onclick="reducemap();calcRoute();" value="Go">
</form>

function reducemap() {
document.getElementById("map-canvas").style.width="620px";
}

function calcRoute() {
    var start = document.getElementById('start').value;
      var end = "An Address in Rome";
      var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.TRANSIT
      };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
}

它的工作原理就像一个魅力,唯一的问题是路线不居中,因为它是根据原始宽度(而不是缩小的宽度)计算的。您可以在此处实时查看(因此您可以查看代码:http://goo.gl/wr3qi)。

最佳答案

当您更改包含 map 的 DIV 的大小时,您还需要将此大小更改通知 Maps API。因此,假设您有一个带有映射引用的 map 变量,您的 reducemap() 函数可能应该是:

function reducemap() {
    document.getElementById("map-canvas").style.width = "620px";
    google.maps.event.trigger( map, "resize" );
}

关于javascript - 谷歌地图 API : Map width gets resized onclick but route is still loaded with original width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16259024/

相关文章:

javascript - Google Maps API 地理编码 - 波兰地址没有邮政编码?

javascript - 为什么无法根据Google Maps地理编码geometry.bounds找到位置,但可以通过MongoDB中的geometry.viewport找到位置?

javascript - 调用 Google maps infowindow.open 会导致网站向下滚动到 infowindow

javascript - 选择字段中的空白选项

javascript - 使用css或js关闭时如何将bootstrap 4模态对话框向右滑动

javascript - ember this.get 未捕获类型错误 : Object #<Window> has no method 'get'

javascript - 与 Google Maps Javascript API v3 一起使用时的服务使用限制

javascript - 如何过滤文档中的嵌入文档?

iphone - 在 iPhone 应用程序中使用时移动 Google map Logo

google-maps - 使用Google Map Tracks API