【发布时间】:2020-11-30 04:09:23
【问题描述】:
在我的 react 应用程序中使用 react-google-maps 库,我试图在第 32 行访问谷歌地图对象调用构造函数,但我得到“谷歌未定义”错误。 这个库不公开地图对象,我如何访问谷歌对象?它也不包含在道具中
import React, { useState, useEffect } from 'react';
import flicker from '../assets/flicker.svg';
import axios from 'axios';
import { compose, withProps } from 'recompose';
import {
withScriptjs,
withGoogleMap,
GoogleMap,
Marker,
} from 'react-google-maps';
const MyMapComponent = compose(
withProps({
googleMapURL: `https://maps.googleapis.com/maps/api/js?key=${process.env.REACT_APP_GOOGLE_API_KEY}&v=3.exp&libraries=geometry,drawing,places&callback=initMap`,
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withScriptjs,
withGoogleMap
)(props => {
console.log(props.google);
return (
<GoogleMap defaultZoom={3} defaultCenter={{ lat: 46, lng: 2 }}>
{props.datas.map(data => (
<Marker
key={data.countryInfo._id}
position={{ lat: data.countryInfo.lat, lng: data.countryInfo.long }}
defaultLabel={data.cases}
icon={{
url: flicker,
size: new google.maps.Size(15, 25), ### HERE ###
}}
/>
))}
</GoogleMap>
);
});
const CoronaMap = props => {
const [data, setData] = useState([]);
useEffect(() => {
axios
.get('https://disease.sh/v3/covid-19/countries?yesterday=0&sort=cases')
.then(res => setData(res.data));
}, []);
return <MyMapComponent datas={data} />;
};
export default CoronaMap;
但在文档中,可以访问 google 对象 https://prnt.sc/tx78tf
【问题讨论】:
标签: reactjs google-maps google-maps-api-3 react-google-maps