使用wgs84坐标绘制多边形openlayers

时间:2014-01-27 23:43:09

标签: openlayers

我在此网站中找到了此代码,这是link

var options = {
    div: "map",
    zoom: 13,
    center: [-9075004.4955698, 5028040.5259088],
    layers: [
        new OpenLayers.Layer.OSM()
    ]
},
source = [
    [{x: -9075004, y: 5028040}, {x:-9079132, y: 5025403}, {x: -9072673, y: 5023568}],
    [{x: -9074004, y: 5026040}, {x:-9073132, y: 5027403}, {x: -9074673, y: 5026568}],
    [{x: -9073004, y: 5027040}, {x:-9072132, y: 5029403}, {x: -9075673, y: 5028568}]
],
polygonList = [],
multuPolygonGeometry,
multiPolygonFeature,    
vector = new OpenLayers.Layer.Vector('multiPolygon'),
map = new OpenLayers.Map(options);


for (var i=0; i<source.length; i+=1) {
    var pointList = [];
    for (var j=0; j<source[i].length; j+=1) {
        var point = new OpenLayers.Geometry.Point(source[i][j].x, source[i][j].y);
        pointList.push(point);
    }
    var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
    var polygon = new OpenLayers.Geometry.Polygon([linearRing]);
    polygonList.push(polygon);
}
multuPolygonGeometry = new OpenLayers.Geometry.MultiPolygon(polygonList);
multiPolygonFeature = new OpenLayers.Feature.Vector(multuPolygonGeometry);

vector.addFeatures(multiPolygonFeature);
map.addLayer(vector);

我需要的是能够将坐标作为lat / lon表达式。我已经读过它的WGS84转换了。我也阅读了this answer,但我不知道如何使第一代码与WGS84坐标一起使用。

2 个答案:

答案 0 :(得分:0)

您的问题不是很明确,我假设您想使用所述代码,但使用WGS84坐标。

您的地图有一个特定的投影,根据您使用的地图而有所不同(Google可能会使用与OpenStreetMaps不同的地图)。机会是,它不是WGS84。

但OpenLayer的几何对象提供transform method

// EPSG:4326 == WGS84
new OpenLayers.Geometry.Point(8.54, 47.37).transform(
    new OpenLayers.Projection("EPSG:4326"),
    map.getProjectionObject()
)

这将从WGS84转换为地图的当前投影

答案 1 :(得分:0)

好吧,我早些时候无法自言自语,所以就在这里。我希望足够清楚:

this code和阅读openlayers并查看其示例,在脚本顶部添加了下一行:

var fromProjection = new OpenLayers.Projection("EPSG:4326");   // Transform from WGS 1984
var toProjection   = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection

接下来,我插入了下一行:

point.transform(fromProjection, toProjection);

所以代码依旧如此:

  <script type="text/javascript">
    $(document).ready(function() {
      var fromProjection = new OpenLayers.Projection("EPSG:4326");   // Transform from WGS 1984
      var toProjection   = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
      var options = {
        div: "map",
        zoom: 13,
        center: [-9075004.4955698, 5028040.5259088],
        layers: [
            new OpenLayers.Layer.OSM()
        ]
      },
      source = [
        [{x: -9075004, y: 5028040}, {x:-9079132, y: 5025403}, {x: -9072673, y: 5023568}],
        [{x: -9073004, y: 5027040}, {x:-9072132, y: 5029403}, {x: -9075673, y: 5028568}]
      ],
      fuente = [
        //[{x: -9074004, y: 5026040}, {x:-9073132, y: 5027403}, {x: -9074673, y: 5026568}]
        [{x: -68.06400954723358, y: -38.95894160235222}, {x:-68.0585914850235, y: -38.95984678037724}, {x: -68.0654364824295, y: -38.964405865315236}]
      ],

      polygonList = [],
      multuPolygonGeometry,
      multiPolygonFeature,    
      vector = new OpenLayers.Layer.Vector('multiPolygon'),
      map = new OpenLayers.Map(options);

      for (var i=0; i<source.length; i+=1) {
          var pointList = [];
          for (var j=0; j<source[i].length; j+=1) {
              var point = new OpenLayers.Geometry.Point(source[i][j].x, source[i][j].y);
              pointList.push(point);
          }
          var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
          var polygon = new OpenLayers.Geometry.Polygon([linearRing]);
          polygonList.push(polygon);
      }
      multuPolygonGeometry = new OpenLayers.Geometry.MultiPolygon(polygonList);
      multiPolygonFeature = new OpenLayers.Feature.Vector(multuPolygonGeometry);

      vector.addFeatures(multiPolygonFeature);
      map.addLayer(vector);

      for (var i=0; i<fuente.length; i+=1) {
          var pointList = [];
          for (var j=0; j<fuente[i].length; j+=1) {
              var point = new OpenLayers.Geometry.Point(fuente[i][j].x, fuente[i][j].y);
              // transform from WGS 1984 to Spherical Mercator
              point.transform(fromProjection, toProjection);
              pointList.push(point);
          }
          var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
          var polygon = new OpenLayers.Geometry.Polygon([linearRing]);
          polygonList.push(polygon);
      }
      multuPolygonGeometry = new OpenLayers.Geometry.MultiPolygon(polygonList);
      multiPolygonFeature = new OpenLayers.Feature.Vector(multuPolygonGeometry);

      vector.addFeatures(multiPolygonFeature);
      map.addLayer(vector);

    });

数组中,我们定义了两个球形墨卡托多边形,而在 fuente 数组中,我们定义了一个WGS1984多边形。