2017-02-11 7 views
-3

sqlからlatとlonを取得し、埋め込まれたGoogleマップにマーカーを配置するスクリプトを作成しました。自動的にデータを取得し、マーカーを自動的に配置するようなスクリプトを毎秒実行します。Javascript - 間隔を設定する

<script> 
    var infoWindow= null; 
    var map = null; 
    var markersArray = []; 


     function initMap() { 
      var myLatlng = new google.maps.LatLng(14.657971, 120.976970); 
      var map = new google.maps.Map(document.getElementById('map_canvas'), { 
      center: myLatlng, 
      zoom: 16, 
      streetViewControl : false, 
      mapTypeId : google.maps.MapTypeId.HYBRID, 
     }); 

     var infoWindow = new google.maps.InfoWindow; 

     updateMaps(); 

function updateMaps() { 


     // Change this depending on the name of your PHP or XML file 

     downloadUrl('phpsqlajax_genxml.php?t=', function(data) { 
      var xml = data.responseXML; 
      var markers = xml.documentElement.getElementsByTagName('marker'); 
      Array.prototype.forEach.call(markers, function(markerElem) { 
       var name = markerElem.getAttribute('name'); 
       var address = markerElem.getAttribute('address'); 
       var type = markerElem.getAttribute('type'); 

       var point = new google.maps.LatLng(
        parseFloat(markerElem.getAttribute('lat')), 
        parseFloat(markerElem.getAttribute('lng'))); 

       var infowincontent = document.createElement('div'); 
       var strong = document.createElement('strong'); 
       strong.textContent = name 
       infowincontent.appendChild(strong); 
       infowincontent.appendChild(document.createElement('br')); 

       var text = document.createElement('text'); 
       text.textContent = address 
       infowincontent.appendChild(text); 
       var icon = customLabel[type] || {}; 
       var marker = new google.maps.Marker({ 
       map: map, 
       position: point, 
       label: icon.label 

       }); 


       marker.addListener('click', function() { 
       infoWindow.setContent(infowincontent); 
       infoWindow.open(map, marker); 

       }); 
      }); 
      }); 
     } 


     function downloadUrl(url, callback) { 
     var request = window.ActiveXObject ? 
      new ActiveXObject('Microsoft.XMLHTTP') : 
      new XMLHttpRequest; 

     request.onreadystatechange = function() { 
      if (request.readyState == 4) { 
      request.onreadystatechange = doNothing; 
      callback(request, request.status); 
      } 
     }; 

     request.open('GET', url, true); 
     request.send(null); 
     } 

     function doNothing() {} 

     } 
     window.setInterval(updateMaps, 1000);  
    </script> 
+1

何が問題ですか? – Daniel

+1

ようこそスタックオーバーフロー。 [ask]を読んで実際に**質問**を送って回答してください。 –

+2

まあ明らかに 'setInterval'がそれを手助けすることができるので、何が問題なのでしょうか? – Carcigenicate

答えて

0
var update; 

function updateInterval() { 
    update = setInterval(function() { 
    updateMaps() 
    }, 1000); 
} 
updateInterval(); 

あなたは、ドキュメントの準備ができたときに()updateIntervalをロードしたり、本文の最後に<script>updateInterval();</script>を書くためにjQueryのを使用する必要があります。

+0

関数名の後ろに '()'をつけて呼び出すか、関数名を匿名関数にラップするのではなく、最初の引数として使うだけです。 – Connum

+0

ありがとう! –

関連する問題