OpenLayers GeoRSS解析 - 所有点都在彼此之上

时间:2012-01-23 11:20:18

标签: openlayers georss

我正在关注OpenGeo的OpenLayers教程,并且正在努力寻找一个矢量图层,该图层读取地震位置的GeoRSS编码XML文件 - 这些教程中似乎使用了很多。这些地图产生一个单点(在0,0处),经过仔细检查后,似乎是文件中的所有点堆叠在彼此之上,因此在XML和OpenLayers中的点的转换之间出现了明显的错误。 / p>

以下是代码:

var geographic = new OpenLayers.Projection("EPSG:4326");
var mercator = new OpenLayers.Projection("EPSG:900913");

var world = new OpenLayers.Bounds(-180, -89, 180, 89).transform(
    geographic, mercator
);

var center = new OpenLayers.LonLat('.$centerMapLat.','.$centerMapLon.').transform(
    geographic, mercator
);

var options = {
    projection: mercator,
    units: "m",
    maxExtent: world
};

var map = new OpenLayers.Map("map-id", options);

var osm = new OpenLayers.Layer.OSM();
map.addLayer(osm);
map.addControl(new OpenLayers.Control.LayerSwitcher()); 
map.setCenter(center, 2);

var mapdata = new OpenLayers.Layer.Vector("Map Data", {
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
        url: "7day-M2.5.xml",
        format: new OpenLayers.Format.GeoRSS()
    })
});
map.addLayer(mapdata);

XML文件采用以下格式:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">

<updated>2012-01-23T09:43:22Z</updated>
<title>USGS M 2.5+ Earthquakes</title>
<subtitle>Real-time, worldwide earthquake list for the past 7 days</subtitle>
<link rel="self" href="http://earthquake.usgs.gov/earthquakes/catalogs/7day-M2.5.xml"/>
<link href="http://earthquake.usgs.gov/earthquakes/"/>
<author><name>U.S. Geological Survey</name></author>

<id>http://earthquake.usgs.gov/</id>
<icon>/favicon.ico</icon>
<entry>
    <id>urn:earthquake-usgs-gov:ak:10395995</id>
    <title>M 2.7, Alaska Peninsula</title>
    <updated>2012-01-23T09:38:43Z</updated>
    <link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/ak10395995.php"/>
    <summary type="html">
    <![CDATA[<img src="http://earthquake.usgs.gov/images/globes/60_-155.jpg" alt="57.806&#176;N 156.412&#176;W" align="left" hspace="20" />
    <p>Monday, January 23, 2012 09:38:43 UTC<br>Monday, January 23, 2012 12:38:43 AM at epicenter</p>
    <p><strong>Depth</strong>: 122.70 km (76.24 mi)</p>]]></summary><georss:point>57.8058 -156.4123</georss:point>
    <georss:elev>-122700</georss:elev>
    <category label="Age" term="Past hour"/>
</entry>

[:]

</feed>

标签之间放置的值或者我剪切的字段数似乎并不重要,该点始终显示为0,0。我可以通过在firebug中手动编辑坐标来移动这个点 - 这是在每个点的html中呈现的内容:

<circle id="OpenLayers.Geometry.Point_424" 
    cx="4.738678387182473" cy="237.58907791425827" 
    r="6" fill="#ee9900" fill-opacity="0.4" stroke="#ee9900" stroke-opacity="1" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="none" pointer-events="visiblePainted" cursor="inherit">

我强烈怀疑自己做错了什么,所以非常感谢理智检查

1 个答案:

答案 0 :(得分:3)

问题在于,虽然地图和背景图层(OSM)的投影是“EPSG:900913”,但您从GeoRSS加载的点数在“EPSG:4326”中。

EPSG:900913坐标看起来像这样:20037508,200370508。在EPSG中:4326坐标范围介于-180和180之间,这就是为什么看起来所有点都在地图上的0,0左右。

解决方案是通过在创建矢量图层时指定投影来重新投影GeoRSS点:

var mapdata = new OpenLayers.Layer.Vector("Map Data", {
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
        url: "7day-M2.5.xml",
        format: new OpenLayers.Format.GeoRSS()
    }),
    projection: geographic
});