简体   繁体   English

如何将弹出窗口绑定到作为来自后端 API 的 GET 请求响应进入的 FeatureCollection 的每个元素?

[英]How do I bind a pop-up to each element of a FeatureCollection coming in as a GET request response from back-end API?

Answer from back end comes back like this:后端的回答是这样的: 在此处输入图像描述

POSTMAN answer to GET REQUEST POSTMAN 回答 GET REQUEST 在此处输入图像描述

I have a service method that sends the GET request to the back-end API.我有一个向后端API发送GET请求的服务方法。

export class PowerPlantService {
  constructor(private http: HttpClient) { }

  getAll() {
    return this.http.get(baseUrl);
  }

In the component, I create a service variable and call that GET method and subscribe to the result (data).在组件中,我创建了一个服务变量并调用该 GET 方法并订阅了结果(数据)。 Then I add the data (which is a feature collection as seen in the picture above) to the map. Final line of code below.然后我将数据(如上图所示是一个特征集合)添加到 map。下面的最后一行代码。

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
 //coordinates for Brasov
  private latitude: number = 45.6427;
  private longitude: number = 25.5887;
  private tiles?: any;
  private geoJsonFeature?: any;
   
  private map!: L.Map;
  private centroid: L.LatLngExpression = [this.latitude, this.longitude];

  ngOnInit(): void {
    this.initMap();
  }
  
  constructor(private powerPlantService: PowerPlantService) { 
  }

  private initMap(): void {
    this.map = L.map('map', {
      center: this.centroid,
      zoom: 2.8
    });

    this.tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 
    {
      minZoom: 2.8,
      attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
    });

    this.tiles.addTo(this.map);
    
    this.powerPlantService.getAll().subscribe((data: any)=>{
      console.log(data);
      this.geoJsonFeature = data;
      
      L.geoJSON(data).addTo(this.map) 
    }) 

This is how the pins are displayed on the map, this is just an example for reference. map 上的管脚是这样显示的,仅供参考。

在此处输入图像描述

I have tried to find a way to bind a pop-up activated on click for each pin of the feature collection which displays the properties of each Feature from the FeatureCollection but I can't find a tutorial anywhere.我试图找到一种方法来绑定在单击功能集合的每个引脚时激活的弹出窗口,该弹出窗口显示 FeatureCollection 中每个功能的属性,但我无法在任何地方找到教程。

After trial and error, I have found a way:经过反复试验,我找到了一种方法:

  1. In the initMap() method I created myOnEachFeatureMethod which takes as argument the feature and your tileLayer.在 initMap() 方法中,我创建了 myOnEachFeatureMethod,它将特征和您的 tileLayer 作为参数。
  2. In the body of the myOnEachFeatureMethod I binded a pop-up to the layer and in the.bindPopup method I used the feature.properties to access each feature in the FeatureCollection and.在 myOnEachFeatureMethod 的主体中,我将弹出窗口绑定到图层,在 .bindPopup 方法中,我使用 feature.properties 访问 FeatureCollection 中的每个功能,并且。 DESIREDpROPERTY to display that feature's property. DESIREDpPROPERTY以显示该功能的属性。 Ex:"Name: " + feature.properties.name例如:“名称:”+ feature.properties.name
  3. In the subscribe method of the getAll() API request, when I add the FeatureCollection to the map, after the FeatureCollection argument (data) I add another argument between {}, the onEachFeature method of GeoJson and my implementation of this method, myOnEachFeatureMethod).在 getAll() API 请求的订阅方法中,当我将 FeatureCollection 添加到 map 时,在 FeatureCollection 参数(数据)之后,我在 {} 之间添加了另一个参数,GeoJson 的 onEachFeature 方法和我对该方法的实现,myOnEachFeatureMethod) .
  4. Enjoy the pop-ups.享受弹出窗口。 :) :)

Here are some details about the onEachFeature but it's not very relevant to be honest, the documentation is rather disappointing if you are a beginner. 这里有一些关于 onEachFeature 的细节,但老实说它不是很相关,如果你是初学者,文档会相当令人失望。

 this.powerPlantService.getAll().subscribe((data: any)=>{
      console.log(data);
      this.geoJsonFeature = data;
      L.geoJSON(data, {onEachFeature: myOnEachFeatureMethod}).addTo(this.map)

    }) 

    function myOnEachFeatureMethod(feature: any, layer:any)
    {
        layer.bindPopup(***the feature attributes you want to display go here***    );
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在以POST(而不是GET)发送请求后,如何将对象作为html页面插入到来自后端的html元素中? - How to insert object as html page into html element which is coming from back-end after sending request as POST (and not GET)? 如何阻止来自iframe的弹出窗口? - How to block pop-up coming from iframe? 如何从反应前端向节点后端发出发布请求? 获得 404 - How do I make a post request from a react front-end to a node back-end? Getting 404 如何从通过 Fetch API 发送的后端 (localhost) 获取数据? (PHP、Fetch、React Native、POST 请求) - How do I fetch data from my back-end (localhost) that's being sent through Fetch API? (PHP, Fetch, React Native, POST request) 如果由于用户不存在而导致对API的后端GET请求失败,如何显示404页面? 前端和后端分离 - How to display 404 page if a back-end GET request to an API fails because user doesn't exists? Separated front-end and back-end 我如何在反应中显示后端响应? - How can i Show Back-End response in react? 我想从 vuejs 的后端获取用户名 - i want to get username from back-end in vuejs 如何获取用户输入的访问权限并在我的后端服务器上计算某些内容? - How do I get the access of user's input and calculate something on my back-end server? 如何在后端捕获 POST 请求? - How to catch POST request on back-end? 如何在 Javascript 中打开本地主机弹出窗口? - How do I open a localhost pop-up in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM