Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 创建Google Maps圆形并在React中设置其半径_Reactjs_Google Maps_React Google Maps - Fatal编程技术网

Reactjs 创建Google Maps圆形并在React中设置其半径

Reactjs 创建Google Maps圆形并在React中设置其半径,reactjs,google-maps,react-google-maps,Reactjs,Google Maps,React Google Maps,鉴于以下数据: [ { "id": 1, "name": "Park Slope", "latitude": "40.6710729", "longitude": "-73.9988001" }, { "id": 2, "name": "Bushwick", "latitude":

鉴于以下数据:

[
        {
            "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"
        }
    ]
]
我通过以下方式创建标记:

<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%` }} />}
  />
如下所示:


除了指定诸如半径之类的属性外,是否支持通过和渲染和圆化

> P>由于标记属性通过<代码>位置PROP,一个考虑的选项是将循环属性与位置数据一起传递,例如:

const places = [
  {
    id: 1,
    name: "Park Slope",
    latitude: "40.6710729",
    longitude: "-73.9988001",
    circle: {
      radius: 3000,
      options: {
        strokeColor: "#ff0000"
      }
    }
  },   
  ...
] 
然后,可以按照如下方式修改渲染标记和圆
贴图
组件:

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>
    );
  }
}
类映射扩展了React.Component{
render(){
返回(
{this.props.places.map(place=>{
返回(
{place.circle&&(
)}
);
})}
);
}
}

< P>由于标记属性通过 SuthPROP,一个选项可以考虑将圆属性与位置数据一起传递,例如:

const places = [
  {
    id: 1,
    name: "Park Slope",
    latitude: "40.6710729",
    longitude: "-73.9988001",
    circle: {
      radius: 3000,
      options: {
        strokeColor: "#ff0000"
      }
    }
  },   
  ...
] 
然后,可以按照如下方式修改渲染标记和圆
贴图
组件:

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>
    );
  }
}
类映射扩展了React.Component{
render(){
返回(
{this.props.places.map(place=>{
返回(
{place.circle&&(
)}
);
})}
);
}
}

什么半径?基于什么?如何显示半径?作为文本?你的问题还不清楚。你仍然没有说从哪里得到半径。在发布的代码中,我看不到任何类似于半径的东西(如距离、米、公里等)。无论如何,要在标记下方显示一个圆,您需要创建一个。半径是多少?基于什么?如何显示半径?作为文本?你的问题还不清楚。你仍然没有说从哪里得到半径。在发布的代码中,我看不到任何类似于半径的东西(如距离、米、公里等)。无论如何,要在标记下方显示一个圆,需要创建一个圆。