我正在学习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°N 156.412°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">我强烈怀疑我做错了什么,所以希望能进行一次理智的检查。
发布于 2012-01-23 22:57:19
问题是,虽然地图和背景层的投影是"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
});https://stackoverflow.com/questions/8970819
复制相似问题