In React Google Maps, Getting "'google' Is Not Defined" Even When Component Is Wrapped With WithScriptjs?
I'm trying to reproduce the example in https://tomchentw.github.io/react-google-maps/#groundoverlay in a React project created with create-react-app; what I have so far is at https
Solution 1:
You need to define for eslint that google
is the global variable like
/* global google */
You can put this at the top of the file where you use
defaultBounds={new google.maps.LatLngBounds(
new google.maps.LatLng(40.712216, -74.22655),
new google.maps.LatLng(40.773941, -74.12544)
)}
To make your code work, you need to also export correctly component from the groundOverlay.js
file, like this:
/* 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;
When you do this, it should work, screen below.
I will create a PR to your repo, so you will can merge it and continue working.
Pull request created: https://github.com/khpeek/trailmaps/pull/1
Solution 2:
Sing google is a global variable, you need to read it explicitly using window
this way
const google = window.google;
or you can directly access it
new window.google.maps.LatLngBounds()
The ESlint fix can be done this way by declaring it at the top of your file
/*global google*/
Solution 3:
try to use declare var google;
before @Component
Example:
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 {
}
Post a Comment for "In React Google Maps, Getting "'google' Is Not Defined" Even When Component Is Wrapped With WithScriptjs?"