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

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

本文介绍了在OpenLayers中使用JavaScript绘制一条路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了示例,如何画一条线,但是示例只显示如何使用鼠标,通过点击。

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

我想要做的是使用JavaScript手动绘制行给定一个经度列表和纬度坐标。

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

上面链接中提供的源无法工作的原因是因为它们只调用 activate 的功能,然后让用户点击地图上。

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

有没有人曾经在OpenLayers地图上编制路径?

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

我想做的是这样的:,但不使用OpenSpace。

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

推荐答案

您需要使用对象

这是一个例子:

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

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

Assuming map is your map object and lon and lat are float values.

这篇关于在OpenLayers中使用JavaScript绘制一条路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:35