【问题标题】:Center encoded Polyline - Google MAP API V3中心编码折线 - Google MAP API V3
【发布时间】:2021-02-04 22:04:06
【问题描述】:

我想使用 API V3 将带有动态编码折线的地图居中并自动缩放。

我的编码 Polyine 在跟踪 1 中,从 ASP.net C# 服务器端获取:

    var trace1 = "<%= scriptString %>";

    vol = new google.maps.Polyline({
        map: map,
        strokeColor: '#ff0000',
        strokeOpacity: 1.0,
        strokeWeight: 3
    });



    var trace = google.maps.geometry.encoding.decodePath(trace1);

    vol.setPath(trace);

    var bounds = new google.maps.LatLngBounds();
    bounds.getCenter();
    map.fitBounds(bounds);

但它不起作用...它使地图以北太平洋为中心,而我的折线在法国...

我没有任何 Javascript 错误。有什么想法吗?

【问题讨论】:

    标签: api google-maps center polyline


    【解决方案1】:

    您需要扩展边界以包含路径上的点。

    var bounds = new google.maps.LatLngBounds();
    vol.getPath().forEach(function(LatLng) {
       bounds.extend(LatLng);
    });
    map.fitBounds(bounds);
    

    【讨论】:

    • 很好的答案。我刚刚将map.setBounds(bounds) 更改为map.fitBounds(bounds)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2014-03-09
    • 2013-02-10
    • 1970-01-01
    • 2012-01-25
    • 2014-02-22
    • 2011-08-04
    相关资源
    最近更新 更多