gpt4 book ai didi

javascript - 打开图层 LineString 不工作

转载 作者:行者123 更新时间:2023-11-30 13:35:38 26 4
gpt4 key购买 nike

很抱歉打扰你们,但我已经被他的问题困扰了半天。

我想使用 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&amp;v=2&amp;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>

我确定我在创建 map 或其他东西时遗漏了一些参数,但我真的不知道是哪一个。

最佳答案

你忘记了这一行中的 '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

关于javascript - 打开图层 LineString 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5032699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com