I know this is a really common error, but I read all the other "Uncaught ReferenceError: google is not defined" questions and none of them helped me fix it. I am using the Google Places API but my application does not use a map so I referred to this question to figure that out.
Many of the answers talked about loading the api script asyncronously before everything else, and the order of the scripts matters, but I don't think that's my issue. I followed the example in the Places API Documentation so I don't know what I am doing wrong.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
</head>
<body>
<h1>Places Near Chicago</h1>
<div id="list"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places" async defer></script>
<script>
var service = new google.maps.places.PlacesService(document.getElementById('list'));
var request = {
location: new google.maps.LatLng(41.8781136, -87.6297982),
radius: 5
};
service.search(request, callback);
function callback(results, status){
if(status == google.maps.places.PlacesServiceStatus.OK){
console.log(status);
for (var i = 0; i < results.length; i++) {
console.log(results[i].name, results[i].types);
}
}
};
</script>
</body>
</html>
The issue is specifically with the line
var service = new google.maps.places.PlacesService(document.getElementById('list'));
Thanks!
EDIT: I updated my code as per Jeff's suggestion and still get the same error.