我正在尝试用https://tomchentw.github.io/react-google-maps/#groundoverlay创建的React项目中重现create-react-app
中的示例;我到目前为止在https://github.com/khpeek/trailmaps。
在src/components
,我有一个groundOverlay.js
如下:
import React from 'react';
const { compose } = require("recompose");
const {
withScriptjs,
withGoogleMap,
GoogleMap,
GroundOverlay,
} = require("react-google-maps");
const MapWithGroundOverlay = compose(
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
defaultZoom={12}
defaultCenter={{lat: 40.740, lng: -74.18}}
>
<GroundOverlay
defaultUrl="https://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg"
defaultBounds={new google.maps.LatLngBounds(
new google.maps.LatLng(40.712216, -74.22655),
new google.maps.LatLng(40.773941, -74.12544)
)}
defaultOpacity={.5}
/>
</GoogleMap>
);
然后,在App.js
,我有
import React, { Component } from 'react';
import './App.css';
import MapWithAnOverlayView from './components/overlayView';
import MapWithGroundOverlay from './components/groundOverlay';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<h1 className="App-title">Custom Overlay</h1>
</header>
<MapWithGroundOverlay
googleMapURL="https://maps.googleapis.com/maps/api/js?key=AIzaSyBimnrhiugaGSNN8WnsjpzMNJcrH_T60GI&v=3.exp&libraries=geometry,drawing,places"
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `400px` }} />}
mapElement={<div style={{ height: `100%` }} />}
/>
</div>
);
}
}
export default App;
但是,我收到以下错误:
知道为什么这不起作用吗?这不是正确的方法吗?
你需要为eslint定义google
是全局变量之类的
/* global google */
您可以将它放在您使用的文件的顶部
defaultBounds={new google.maps.LatLngBounds(
new google.maps.LatLng(40.712216, -74.22655),
new google.maps.LatLng(40.773941, -74.12544)
)}
要使代码正常工作,还需要从groundOverlay.js
文件中正确导出组件,如下所示:
/* global google */
import React from 'react';
const { compose } = require("recompose");
const {
withScriptjs,
withGoogleMap,
GoogleMap,
GroundOverlay,
} = require("react-google-maps");
const MapWithGroundOverlay = compose(
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
defaultZoom={12}
defaultCenter={{lat: 40.740, lng: -74.18}}
>
<GroundOverlay
defaultUrl="https://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg"
defaultBounds={new google.maps.LatLngBounds(
new google.maps.LatLng(40.712216, -74.22655),
new google.maps.LatLng(40.773941, -74.12544)
)}
defaultOpacity={.5}
/>
</GoogleMap>
);
export default MapWithGroundOverlay;
当你这样做时,它应该工作,屏幕下方。
我将为您的仓库创建一个PR,这样您就可以合并它并继续工作。
尝试在@Component之前使用declare var google;
例:
import React, { Component } from 'react';
import './App.css';
import MapWithAnOverlayView from './components/overlayView';
import MapWithGroundOverlay from './components/groundOverlay';
declare var google;
class App extends Component {
}