简体   繁体   English

OpenLayers 2,GeoJSON无法获取坐标才能正常工作

[英]OpenLayers 2, GeoJSON cant get coordinates to work

I'm trying to display some Polygons on a my map, I'm new in whole OpenLayers maps geojson thing so be gentle please :). 我正在尝试在我的地图上显示一些多边形,我是整个OpenLayers地图geojson的新手,请保持柔和:)。

First what work: 首先是什么工作:

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

This piece of code works, the geojson from test.php show up on server, at position where it should be. 这段代码有效,来自test.php的geojson显示在服务器上应有的位置。 But geojson I try to reach is on another server and it throw errors(I know I can set headers and it would work) but I don't want to do it this way. 但是我尝试到达的geojson在另一台服务器上并且抛出错误(我知道我可以设置标头并且可以工作),但是我不想这样做。

This dont work: 这不起作用:

var geojs_format = new OpenLayers.Format.GeoJSON();
var geojsval= {
                "type": "Polygon", 
                "coordinates": 
                    [[[11.0878902207, 45.1602390564], 
                      [14.931640625, 40.9228515625], 
                      [0.8251953125, 41.0986328125], 
                      [7.63671875, 48.96484375], 
                      [11.0878902207, 45.1602390564]]]
            };
vector = new OpenLayers.Layer.Vector("GeoJSON",{projection:"EPSG:4326"});
vector.addFeatures(geojs_format.read(geojsval));

This does not work - It display polygon on a different place even though projection is specified in same way(or atleast for me...). 这是行不通的-即使以相同的方式(或至少对我来说是指定的)指定投影,它也会在不同的位置显示多边形。

I solved this. 我解决了 When creating OpenLayers.Format.GeoJSON you can specific projection like this... 创建OpenLayers.Format.GeoJSON时,您可以像这样进行特定的投影...

var geojs_format = new OpenLayers.Format.GeoJSON({
            'internalProjection': new OpenLayers.Projection("EPSG:900913"),
            'externalProjection': new OpenLayers.Projection("EPSG:4326")
        });

This solved the problem. 这样就解决了问题。

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

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