Google Maps API v3花费很长时间才能显示GeoRSS Feed中的更改 [英] Google Maps API v3 takes too long to show changes in the GeoRSS feed

查看:94
本文介绍了Google Maps API v3花费很长时间才能显示GeoRSS Feed中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Google Maps API v3上显示GeoRSS资讯提供。此订阅源是通过以下过程创建的:


  1. 用户输入关键字

  2. 感谢一个PHP代码,一个RSS文件是通过从3个不同的现有RSS提要中获取包含关键字的新闻项而创建的。

  3. 这个RSS文件的链接提供给Metacarta RSS Geotagger服务(< a href =http://labs.metacarta.com/rss-geotagger/ =nofollow> http://labs.metacarta.com/rss-geotagger/ )

  4. 获取的GeoRSS文件也必须转换为KML文件(如果我将链接指向GeoRSS作为KmlLayer函数的参数,Google Maps将会放大到海洋)。为此,我使用GeoRSS来转换geonames.org的KML转换器。

  5. 然后将得到的URL作为KmlLayer函数的参数传递

  6. ol>

    在我第一次尝试时,一切正常。但是,当我输入新的关键字时,Google地图会显示我以前的关键字尝试过的标记。花了将近15分钟才显示新的KML图层(当然,点击我的网页浏览器的刷新按钮后)。有没有办法解决这个问题,以便修改后的GeoRSS feed会在输入新关键字后立即显示在地图上?



    由于新用户的超链接数量限制,我将在这里输入源代码。您可以从这里访问这些文件: http://denizseeu.comule.com/
    文件我使用的是:home.html,my_rss.php,rss.xml和map.html


    $ b 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>主页< / title>
    < style type =text / css>
    #keyword
    {
    color:grey;
    字体:italic 12pt Arial;
    }
    < / style>
    < / head>
    < body>
    < form name =form1method =POSTaction =my_rss.php>
    < input type =Textonblur =if(this.value ==''){this.value ='Enter keyword'; this.style.color ='gray'; this.style.font ='italic 12pt Arial';}onfocus =if(this.value =='Enter keyword'){this.value =''; this.style.color ='#111111'; this.style.font ='正常12pt Arial';}value =输入关键字name =keywordid =keyword>
    < input type =Submitname =Submit1value =在地图上显示新闻/>
    < / form>
    < / body>
    < / html>

    my_rss.php - 创建RSS文件的PHP文件

     < html> 
    < head>
    < title>重定向...< / title>
    < meta http-equiv =refreshcontent =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版= UK, http://rss.cnn.com/rss/edition.rss)?;
    $ output =
    < rss version = \2.0 \>
    <频道>
    <标题> RSS收藏< / title>
    ;
    for $($ y = 0; $ y< count($ URL); $ y ++)
    {
    $ rss [$ y] = simplexml_load_file($ URL [$ y]);

    foreach($ rss [$ y] - >频道 - >项目为$ item)
    {
    if(preg_match(/\".$ keyword。/ ,$ item-> title,$ result))
    {
    $ output。=
    < item>
    < title>。 $ item-> title。< / title>
    < description>。 $ item-> description。< / description>
    < link>;
    $ link = $ item->链接;
    $ 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')或死(无法打开文件);
    fwrite($ file,$ output);
    fclose($ file);
    ?>
    < / body>
    < / html>

    map.html - Google Maps API的网页



     <!DOCTYPE html> 
    < html>
    < head>
    < title>欢迎来到我的GeoRSS地图< / 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;
    身高: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);
    函数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>


    解决方案

    KmlLayer缓存KML,并重新加载显然相同的内容文件不会导致Google重新加载KML并刷新其图块。



    添加一个随机虚拟参数(可能基于时间,或 Math.random )添加到您的KML网址。这将确保Google每次都会获得新的网址,并且是在地图中获取正确数据的最佳机会。



    注意:Google的KML处理是一个黑盒子。它可能不会被一个虚拟参数愚弄。


    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>
    

    解决方案

    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.

    这篇关于Google Maps API v3花费很长时间才能显示GeoRSS Feed中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆