Jonathan Allen Grant
Jonathan Allen Grant

Reputation: 3648

Google Maps Distance API is undefined in react

I am trying to use the Google Maps Distance Matrix Service (https://developers.google.com/maps/documentation/javascript/distancematrix).

I put this line in my public/index.html: <script src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY"></script>

In a react's js file, I can print window.google and see the global variable. I can also see that it has a function window.google.maps.DistanceMatrixService() to create the service. However, when I create that service with var service = window.google.maps.DistanceMatrixService(), service is undefined. Any idea why?

Upvotes: 0

Views: 1268

Answers (1)

Jonathan Allen Grant
Jonathan Allen Grant

Reputation: 3648

As Jaromanda X pointed out, I missed the new keyword.

Full line is now: var service = new window.google.map.DistanceMatrixService().

Upvotes: 1

Related Questions