繁体   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