【问题标题】:Best way to add for loop to iterate and draw polylines in google maps?添加for循环以在谷歌地图中迭代和绘制折线的最佳方法?
【发布时间】:2014-12-05 13:17:39
【问题描述】:

我有一个显示地图的以下函数。但是如何在.addPolylines() 函数中设置循环。我得到了正确的纬度和经度,但我添加了一个循环,但它显示了一张空白地图。你能帮我修一下吗?

create_map(<%= raw @boundary_points.to_json %>);

function create_map(boundary_points) {
    var handler = Gmaps.build('Google');

    handler.buildMap({
        internal: {
            id: 'geolocation'
        }
    }, function() {

        for (i = 1; i < boundary_points.length; i++) {
            var ls = boundary_points[i].trim().split(" ");
        }

        var polylines = handler.addPolylines(
            [
                [
                    for (i = 1; i < boundary_points.length; i++) {
                        var ls = boundary_points[i].trim().split(" "); {
                            lat: ls[0],
                            lng: ls[1]
                        },
                    }

                ]
            ], {
                strokeColor: '#FF0000'
            }
        );
        handler.bounds.extendWith(polylines);
        handler.fitMapToBounds();
        handler.getMap().setZoom(15);
        alert(boundary_points);
    });
}

【问题讨论】:

    标签: javascript google-maps ruby-on-rails-4


    【解决方案1】:

    //
    // This is a sample array with points
    //
    var points = ["45.678295 -121.603813", "45.678196 -121.603607"];
    
    //
    // This should be used for handler.addPolylines call
    //
    var polylines = [];
    
    for (i = 0; i < points.length; i++) {
        var ls = points[i].trim().split(" ");
    
        polylines[i] = {};
        polylines[i]["lat"] = parseFloat(ls[0]);
        polylines[i]["lng"] = parseFloat(ls[1]);
    }
    
    console.log(polylines);

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 2018-03-06
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2023-04-06
      • 2019-12-16
      相关资源
      最近更新 更多