在React Google Maps中,即使使用withScriptjs包装组件,也未定义“'google'”?

问题描述 投票:0回答:2

我正在尝试用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;

但是,我收到以下错误:

enter image description here

知道为什么这不起作用吗?这不是正确的方法吗?

javascript reactjs google-maps google-maps-api-3 google-api
2个回答
1
投票

你需要为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;

当你这样做时,它应该工作,屏幕下方。

enter image description here

我将为您的仓库创建一个PR,这样您就可以合并它并继续工作。

拉创请求:https://github.com/khpeek/trailmaps/pull/1


-1
投票

尝试在@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 {

}
© www.soinside.com 2019 - 2024. All rights reserved.