2017-02-28 72 views
-1

我是新使用谷歌地圖和新的錯綜複雜的JavaScript。考慮到這一點,我正在嘗試使用USGS提供的數據源創建網絡地圖。此Feed每5分鐘更新一次。我希望使用這個相同的feed(這是一個geojson文件)每5分鐘刷新我的地圖。困惑關於地圖更新

我的最終目標是在我的地圖上顯示/更新至少一個其他Feed。在過去的四天裏,我經歷了數十個帖子,並且處於超負荷和困惑的境地。有人請清理我的霧嗎?

我發佈的代碼是99%不是我的代碼,大部分是我添加了評論,所以我可以找出代碼中發生了什麼。

<HTML> 
    <HEAD> 
    <TITLE>TEST OF MAP</TITLE> 
    <STYLE> 
/* --------------------------------------------------- */ 
/* Set the map height explicitly to define the size of */ 
/* the DIV * element that contains the map.   */ 
/* ----------------------------------------------------*/ 
     #map { 
     height: 75%; 
     border: 5px solid green; 
     } 
    </STYLE> 
    </HEAD> 

<SCRIPT type="text/javascript">  

// -------------------------------------- 
// Set a refresh interval in milliseconds 
// -------------------------------------- 
    setInterval(page_refresh, 1*60000); 
    google.maps.event.addDomListener(window, 'load', initialize); 

</SCRIPT> 

    <BODY> 
    <H1><CENTER>MAP Demo</CENTER></H1> 
    <DIV id="map"></DIV> 

    <SCRIPT> 
     var map; 
     function initMap() { 
     map = new google.maps.Map(document.getElementById('map'), { 
      zoom: 2, 
      center: new google.maps.LatLng(35.4437,139.6380), 
      mapTypeId: 'terrain' 
     }); 

// --------------------------------------------------------- 
// Create a <SCRIPT> tag and set the USGS URL as the source. 
// --------------------------------------------------------- 
     var script = document.createElement('script'); 

// ----------------------------------------------------------------------- 
// Using a local copy of the GeoJSON stored on the USGS server 
// 

http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.geojsonp 
// ----------------------------------------------------------------------- 
     script.src = 

'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.geojson 

p'; 
     document.getElementsByTagName('head')[0].appendChild(script); 
     } 

// ------------------------------------------------------------------ 
// Loop through the results array and place a marker for each set of 
// coordinates. 
// ------------------------------------------------------------------ 
     window.eqfeed_callback = function(results) { 
     for (var i = 0; i < results.features.length; i++) { 
      var coords = results.features[i].geometry.coordinates; 
      var latLng = new google.maps.LatLng(coords[1],coords[0]); 
      var marker = new google.maps.Marker({ 
      position: latLng, 
      map: map 
      }); 
     } 
     } 
    </SCRIPT> 

    <SCRIPT async defer src="https://maps.googleapis.com/maps/api/js? 

key=MY_MAP_KEY&callback=initMap"> 
    </SCRIPT> 
    </BODY> 
</HTML> 
+2

你的問題是什麼?發佈的代碼有什麼問題? – geocodezip

+0

對不起,我以爲我的問題很清楚。它是,但只在*我的頭上。我很抱歉。看來setInterval不起作用,我不知道爲什麼。我確認的唯一方法是在設定的刷新時間限制過後很長時間手動刷新頁面。我能做些什麼來獲得頁面刷新?感謝任何/所有的幫助。 –

回答

0

雖然不是很優雅,但我用了下面的刷新。由於我只是在構建「概念驗證」,因此刷新整個頁面並不是問題。

function timedRefresh(timeoutPeriod) { 
    setTimeout("location.reload(true);",timeoutPeriod); 
    } 
    window.onload = timedRefresh(60*5000);