簡體   English   中英

打開第2層,從Ajax響應創建要素並將其添加到矢量層

[英]Open Layers 2 Creating and adding Feature to Vector Layer from Ajax Response

如標題所示,創建要素並將其添加到已創建的矢量層。 我正在從服務器獲取GeoJSON並試圖以某種方式添加到矢量層,但是我無法使其正常工作。所以基本上我在問如何從我的GeoJSON中獲取Feature元素,因此我可以將其添加到矢量層。 我現在有什么..

這是我從服務器獲取的GeoJSON:

{"type":"MultiPolygon","coordinates":[[[[20.5629940201429,48.9488601183337],[20.5630121528311,48.9489447276126],[20.563289335522,48.9489141101973],[20.563260061873,48.9488286413488],[20.5629940201429,48.9488601183337]]]]}

接下來,我在JavaScript中有嘗試嘗試魔術的addVector函數。(變量GeoJS是從服務器獲取的GeoJSON)

function addVector(geoJS){
    var feature = new OpenLayers.Feature.Vector( new OpenLayers.Geometry.MultiPolygon(geoJS) );
    vector = new OpenLayers.Layer.Vector("Magic"); 
    map.addLayer(vector);
    vector.addFeatures([feature]);
}

是的,我知道我在其中創建功能的第二行是錯誤的,但是我無法正確設置,因此我想id無關緊要我現在寫的內容...我嘗試使用var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-70.702451, 42.374473);並且它在地圖上的工作位置不是我想要的位置,但是我知道我必須對投影進行某些操作...現在就不重要了。

順便說一句我有這個

vector = new OpenLayers.Layer.Vector("GeoJSON",
{
    projection       : "EPSG:4326",
    onFeatureInsert  : postIns,
    strategies       : [new OpenLayers.Strategy.Fixed()],
    protocol         :  new OpenLayers.Protocol.HTTP({
                            url: "test.php",
                            format: new OpenLayers.Format.GeoJSON()
                        })
});

這是可行的,位置是我想要的位置,它的完美之處在於它僅在我對域發出請求時才有效,而我嘗試訪問的服務器在另一個服務器上(我知道我可以設置標頭並且可以使用),但是我不這樣做不想這樣做。

所以基本上我在問如何從我的GeoJSON獲取功能。 我對OpenLayers真的很陌生,因此如果我提出明顯的要求,對不起。

要使用官方示例的簡化版本:

var inputGeoJson = '...some-GeoJSON-here...';
var geojson_format = new OpenLayers.Format.GeoJSON();
var vector_layer = new OpenLayers.Layer.Vector();
map.addLayer(vector_layer);
vector_layer.addFeatures(geojson_format.read(inputGeoJson));

您可以在GeoJSON類文檔中找到更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM