我需要在 Google Maps API v3 上显示 GeoRSS 提要。此提要是通过以下过程创建的:
- 用户输入关键字
- 感谢 PHP 代码,通过从 3 个不同的现有 RSS 提要中获取包含关键字的新闻项目来创建 RSS 文件。
- 此 RSS 文件的链接提供给 Metacarta RSS 地理标记服务 ( http://labs.metacarta.com/rss-geotagger/ )
- 获得的 GeoRSS 文件也必须转换为 KML 文件(如果我将 GeoRSS 的链接作为 KmlLayer 函数的参数,Google 地图只会放大到海洋)。为此,我使用 geonames.org 的 GeoRSS 到 KML 转换器
- 然后将生成的 URL 作为 KmlLayer 函数的参数传递
在我第一次尝试时,一切都很好。但是当我输入一个新的关键字时,谷歌地图会显示我尝试使用前一个关键字的标记。新的 KML 图层显示花了将近 15 分钟(当然,在单击我的 Web 浏览器的刷新按钮之后)。有没有办法解决这个问题,以便在输入新关键字后立即在地图上显示修改后的 GeoRSS 提要?
由于新用户的超链接数量限制,我将在此处输入源代码。您可以从这里访问文件:http ://denizseeu.comule.com/ 我正在使用的文件是:home.html、my_rss.php、rss.xml 和 map.html
home.html - 用于输入和确认关键字的第一页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home</title>
<style type="text/css">
#keyword
{
color:grey;
font: italic 12pt Arial;
}
</style>
</head>
<body>
<form name ="form1" method ="POST" action = "my_rss.php">
<input type="Text" onblur="if(this.value=='') { this.value='Enter keyword'; this.style.color='grey'; this.style.font='italic 12pt Arial'; }" onfocus="if(this.value=='Enter keyword') { this.value=''; this.style.color='#111111'; this.style.font='normal 12pt Arial'; }" value="Enter keyword" name="keyword" id="keyword">
<input type="Submit" name="Submit1" value= "Show news on map" />
</form>
</body>
</html>
my_rss.php - 创建 RSS 文件的 PHP 文件
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0;URL=map.html">
</head>
<body>
<?php
$keyword = $_POST['keyword'];
$URL = array("http://www.nytimes.com/services/xml/rss/nyt/GlobalHome.xml","http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk","http://rss.cnn.com/rss/edition.rss");
$output = "
<rss version=\"2.0\">
<channel>
<title>RSS collection</title>
";
for($y=0;$y<count($URL);$y++)
{
$rss[$y] = simplexml_load_file($URL[$y]);
foreach ($rss[$y]->channel->item as $item)
{
if(preg_match("/".$keyword."/",$item->title,$result))
{
$output .= "
<item>
<title>". $item->title ."</title>
<description>". $item->description ."</description>
<link>";
$link=$item->link;
$link = str_replace('&', '&', $link);
$output .= $link ."</link>
</item>
";
}
}
}
$output .= "</channel></rss>";
header ('Content-type: text/html; charset=utf-8');
echo $output;
$xml = "rss.xml";
$file = fopen($xml, 'w') or die("can't open file");
fwrite($file, $output);
fclose($file);
?>
</body>
</html>
map.html - 带有 Google Maps API 的网页
<!DOCTYPE html>
<html>
<head>
<title>Welcome to my GeoRSS map</title>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<style type="text/css">
html, body, #map_canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var initial_point = new google.maps.LatLng(42.02,20.97);
function initialize() {
var myOptions = {
zoom: 2,
center: initial_point,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
var geoRSS = new google.maps.KmlLayer('http://ws.geonames.org/rssToGeoRSS?type=kml&feedUrl=http%3A%2F%2Flabs.metacarta.com%2Frss-geotagger%2Ftag%2F%3Furl%3Dhttp%253A%252F%252Fdenizseeu.comule.com%252Frss.xml');
geoRSS.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>