【问题标题】:Google maps: API 3, how to set polyline in the center谷歌地图:API 3,如何在中心设置折线
【发布时间】:2012-10-13 05:05:39
【问题描述】:

大家好,我需要能够使折线在地图中心居中

只有代码的必要部分...

success: function(data) {//callback to be executed when the response has been received

    data = JSON.parse(data);
    for (var i=0;i<data.length;i++) {
        flightPlanCoordinates[i] = new google.maps.LatLng(data[i].x,data[i].y);
    }
    map = new google.maps.Map(document.getElementById("map_find"),
    mapOptions);
    flightPath = new google.maps.Polyline({
            path: flightPlanCoordinates,
            strokeColor: "#8a2be2",
            strokeOpacity: 1.0,
            strokeWeight: 3
    });

    flightPath.setMap(map);
    map.setZoom(5);
    map.setCenter(flightPlanCoordinates);
}

这不是我需要的。我的地图没有居中也没有缩放,如何实现?

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 center polyline


    【解决方案1】:

    你可以使用这个函数,以折线为参数调用它:

    function zoomToObject(obj){
        var bounds = new google.maps.LatLngBounds();
        var points = obj.getPath().getArray();
        for (var n = 0; n < points.length ; n++){
            bounds.extend(points[n]);
        }
        map.fitBounds(bounds);
    }
    

    叫它:

    flightPath.setMap(map);
    zoomToObject(flightPath);
    

    只要确保你的地图对象是一个全局变量。

    【讨论】:

    • 我需要在此行之前调用它flightPath.setMap(map); ?
    • 我认为您的代码正在运行,但知道我的地图没有在整个 div 中初始化...当我调用此函数时,就像在整个 div 的 1/6 中一样。有什么想法吗?
    • 最好发布一个包含完整代码的新问题。你的地图
      有尺寸吗? (宽度和高度)?
    • 我发布了一个新问题,请查看:stackoverflow.com/questions/13032245/…
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签