很抱歉打扰你们,但我已经坚持了半天的问题。
我想使用LineString对象在OpenLayers中绘制多边形线,所以我从文档中复制了这个例子。它运行正常,但我看不到屏幕上的线
代码看起来像这样
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<script type="text/javascript" src="http://openlayers.org/api/OpenLayers.js"></script>
<script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script>
<script src='http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAjpkAC9ePGem0lIq5XcMiuhR_wWLPFku8Ix9i2SXYRVK3e45q1BQUd_beF8dtzKET_EteAjPdGDwqpQ'></script>
<script type="text/javascript">
var map;
var lineLayer ;
var points;
var style;
var polygonFeature
function test()
{
lineLayer = new OpenLayers.Layer.Vector("Line Layer");
style = { strokeColor: '#0000ff',
strokeOpacity: 1,
strokeWidth: 10
};
map.addLayer(lineLayer);
points = new Array();
points[0] =new OpenLayers.LonLat(-2.460181,27.333984 ).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());;
points[1] = new OpenLayers.LonLat(-3.864255,-22.5 ).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());;
var linear_ring = new OpenLayers.Geometry.LinearRing(points);
polygonFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([linear_ring]), null, style);
lineLayer.addFeatures([polygonFeature]);
alert("1");
}
function initialize()
{
map = new OpenLayers.Map ("map_canvas", {
controls:[
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.Attribution()],
maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
maxResolution: 156543.0399,
numZoomLevels: 19,
units: 'm',
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326")
});
// Define the map layer
// Here we use a predefined layer that will be kept up to date with URL changes
layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
map.addLayer(layerMapnik);
var lonLat = new OpenLayers.LonLat(0, 0).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
map.zoomTo(3);
map.setCenter(lonLat, 19);
test();
}
</script>
</head>
<body onload="initialize()" >
<div id="map_canvas" style="width: 828px; height: 698px"></div>
</body>
</html>
我确定我在创建地图或其他东西时缺少一些参数,但我真的无法弄清楚哪一个。
答案 0 :(得分:3)
你在这一行中忘记了's'字符:
lineLayer.addFeatures([lineFeature]);
功能'addFeature'不存在:OpenLayers.Layer.Vector.addFeatures
要查看此功能,请按住键盘上的Shift键并尝试绘制一个框
编辑:好的,现在我知道你是怎么想的。
您需要为数据库中的每个点创建一个OpenLayers.Point对象。一种解决方案是使用Ajax调用自己的PHP函数来检索它们。 PHP文件包含获取它们的代码。我建议以JSON格式返回它们(也许是一个字符串)。使用JQuery框架:
$.ajax({
url: 'your_php_file.php',
dataType: JSON // for example, you can use 'string' type too
success: function(coordinates){
}
});
现在您的数据库中有很多坐标。是时候绘制多边形了。以下链接可能很有用
OpenLayers - how do I draw a Polygon from existing lonLat points?
我希望它可以帮到你。快乐的编码!
<强> EDIT2 强>:
linear_ring是一个属于OpenLayers.Geometry.LinearRing类的对象。 constructor需要一个OpenLayers.Geometry.Points数组,你给它的是OpenLayers.LonLat数组。
你需要修改这个在每个点创建后添加这一行(根据你自己的代码修改索引):
points[0] = new OpenLayers.Geometry.Point(points[0].lon,points[0].lat);
所以你的测试函数如下所示:
function test(){
lineLayer = new OpenLayers.Layer.Vector("Line Layer");
style = { strokeColor: '#0000ff',
strokeOpacity: 1,
strokeWidth: 10
};
points = new Array();
points[0] =new OpenLayers.LonLat(-2.460181,27.333984 ).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());;
points[0] = new OpenLayers.Geometry.Point(points[0].lon,points[0].lat);
points[1] = new OpenLayers.LonLat(-3.864255,-22.5 ).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());;
points[1] = new OpenLayers.Geometry.Point(points[1].lon,points[1].lat);
var linear_ring = new OpenLayers.Geometry.LinearRing(points);
polygonFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([linear_ring]), null, style);
lineLayer.addFeatures([polygonFeature]);
map.addLayer(lineLayer);
}
结果如下:Image result
答案 1 :(得分:0)
是的,OpenLayers可以以编程方式绘制线条。它甚至足够快,可以每秒多次绘制新线条,因此您可以显示实时数据或动画。无论如何,你的代码中有很多东西。也许你的风格搞砸了并且绘制了不可见的线条,或者你的数据点变得不正确并且线条离开了地图。无论如何,我建议这个简单的测试。
创建地图,然后添加矢量图层。挂在名为layerVec的全局变量中的矢量图层上。然后,运行此代码。
var CreateLine = function()
{
var pList = new Array();
for(var i=0; i<200; i++)
{
var p = new OpenLayers.Geometry.Point();
p.x = i;
p.y = i;
pList.push(p);
}
var g = new OpenLayers.Geometry.LineString(pList);
var f = new OpenLayers.Feature.Vector(g,null,null);
layerVec.addFeatures(f);
};
不要担心坐标,这应该是地图中间的一条线。根据您的投影,它可能很小或很大,因此请放大。