Google Maps API v3 takes too long to show changes

2019-08-01 06:15发布

问题:

I need to display a GeoRSS feed on Google Maps API v3. This feed is created through the following procedures:

  1. The user types in a keyword
  2. Thanks to a PHP code, an RSS file is created by taking news items containing the keyword from 3 different existing RSS feeds.
  3. The link of this RSS file is given to the Metacarta RSS Geotagger service ( http://labs.metacarta.com/rss-geotagger/ )
  4. The obtained GeoRSS file must be also converted into a KML file (if I give the link to the GeoRSS as an argument for the KmlLayer function, Google Maps will just zoom into the ocean). For that purpose I'm using the GeoRSS to KML convertor of geonames.org
  5. The resulting URL is then passed as an argument for the KmlLayer function

At my first try, it all worked fine. But when I entered a new keyword, Google Maps showed the markers from my try with the previous keyword. It took nearly 15 minutes until the new KML Layer was displayed (of course, after clicking the Refresh button of my web browser). Is there a way to solve this problem so that the modified GeoRSS feed will be shown on map immediately after typing in the new keyword?

Due to hyperlink number restrictions for new user, i will type the sourcecodes here. You can access the files from here: http://denizseeu.comule.com/ The files I'm using are: home.html, my_rss.php, rss.xml and map.html

home.html - The first page used for typing and confirming the keyword

<!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 - PHP file that creates the RSS file

<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('&', '&amp;', $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 - The web page with the 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>

回答1:

KmlLayer caches KML, and reloading what is apparently the same file doesn't cause Google to reload the KML and refresh its layer tiles.

Add a random dummy parameter (perhaps based on the time, or Math.random) to your KML url. That will ensure that Google gets a fresh url each time and is the best chance of getting the right data in your map.

Note: Google's KML handling is a black box. It may not be fooled by a dummy parameter.