簡體   English   中英

如何從我的多邊形返回/獲取坐標?

[英]How to return/get coordinates from my Polygon?

我正在使用google-maps-react在地圖上渲染多邊形。

當我單擊一個多邊形時,我希望它返回該選定多邊形的所有坐標。 你能告訴我這是否可能嗎?

這是我放在 codeandbox 中的代碼: https ://codesandbox.io/s/falling-tree-ikvq1 ? file =/ src/index.js

這是我繪制地圖的地方:

 <Map google={this.props.google} className={"map"} zoom={4} initialCenter={{ lat: 20.0, lng: -70.0 }} > <Polygon onClick={this.handleClick} paths={triangleCoords} strokeOpacity={0.8} strokeWeight={2} fillColor={this.fillColors[0]} fillOpacity={0.35} /> </Map>

提前致謝。

看起來您正在尋找的是來自 Polygon 類的getPath方法https://developers.google.com/maps/documentation/javascript/reference/polygon

在您的沙箱中,我能夠將路徑作為長度為 4 的數組的對象獲取:

handleClick = (props, polygon, e) => {
  console.log("polygon: ", polygon.getPath());
  const currentColor =
  polygon.fillColor == this.fillColors[0]
    ? this.fillColors[1]
    : this.fillColors[0];
   polygon.setOptions({ fillColor: currentColor });

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM