简体   繁体   中英

Google Maps API javascript: nearbysearch callback function not calling

I followed Google's documentation closely, but for some reason my callback function isn't being called. I stripped everything down so that it should just show an alert, and it doesn't. I've tried searching for the problem, but it's so simple I can't even figure out what to search for. Any thoughts?

service = new google.maps.places.PlacesService(myMap);
service.nearbySearch(request, callback);

// this displays
alert("TESTING");

...

function callback(results, status) {

     // this does not display
     alert("WHY DOESNT THIS DISPLAY?");

}

I've also tried this:

service = new google.maps.places.PlacesService(myMap);
service.nearbySearch(request, function callback(results, status) {

alert("WHY DOESNT THIS WORK?");

});

Take a look at the console, you will see this warning:

Warning: you have included the Google Maps API multiple times on this page. This may cause unexpected errors.

That's what you got, an unexpected error.

You have included the API 3 times:

line#140

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCx9FbzJ-0Ck8gts9oq11nAZU7F6LjLq8Y&sensor=false">

            </script>

line#146:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>

line 636:

<script type='text/javascript' src='http://maps.google.com/maps/api/js?sensor=false&#038;ver=1.0.0'></script>

Remove the scripts from line#140 and line#636 and you will see both alerts

function callback(results, status) {

应该:

function (results, status) {

Is your request object/string valid? You're not setting request to anything in this snippet: googleplaces probably won't fire your callback if you didn't make a real requst.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM