创建Google Maps Circle并在React中设置其半径

Sha*_*hah 1 google-maps reactjs react-google-maps

鉴于以下数据:

[
        {
            "id": 1,
            "name": "Park Slope",
            "latitude": "40.6710729",
            "longitude": "-73.9988001"
        },
        {
            "id": 2,
            "name": "Bushwick",
            "latitude": "40.6942861",
            "longitude": "-73.9389312"
        },
        {
            "id": 3,
            "name": "East New York",
            "latitude": "40.6577799",
            "longitude": "-73.9147716"
        }
    ]
]
Run Code Online (Sandbox Code Playgroud)

我正在通过react-google-maps创建标记,如下所示:

<Map
    center={{ lat: 40.64, lng: -73.96 }}
    zoom={12}
    places={data}
    googleMapURL="https://maps.googleapis.com/maps/api/js?key="
    loadingElement={<div style={{ height: `100%` }} />}
    containerElement={<div style={{ height: `800px` }} />}
    mapElement={<div style={{ height: `100%` }} />}
  />
Run Code Online (Sandbox Code Playgroud)

哪里

class Map extends React.Component {
  render() {
    return (
      <GoogleMap
        defaultZoom={this.props.zoom}
        defaultCenter={this.props.center}
      >
        {this.props.places.map(place => {
          return (
            <Marker
              position={{
                lat: parseFloat(place.latitude),
                lng: parseFloat(place.longitude)
              }}
            />
          );
        })}
      </GoogleMap>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,我想与标记一起绘制一个圆,但仅针对特定标记,例如第一个:

{
    "id": 1,
    "name": "Park Slope",
    "latitude": "40.6710729",
    "longitude": "-73.9988001"
}, 
Run Code Online (Sandbox Code Playgroud)

如下所示:

在此处输入图片说明

是否支持通过react-google-maps渲染和画圆以及指定半径等属性?

Vad*_*hev 5

由于标记属性是通过placesprop 传递的,因此要考虑的一种选择是将圆属性与位置数据一起传递,例如:

const places = [
  {
    id: 1,
    name: "Park Slope",
    latitude: "40.6710729",
    longitude: "-73.9988001",
    circle: {
      radius: 3000,
      options: {
        strokeColor: "#ff0000"
      }
    }
  },   
  ...
] 
Run Code Online (Sandbox Code Playgroud)

然后Map可以像这样修改标记和圆形组件:

class Map extends React.Component {
  render() {
    return (
      <GoogleMap
        defaultZoom={this.props.zoom}
        defaultCenter={this.props.center}
      >
        {this.props.places.map(place => {
          return (
            <Fragment key={place.id}>
              <Marker
                position={{
                  lat: parseFloat(place.latitude),
                  lng: parseFloat(place.longitude)
                }}
              />
              {place.circle && (
                <Circle
                  defaultCenter={{
                    lat: parseFloat(place.latitude),
                    lng: parseFloat(place.longitude)
                  }}
                  radius={place.circle.radius}
                  options={place.circle.options}
                />
              )}
            </Fragment>
          );
        })}
      </GoogleMap>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

演示版