您在此页面上多次包含 Google Maps JavaScript API

Kam*_*ran 4 google-map-react

如何避免“您在此页面上多次包含 Google Maps JavaScript API。这可能会导致意外错误。” 如果我使用 google-map-react 在另一个组件中显示地图和 react-places-autocomplete 来获取地址和坐标?

//LocationMapPage  component that displays the map box and pass the props to it
class LocationMapPage extends Component {

  render() {
    let {latLng,name,address} = this.props.location;
    return (
        <MapBox lat={latLng.lat} lng={latLng.lng} name={name} address={address}/>
    )
  }

}

//MapBox component
import React from "react";
import GoogleMapReact from 'google-map-react';
import apiKey from "../../configureMap";


const Marker = () => <i className="fa fa-map-marker fa-2x text-danger" />

const MapBox = ({lat,lng, name, address}) => {
    const center = [lat,lng];
    const zoom = 14;
    return (  
        <div style={{ height: '300px', width: '100%' }}>
            <GoogleMapReact
            bootstrapURLKeys={{ key: apiKey }}
            defaultCenter={center}
            defaultZoom={zoom}
            >
            <Marker
                lat={lat}
                lng={lng}
                text={`${name}, ${address}`}
            />
            </GoogleMapReact>
      </div>
    );
}

export default MapBox; 
Run Code Online (Sandbox Code Playgroud)

地图为空: 在此处输入图片说明 控制台中的错误:您在此页面上多次包含 Google Maps JavaScript API。这可能会导致意外错误。

怎么解决?

我在项目中使用google-map-reactreact-places-autocomplete

Kam*_*ran 6

作为我在两个不同组件中使用谷歌地图 API 的特定用例的临时解决方案,我刚刚在 index.html 中添加了脚本:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script> 
Run Code Online (Sandbox Code Playgroud)

我这样做是为了避免根据react-places-autocomplete GitHub 页面上的文档出现该特定错误。