简体   繁体   English

打开第2层,从Ajax响应创建要素并将其添加到矢量层

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

as title suggest, create Feature and add it to already created vector layer. 如标题所示,创建要素并将其添加到已创建的矢量层。 I am fetching GeoJSON from server and trying to somehow add to vector layer but I can't get it to work... So basically I am asking how to get Feature element from my GeoJSON, so I can add it to vector layer. 我正在从服务器获取GeoJSON并试图以某种方式添加到矢量层,但是我无法使其正常工作。所以基本上我在问如何从我的GeoJSON中获取Feature元素,因此我可以将其添加到矢量层。 What I currently have.. 我现在有什么..

This is my GeoJSON fetched from server : 这是我从服务器获取的GeoJSON:

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

next I have addVector function in JavaScript where I'm trying to the magic.(variable GeoJS is GeoJSON fetched from server) 接下来,我在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]);
}

and yep I know that second line where I creating feature is wrong, but i cant make it right so i guess id doesn't matter what I write there for now... I tried it with var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-70.702451, 42.374473); and it worked position on the map was not where I want it to be but I know that i have to do something with projection... It just doesn't matter now. 是的,我知道我在其中创建功能的第二行是错误的,但是我无法正确设置,因此我想id无关紧要我现在写的内容...我尝试使用var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-70.702451, 42.374473);并且它在地图上的工作位置不是我想要的位置,但是我知道我必须对投影进行某些操作...现在就不重要了。

and btw I have this 顺便说一句我有这个

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

And this works, position is where I want it, its perfect except it only works when I make request on my domain, and server I try to reach is on another(I know I can set headers and it would work) but I don't want to do it this way. 这是可行的,位置是我想要的位置,它的完美之处在于它仅在我对域发出请求时才有效,而我尝试访问的服务器在另一个服务器上(我知道我可以设置标头并且可以使用),但是我不这样做不想这样做。

So basically I am asking how to get Feature from my GeoJSON. 所以基本上我在问如何从我的GeoJSON获取功能。 I am really new to OpenLayers so I'm sorry if I asking something obvious. 我对OpenLayers真的很陌生,因此如果我提出明显的要求,对不起。

To use a simplified version of the official example : 要使用官方示例的简化版本:

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

You can find more details in the GeoJSON class documentation . 您可以在GeoJSON类文档中找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM