1

我现在在 React 中使用 Google Map,现在我在使用getDetails()Google PlacesService 的功能时遇到了问题。这是我的代码的一部分,我想在其中使用此功能显示有关所选地点的信息InfoWindow

<Marker
  {...marker}
  key={i}
  position={marker.location}
  title={marker.title}
  place_id={marker.place_id}
  id={i}
  icon={'http://www.serwisstron.pl/icons/' + marker.type + '.png'}
  onClick={() => {
      props.toggleLocationsActive(i);
  }}
  animation={google.maps.Animation.DROP}
  >
  {i === props.activeKey && (
    service.getDetails({placeId: marker.place_id}, (marker, status) => {
      if(status === google.maps.places.PlacesServiceStatus.OK) {
      // here will be code will which put the data on InfoWindow
      }
   })
)}
   <InfoWindow onCloseClick={props.onToggleOpen}>
     <div id="details"><strong>{ marker.title } </strong><span><br/>Address:</span><span id="name"></span></div>
   </InfoWindow>)}
</Marker>

现在这段代码得到这个错误:

未捕获的类型错误:无法在 Lc(js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4)的 P4.attributionText_changed(places_impl.js:32)处读取 Object._.Tv(util.js:20)处未定义的属性“innerHTML”: 51) 在 P4._.M.bindTo (js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:126) 在 Object.Q4.f (places_impl.js:32) 在 Hr。(js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:179) 在 js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:136 在 Object. (js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:60) 在 js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4: 136 在对象。(js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:60) at js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:136 at js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:60 at js?libraries=geometry, drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:136 at Sd (js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:63) at Rd.wa (js?libraries=geometry,drawing,places&key=AIzaSyBqtLvddq3jzZ_Lnu9M8266EMVBfXtlUT4:136) at util.js:1

也许你们中的一些人知道,如何正确使用这个功能来反应......我在谷歌上找了很长时间,但我什么也没找到......

这是 Github 上该项目的链接: https ://github.com/hajczek/Neighborhood---Warsaw-Cultural-Map

感谢您的任何提示:)

4

1 回答 1

0

所有 DOM 修改都应该通过 React 完成,因为您正在使用它。所以不要设置innerHTML(这可能会导致漏洞),你应该设置组件的状态,然后根据状态渲染标记。您可以提取一个呈现 InfoWindow 的组件:

class Popup extends Component {
  state = {};
  componentDidMount() {
    const marker = this.props.marker;

    getInfo(marker.title).then(data => {
        console.log('---',data);
      const link = data[3][0];
      const art = data[2][0];
      if (link) {
        this.setState({
          art,
          link,
          text: "See article in Wikipedia »"
        });
      } else {
        this.setState({
          info: "Unfortunately, no info was returned for this data."
        });
      }
    });

    geocodeByPlaceId(marker.place_id)
      .then(results => {
        const address = results[0].formatted_address;
        this.setState({ address });
      })
      .catch(error => console.error(error));
  }
  render() {
    const props = this.props;
    return (
      <InfoWindow onCloseClick={props.onToggleOpen}>
        <div>
          <span id="title">{props.marker.title}</span>
          <br />
          <br />
          <span id="address-title">Address:</span>
          <br />
          <span>{this.state.address}</span>
          <br />
          <br />
          <span>
            <em>{this.state.art}</em>
          </span>
          <br />
          <br />
          <a target="blank" href={this.state.link}>
            {this.state.text}
          </a>
          <span>{this.state.info}</span>
        </div>
      </InfoWindow>
    );
  }
}

并将其呈现在标记内:

{i === props.activeKey && (
                <Popup marker={marker} onCloseClick={props.onToggleOpen} />
              )}

请注意,getInfo应该用数据解决承诺:

export const getInfo = search => {
  return fetch(
    "https://pl.wikipedia.org/w/api.php?&origin=*&action=opensearch&search=" +
      search +
      "&limit=1",
    {
      headers: { Accept: "application/json" }
    }
  )
    .then(response => response.json())
    .catch(e => requestError(e));

上面的示例可能无法正常工作,它是为了演示该方法。

于 2018-05-26T23:44:36.417 回答