如何借助JavaScript中的数组对象在OpenStreetMap上绘制折线?

时间:2018-06-26 12:13:23

标签: javascript jquery

我想借助标记数组对象在OpenStreetMap上绘制折线 目前,我已从数据库中选择了所有标记(经度,纬度)值,并且在此标记的基础上,我想绘制一条折线以显示特定用户的路线。

[window.onload = function GetOSMMap() {
    map = new OpenLayers.Map("dvMap");
    var mapnik = new OpenLayers.Layer.OSM();
    var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
    var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection                                                                                            
    var zoom = 15;
    map.addLayer(mapnik);

    var marker = new OpenLayers.Layer.Markers("Markers");

    for (i = 0; i < markers.length; i++) {
        var data = markers\[i\]
        var lonLat = new OpenLayers.LonLat(data.lng, data.lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());

        map.addLayer(marker);
        marker.addMarker(new OpenLayers.Marker(lonLat));
        map.setCenter(lonLat, zoom);

    }
    var lineLayer = new OpenLayers.Layer.Vector("Line Layer");

    map.addLayer(lineLayer);
    map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path));
    var points = new Array(
        new OpenLayers.Geometry.Point(data.lng, data.lat)
    );
    points.push(lonLat);
    var line = new OpenLayers.Geometry.LineString(points);

    var style = {
        strokeColor: '#0000ff',
        strokeOpacity: 0.5,
        strokeWidth: 5
    };

    var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
    lineLayer.addFeatures(\[lineFeature\]);

}]

1 个答案:

答案 0 :(得分:0)

1)更改数组变量声明以创建简单的数组对象
var points = new Array(         新的OpenLayers.Geometry.Point(data.lng,data.lat)     );至 var points = new Array(); 2)改变points.push(lonLat);至  points.push(新的OpenLayers.Geometry.Point(data.lng,data.lat));并将此行写在循环标记下。 3)多加一行 line.transform(new OpenLayers.Projection(“ EPSG:4326”),new OpenLayers.Projection(“ EPSG:900913”));