使用 JavaScript 在 OpenLayers 中绘制一条带线的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2735944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:42:58  来源:igfitidea点击:

Drawing a path with a line in OpenLayers using JavaScript

javascriptmapopenlayers

提问by Andreas Grech

I have seen the examples presented hereof how to draw a line but the examples only show how to do it with the mouse, by clicking.

我已经看过这里介绍的如何画一条线的例子,但这些例子只展示了如何用鼠标点击。

What I want to do is draw the line manually using JavaScript given a list of Longitude and Latitude coordinates.

我想要做的是在给定经度和纬度坐标列表的情况下使用 JavaScript 手动绘制线

The reason I cannot work on the source provided in the link above is because they are only calling activateon the feature, and then let the user point and click on the map.

我无法处理上面链接中提供的源的原因是因为他们只是调用activate该功能,然后让用户指向并单击地图。

Has anyone ever drew a path on an OpenLayers map programatically?

有没有人以编程方式在 OpenLayers 地图上绘制路径?

What I want to do is exactly this: http://openspace.ordnancesurvey.co.uk/openspace/example4.html, but without using OpenSpace.

我想要做的正是这个:http: //openspace.ordnancesurvey.co.uk/openspace/example4.html,但不使用 OpenSpace。

回答by Drahcir

You would need to make use of the LineStringobject

您需要使用LineString对象

Here is an example:

下面是一个例子:

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(lon1, lat1),
   new OpenLayers.Geometry.Point(lon2, lat2)
);

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]);

Assuming mapis your map object and lonand latare float values.

假设map是您的地图对象,lon并且lat是浮点值。

回答by tony gil

this page is a classic exampleof animation via javascript using openlayers.

此页面是使用 openlayers 通过 javascript 制作动画的经典示例

it uses a filter strategy to define what to show at what moment in time.

它使用过滤策略来定义在什么时间显示什么。

full javascript available.

完整的 javascript 可用。

回答by RoToRa

I've never done it myself before, but I know OpenSteetMap does it. For example:

我以前从来没有自己做过,但我知道 OpenSteetMap 做到了。例如:

http://www.openstreetmap.org/?way=23649627

http://www.openstreetmap.org/?way=23649627

No idea how difficult it would be to work through their code.

不知道通过他们的代码工作会有多困难。