I use the Google Maps API for v3 to return several βtypesβ of locations, each of which is represented by a different marker on the map.
I create a google.maps.places.PlacesService object and then call the "search" method once for the place type. Each time I use a different callback function (the second parameter is βsearchβ), because I need to select a different MarkerImage for each type.
var address = "97-99 Bathurst Street, Sydney, 2000"; geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { var location = results[0].geometry.location; map.setCenter(location); var marker = new google.maps.Marker({ map: map, position: location }); infowindow = new google.maps.InfoWindow(); var service = new google.maps.places.PlacesService(map);
Here are the callback functions that differ only in MarkerImage:
function banks(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i < results.length; i++) { createMarker(results[i], new google.maps.MarkerImage("/images/bank.png", null, null)); } } } function bars(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i < results.length; i++) { createMarker(results[i], new google.maps.MarkerImage("/images/bar.png", null, null)); } } } function carparks(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i < results.length; i++) { createMarker(results[i], new google.maps.MarkerImage("/images/parking.png", null, null)); } } }
This code works 100%, BUT I would like to avoid duplicate callback for each type of place (there will be about 10). Is there a way to pass the token url to a callback function? Then I need only one callback ...
javascript callback google-maps-api-3
howlee
source share