添加for循环以在Google地图中迭代和绘制折线的最佳方法?

时间:2014-12-05 13:17:40

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

我有一个显示地图的下方功能。但是如何在.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);
    });
}

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
//
// 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);
&#13;
&#13;
&#13;