dopuzf0898 2019-08-05 17:17
浏览 685

Leaflet从geojson获取id并将其绑定到popup

var map = L.mapbox.map('map', undefined, options);
    map.setView([56.413300, 8.869450], 16)
    var markerpositions;
    map.addLayer(L.mapbox.styleLayer('mapbox://styles/mapbox/satellite-streets-v9'));
   // L.Control.geocoder().addTo(map);
   var geocoder = L.control.geocoder('search-MKZrG6M').addTo(map);
    L.marker([56.41083875205333, 8.864636421203615], {icon: HydrantIcon}).addTo(map).bindPopup("Hydrant  1");
    L.marker([56.41069629842804, 8.868584632873537], {icon: HydrantIcon}).addTo(map).bindPopup("Hydrant  2");
    L.marker([56.40941419180167, 8.866181373596193], {icon: HydrantIcon}).addTo(map).bindPopup("Hydrant  3");
    var data = <?php echo JSON_encode($geojson); ?>;
    var geoJsonLayer = L.geoJson().addTo(map);
function requestGeoJson(){
    fetch('https://laerkeholt.dk/admin/get-features')
      .then(function(response){ return response.json() })
      .then(function(json){
         geoJsonLayer.clearLayers().addData(json).bindPopup('ID: + IDHERE');
         var http = new XMLHttpRequest();
         var url = "https://maker.ifttt.com/trigger/vandingsmaskine/with/key/";
          http.open("POST", url, true);
          http.send(JSON.stringify({"value1": "ID 1"}));
          var hydrat1 = [56.41083875205333, 8.864636421203615];
          var hydrat2 = [56.41069629842804, 8.868584632873537];
          var hydrat3 = [56.40941419180167, 8.866181373596193];
          var test = L.latLng(json).distanceTo(hydrat1);
          <?php echo $test ?>
    
         setTimeout(requestGeoJson, 5000);
      });
};
requestGeoJson();

        

So basically I want to have it so geoJsonLayer binds a popup with the id it got for the response JSON so each marker would be assigned its specific id from the response JSON. I have tried todo json.properties.id to try and get the ID

</div>
  • 写回答

1条回答 默认 最新

  • dongyi0114 2019-08-06 14:09
    关注

    You can use either pointToLayer or onEachFeature for this.

    When you instantiate the geoJSON layer, use this:

    var geoJsonLayer = L.geoJson(false, {onEachFeature: addMyPopup}).addTo(map);

    // Initialise the three reference points
    var hydrat1 = L.latLng(<..>, <..>);
    var hydrat2 = L.latLng(<..>, <..>);
    var hydrat3 = L.latLng(<..>, <..>);
    
    // This gets called for every point in the GeoJSON
    function addMyPopup ( feature, layer) {
    if ((layer.getLatLng().distanceTo(hydrat1) < 5)
      ||(layer.getLatLng().distanceTo(hydrat2) < 5)
      ||(layer.getLatLng().distanceTo(hydrat3) < 5)) {
        // Do something
      }
      layer.bindPopup("My ID is: " + feature.properties.id);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序