简体   繁体   English

如何通过 Angular 中的弹出窗口显示多边形坐标?

[英]How can I show the polygon coordinates by popup in Angular?

here is the function of drawing polygon in leaflet map.这是 leaflet map 中绘制多边形的 function。

onDrawCreated(e: any) {
        const { layerType, layer } = e;
        if (layerType === "polygon") {
            const polygonCoordinates = layer._latlngs;
            console.log(polygonCoordinates);


        }
        this.drawnItems.addLayer(e.layer);
    }
    

And here the polygonCoordinates;这里是多边形坐标;

 Array(1)
    0
    : 
    Array(4)
    0
    : 
    LatLng {lat: 54.23955053156179, lng: -112.10449218750001}
    1
    : 
    LatLng {lat: 50.064191736659104, lng: -108.4130859375}
    2
    : 
    LatLng {lat: 48.19538740833338, lng: -116.80664062500001}
    3
    : 
    LatLng {lat: 52.07950600379697, lng: -115.66406250000001}
    length
    : 
    4
    [[Prototype]]
    : 
    Array(0)
    length
    : 
    1
    [[Prototype]]
    : 
    Array(0)

How can I show this coordinates on function with popup?如何通过弹出窗口在 function 上显示此坐标?

Thank you in advance for your help!预先感谢您的帮助!

There are many ways to do that.有很多方法可以做到这一点。 One way is to build an array that will contain all the lat lngs and then join them inside the popup in order to visualize them.一种方法是构建一个包含所有lat lngs的数组,然后将它们加入弹出窗口中以可视化它们。 Then using polygon's bindPopup and openPopup methods you can display them.然后使用多边形的bindPopupopenPopup方法可以显示它们。

onDrawCreated(e: any) {
    const { layerType, layer } = e;
    if (layerType === "polygon") {
      const polygonCoordinates = layer._latlngs;
      console.log(polygonCoordinates);

      const allCoordsInOneArray = [];
      polygonCoordinates[0].forEach((item) => {
        allCoordsInOneArray.push(item.lat.toFixed(2));
        allCoordsInOneArray.push(item.lng.toFixed(2));
      });
      console.log(allCoordsInOneArray);
      layer
        .bindPopup(`Polygon coordinates are ${allCoordsInOneArray.join(", ")}`)
        .openPopup();
    }
    this.drawnItems.addLayer(e.layer);
  }

Demo 演示

Normally the popup should auto trigger once you form the polygon.通常,一旦形成多边形,弹出窗口就会自动触发。 If it does not simply click the polygon and you will get it open.如果它不简单地单击多边形,您将打开它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM