I am creating a web that utilizes react-google-maps and I am getting a bunch of undefined errors. I am new to react/js world so any help would be much appreciated. Here's the exact errors:
Failed to compile.
./src/App.js
Line 23: 'lifecycle' is not defined no-undef
Line 25: 'google' is not defined no-undef
Line 28: 'google' is not defined no-undef
Line 29: 'google' is not defined no-undef
Line 30: 'google' is not defined no-undef
Line 32: 'google' is not defined no-undef
Here's the code:
import React, { Component } from 'react';
import './App.css';
import { compose, withProps } from "recompose"
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"
const MapWithDirections = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `800px`, width: '800px'}} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withScriptjs,
withGoogleMap,
lifecycle({
componentDidMount() {
const DirectionsService = new google.maps.DirectionsService();
DirectionsService.route({
origin: new google.maps.LatLng(45.29233869999999, -70.06117489999997),
destination: new google.maps.LatLng(45.3570637, -70.06257679999999),
travelMode: google.maps.TravelMode.DRIVING,
}, (result, status) => {
if (status === google.maps.DirectionsStatus.OK) {
this.setState({
directions: result,
});
} else {
console.error(`error fetching directions ${result}`);
}
});
}
})
)((props) =>
<GoogleMap
defaultZoom={8}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
{props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
</GoogleMap>
)
class App extends Component {
render() {
return (
<div className="App">
<MapWithDirections/>
</div>
);
}
}
export default App;
It looks like I am failing to properly import google maps but looking at the example, it shouldn't have to. Any ideas?
You can try this new window.google.maps
It works for me!
Example:
<Marker
icon={{
url:"../../assets/img/carz.png",
anchor: new window.google.maps.Point(10, 10),
scaledSize: new window.google.maps.Size(20, 20)
}}
/>
You have missed the import of both lifecycle and google. In the example you can see,
const { compose, withProps, lifecycle } = require("recompose");
and the import of google is not mentioned in the example but that too should be imported.
Regarding importing google, it's to do with eslint validation not JS. do the following change:
PlacesAutocomplete.js Line 1:
/*global google*/
^ this will declare to eslint that google is a global name and will be available at runtime. For more info you can go thru this git page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With