支持`loadingElement`在`withScriptjs(带谷歌地图(组件))中被标记为必需

时间:2017-11-13 10:09:05

标签: javascript reactjs google-maps google-maps-api-3

我收到此错误:“道具类型失败:道具loadingElementwithScriptjs(withGoogleMap(Component))中被标记为必需,但其值为undefined。     in withScriptjs(withGoogleMap(Component))“

我的Map.js文件:

import React, {Component} from "react";    
import {
  withScriptjs,
  withGoogleMap,
  GoogleMap,
  Marker,
} from "react-google-maps";

const MapWithAMarker = withScriptjs(withGoogleMap(props =>
  <GoogleMap
    defaultZoom={8}
    defaultCenter={{ lat: -34.397, lng: 150.644 }}
  >
    <Marker
      position={{ lat: -34.397, lng: 150.644 }}
    />
  </GoogleMap>
));

<MapWithAMarker
  googleMapURL="https://maps.googleapis.com/maps/api/js?key=AIzaSyAIdFegRbfOurYRvDN8oQNJRmpKgIj48ZY&libraries=geometry,drawing,places"
  loadingElement={<div style={{ height: `100%` }} />}
  containerElement={<div style={{ height: `100%` }} />}
  mapElement={<div style={{ height: `100%` }} />}
/>  
export default MapWithAMarker;

我正在我的其他js文件中导入此文件并放入

<div style={{width:400, hight:300, background:"red"}}>
          <MapWithAMarker />
        </div>
像这里面的那个js。我收到了上述错误。

1 个答案:

答案 0 :(得分:1)

这就是我在当前项目中调用它的方式,它运行得很好。也许这会让你指向正确的方向。

const EmptyMap = compose(
    withProps({
      googleMapURL: "https://maps.googleapis.com/maps/api/js?key=APIKEY&v=3.exp&libraries=geometry,drawing,places",
      loadingElement: <div style={{ height: `100%` }} />,
      containerElement: <div style={{ height: `1000px`, display: 'none' }} />,
      mapElement: <div style={{ height: `100%` }} />,
    }),
    withScriptjs,
    withGoogleMap
  )((props) => 
        <GoogleMap
        defaultZoom={9}
        defaultCenter={{ lat: parseFloat(props.userLat), lng: parseFloat(props.userLng) }}
        >

        </GoogleMap>
);
相关问题