2014-01-27 156 views
-1

,我發現這個代碼在這個網站,這是link繪製多邊形的OpenLayers與WGS84座標

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

我需要的是能夠把座標緯度/經度表達。我讀過它的WGS84轉換。我也讀過this answer,但我不知道如何使第一代碼與WGS84座標一起工作。

+0

我使用Proj4來做這些類型的座標轉換。儘管如此,Proj4很難與之合作,雖然它非常強大。 http://trac.osgeo.org/proj/ –

+0

非常感謝您的快速回答。我會嘗試。 –

+0

對不起,我沒有更多的細節。我沒有我的Proj4代碼在我面前。如果您有任何疑問可以再次找到我,我會發現我使用Proj4。 –

回答

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轉換爲地圖的當前投影

+0

是的,這是我需要的。感謝您的回答。並閱讀您的帖子,我注意到我忘了說我正在使用openstreetamp。抱歉。 –

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多邊形。