0

私は状態を移動するときに特定のモニュメントを表示するマップを作成しようとしています。画面上の地図を移動するときに使用できるモニュメントを更新したい。 私は、データベースから入手可能なポイントを返すために、GETパラメータ付きのPHPページを使用しています。 私は現在出発地点に設定していますが、マップをドラッグした後に更新する方法を理解できません。私はそれが地図の中心に基づいて更新することを望んでいます、どこでもそれがドラグにあるかもしれません。 これはJavaScriptを搭載した地図ページです。新しい場所に基づいてLAT/LONGバリエーションとリフレッシュピンを更新するには

<!DOCTYPE html > 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>NGS Dynamic Map with Google Maps</title> 
    <style> 
     #main{ 
     height:90%; 
     } 
     /* Always set the map height explicitly to define the size of the div 
     * element that contains the map. */ 
     #map { 
     height: 100%; 
     } 
     /* Optional: Makes the sample page fill the window. */ 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
    </style> 
    </head> 

    <body> 
    <div id="main"> 
    <div id="map"></div> 
    </div> 
    <script> 



     function initMap() { 
     var map = new google.maps.Map(document.getElementById('map'), { 
      center: new google.maps.LatLng(30.27,-98.87), 
      zoom: 12 
     }); 
     var infoWindow = new google.maps.InfoWindow; 
    var NewMapCenter = map.getCenter(); 
    var vlat = NewMapCenter.lat(); 
    var vlong = NewMapCenter.lng(); 


      // Change this depending on the name of your PHP or XML file 
      downloadUrl('makePoints.php?lat=' + vlat + '&long=' + vlong, function(data) { 
      var xml = data.responseXML; 
      var markers = xml.documentElement.getElementsByTagName('marker'); 
      Array.prototype.forEach.call(markers, function(markerElem) { 
       var id = markerElem.getAttribute('id'); 
       var name = markerElem.getAttribute('name'); 
       var county = markerElem.getAttribute('county'); 
       var state = markerElem.getAttribute('state'); 
      var elevation = markerElem.getAttribute('elevation'); 
      var data_source = markerElem.getAttribute('data_source'); 

       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 = "PID# " + name 
       infowincontent.appendChild(strong); 
       infowincontent.appendChild(document.createElement('br')); 

       var text = document.createElement('text'); 
       text.textContent = county + " County" 
       infowincontent.appendChild(text); 
       infowincontent.appendChild(document.createElement('br')); 

       var text = document.createElement('text'); 
       text.textContent = elevation + " (US Survey Ft.)" 
       infowincontent.appendChild(text); 
       infowincontent.appendChild(document.createElement('br')); 


       var newlink = document.createElement('a'); 
       newlink.textContent = data_source 
       newlink.setAttribute('target', '_new'); 
       newlink.setAttribute('href', data_source); 
       infowincontent.appendChild(newlink); 
       infowincontent.appendChild(document.createElement('br')); 

       var marker = new google.maps.Marker({ 
       map: map, 
       position: point 
       }); 
       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() {} 
    </script> 
    <script async defer 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD_wB5WVi2nupVm9WzQ9C8Px_iYB1JZJv0&callback=initMap"> 
    </script> 
    </body> 
</html> 

私はちょうど私の設定downloadUrlパラメータの変数を保持し、またdownloadUrl()データを呼び出し、処理する関数を再初期化しますdragendイベントのイベントでそれらを更新する可能性が考えていました。それからマップは更新されるだろうが、これが実現する正しい方法であるかどうかはわからない。

また、downloadUrlページでは、GETによって渡されたlat/longを受け取り、dbにアクセスして、それを通過したlat/longの半径10マイル以内のすべてを取得します。 PHPはXMLを返します。 私は本当にこの仕事をする方法を理解するのに十分な.jsを知りません。いかなる考えも認められるだろう。

答えて

1

あなたのPHPファイルにajaxリクエストを繰り返すために、マップ上にdragendのイベントリスナーが必要です。そのajaxリクエストの呼び出しをそれ自身の関数で実行させます。 mapをグローバル変数にして、両方の関数でアクセスできるようにします。

このような何か:

var map; 

function initMap() { 
    map = new google.maps.Map(document.getElementById('map'), { 
     center: new google.maps.LatLng(30.27, -98.87), 
     zoom: 12 
    }); 

    getPoints(30.27, -98.87); 

    map.addListener('dragend', function() { 
     var NewMapCenter = this.getCenter(); 
     var vlat = NewMapCenter.lat(); 
     var vlong = NewMapCenter.lng(); 
     getPoints(vlat, vlong); 
    }); 
} 

function getPoints(vlat, vlong) { 
    downloadUrl('makePoints.php?lat=' + vlat + '&long=' + vlong, function(data) { 
     var xml = data.responseXML; 
     var markers = xml.documentElement.getElementsByTagName('marker'); 
     var infoWindow = new google.maps.InfoWindow(); 

     markers.forEach(function(markerElem) { 
      var id = markerElem.getAttribute('id'); 
      var name = markerElem.getAttribute('name'); 
      var county = markerElem.getAttribute('county'); 
      var state = markerElem.getAttribute('state'); 
      var elevation = markerElem.getAttribute('elevation'); 
      var data_source = markerElem.getAttribute('data_source'); 

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

      var infowincontent = // ... etc 

      var marker = new google.maps.Marker({ 
       map: map, 
       position: point 
      }); 
      marker.addListener('click', function() { 
       infoWindow.setContent(infowincontent); 
       infoWindow.open(map, marker); 
      }); 
     }); 
    }); 
} 
関連する問題