【问题标题】:Google Maps React Polygon Issues谷歌地图反应多边形问题
【发布时间】:2020-12-26 13:52:46
【问题描述】:

我正在创建一个应用程序,它使用来自 API 的野火数据并翻转它,以便使用 google-maps-react 包在 Google 地图上显示多边形。我已经弄清楚了一切,直到使用地图组件中内置的函数返回和显示多边形。有人想插话可能是什么问题吗?我真的很感激一些帮助。谢谢。

import React, { Component } from 'react';
import { Map, GoogleApiWrapper, Polygon } from 'google-maps-react';



const mapStyles = {
    margin: 30,
    width: '93.75%',
    height: '90%',
    border: '1px solid #3E1C18',
    display: 'inline-block'
};


class FireMap extends Component {

    constructor(props) {
        super(props)

        this.state = {
            fires: [],
            polygons: []
        }
    }
    

    componentDidMount() {
        fetch('https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Public_Wildfire_Perimeters_View/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json')
            .then(res => res.json())
            .then(data => {
                this.setState({ fires: data.features })
                this.state.fires.map((fire) =>{
                    if (fire.geometry !== null){
                        let fireCoords = fire.geometry.rings
                        let trueCoords = []
                        fireCoords.map((coords) => {
                            coords.map((pair) => {
                                let newPair = {lat: pair[1], lng: pair[0]}
                                return trueCoords.push(newPair)
                            })
                        })
                        this.state.polygons.push(trueCoords);
                        console.log(this.state.polygons)
                     }
                 })
            })
    }

    showPolygons = () => {
        this.state.polygons.map((polygon) => {
            let firePoly=  <Polygon paths={polygon} options={{
                fillColor: "#BF5E4B",
                fillOpacity: 0.45,
                strokeColor: "#6B352A",
                strokeOpacity: 0.9,
                strokeWeight: 1
            }}/>
            return firePoly
        })
    }
    
    render(){
        return(
            <div className="mapBox">
                <Map
                    google={this.props.google}
                    zoom={8}
                    style={mapStyles}
                    initialCenter={{ lat: 37.7749, lng: -122.4149 }}
                >
                    {this.showPolygons()}
                </Map>
            </div>
        );
    }
}

【问题讨论】:

  • 如果将this.state.fires.map(...) 替换为some_static_fires_data.map(...),它会呈现多边形吗? new Polygon 而不是 &lt;Polygon&gt; 真的有效吗?同样从文档中我得到的印象是它应该是&lt;Polygon fillColor: ...&gt; 而不是&lt;Polygon options={{ fillColor: ... }}&gt;
  • @x00 我尝试使用尖括号,但它不起作用。我更改了代码以使用控制台来确认我的多边形正在接受路径属性的真实坐标并且正在创建多边形值,但它似乎没有被返回。我被困住了

标签: reactjs google-maps google-maps-api-3 geojson google-maps-react


【解决方案1】:

如果您尝试了我建议的操作,我还没有从您的评论中发现...但是您编辑了您的帖子,并进行了一些随机(且不正确)的更改。好的,我会尝试发布答案。这是一个与原始代码相关的答案,因为它看起来更简单并且包含的​​错误更少。

我认为它应该是这样的:

const coord_pair_to_latlng =  ([lat,lng]) => ({ lat, lng })
const convert_ring_coords = ring => ring.map(coord_pair_to_latlng)

class FireMap extends Component {
  constructor(props) {
    super(props)
    this.state = { fires: [] }
  }
  componentDidMount() {
    fetch('https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Public_Wildfire_Perimeters_View/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json')
      .then(res => res.json())
      .then(data => this.setState({ fires: data.features }))
  }
  displayFires = () => this.state.fires
    .filter(fire => fire.geometry !== null)
    .map(fire => fire.geometry.rings)
    .map(rings => <Polygon 
      paths = { rings.reduce((acc, ring) => acc.concat(convert_ring_coords(ring)), []) }
      fillColor     = "#BF5E4B"
      fillOpacity   = {0.45}
      strokeColor   = "#6B352A"
      strokeOpacity = {0.9}
      strokeWeight  = {1}
    />)
  render() {
    return (
      <div className="mapBox">
        <Map
          google        = {this.props.google}
          zoom          = {8}
          style         = {mapStyles}
          initialCenter = {{ lat: 37.7749, lng: -122.4149 }}
        >
          {this.displayFires()}
        </Map>
      </div>
    )
  }
}

除了更实用的代码风格(你可以忽略),基本上我改变的只是:

  1. new Polygon()&lt;Polygon&gt; 不是一回事。你应该返回后者。 JSX 转换为 React.createElement(Polygon,...) 而不是 new Polygon(...)。好的,你已经解决了。

  2. 根据docssource codePolygon 应创建为

    <Polygon
      paths         = {coords}
      fillColor     = "#BF5E4B"
      fillOpacity   = {0.45}
      strokeColor   = "#6B352A"
      strokeOpacity = {0.9}
      strokeWeight  = {1}
    />
    

    而不是像

    <Polygon
      paths         = {coords}
      options       = {{...}}
    />
    

    您的 options 将被忽略

  3. componentDidMount 中的this.displayFires() 不执行任何操作,因此应将其删除。

    附带说明:在this.displayFires() 调用this.state is not changed yet 时也是如此。但它不应该改变结果,因为正如我所说,this.displayFires()componentDidMount 中没有效果......但是你的新版本代码中的this.state.polygons.push 可以产生效果......或者我最好说将引入错误。你永远不应该那样做。

【讨论】:

  • 非常感谢!当我尝试使用提供的代码导出此组件时仍然出现错误,看起来缺少一些标点符号。编辑:您忘记了多边形的结束“/”。现在就试试这个。
  • 这个答案有效!我最初使用的 API 不起作用,我忽略了这是更改代码和修补它的原因,但是哈利路亚!多边形显示完美。非常感谢你 x00,将尝试更多的悬停和部署。
猜你喜欢
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 2012-06-15
  • 1970-01-01
  • 2020-03-17
  • 2013-02-03
相关资源
最近更新 更多