1

我想在 osm 地图中的两点之间画一条线

map = new OpenLayers.Map();

var start_point = new OpenLayers.Geometry.Point(0,10);
var end_point = new OpenLayers.Geometry.Point(30,0);

var vector = new OpenLayers.Layer.Vector();
vector.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString([start_point, end_point]))]);
map.addLayers([vector]);

我尝试了这段代码,但它没有用,所以请帮助我。

4

1 回答 1

1

绘制线串的代码是正确的。由于您没有发布完整的代码和错误的确切描述,因此只有两个疯狂的猜测。您的地图可能采用 900913 投影,坐标采用 WGS。转换它们:

var start_point = new OpenLayers.Geometry.Point(0,10);
var end_point = new OpenLayers.Geometry.Point(30,0);

start_point.transform(
    new OpenLayers.Projection("EPSG:4326"),
    new OpenLayers.Projection("EPSG:900913")
); 
end_point.transform(
    new OpenLayers.Projection("EPSG:4326"),
    new OpenLayers.Projection("EPSG:900913")
); 

此外,您可能需要适合视图:

map.zoomToMaxExtent();
于 2013-01-03T10:13:17.140 回答