2

我正在关注来自 OpenGeo 的 OpenLayers 教程,并且正在努力使用一个矢量图层来读取地震位置的 GeoRSS 编码 XML 文件——这似乎在这类教程中被大量使用。地图生成一个点(在 0,0 处),仔细检查后,该点似乎是文件中的所有点堆叠在一起,因此很明显,在 XML 和 OpenLayers 中的点转换之间出现了问题。

这是代码:

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">

我强烈怀疑我做错了什么,所以希望能进行一次完整性检查

4

1 回答 1

3

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

EPSG:900913 坐标如下所示:20037508, 20037508。在 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
});
于 2012-01-23T14:57:19.010 回答