如何在osm中的两点之间画一条线

时间:2013-01-03 06:42:59

标签: openlayers openstreetmap

我想在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]);

我尝试使用此代码,但它没有用,所以请帮助我。

1 个答案:

答案 0 :(得分: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();