Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-google-maps "'google' is not defined" error

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?

like image 914
SomethingsGottaGive Avatar asked May 27 '18 02:05

SomethingsGottaGive


2 Answers

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)
  }}
/>
like image 115
phan kosal Avatar answered Sep 19 '22 23:09

phan kosal


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

like image 33
Rohith Murali Avatar answered Sep 18 '22 23:09

Rohith Murali