1

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

我想使用 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>

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

4

2 回答 2

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 - 如何从现有的 lonLat 点绘制多边形?

我希望它对你有帮助。快乐编码!

编辑2

linear_ring 是属于 OpenLayers.Geometry.LinearRing 类的对象。构造函数需要一个 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);

 }

结果是这样的:图像结果

于 2011-02-17T21:38:00.793 回答
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);
};

不要担心坐标,这应该是地图中间的一条线。根据您的投影,它可能很小或很大,因此请放大。

于 2012-01-05T23:10:55.067 回答