我正在使用 Google Maps Places API v3 返回许多“类型”的地点,每个地点都由地图上的不同标记表示。
我创建了一个 google.maps.places.PlacesService 对象,然后为每个地点类型调用一次“搜索”方法。每次,我都使用不同的回调函数(“搜索”的第二个参数),因为我需要为每种类型选择不同的 MarkerImage。
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);
// banks
var req_bank = { location: location, radius: 500, types: ['bank'] };
service.search(req_bank, banks);
// bars
var req_bar = { location: location, radius: 500, types: ['bar'] };
service.search(req_bar, bars);
// car parks
var req_parking = { location: location, radius: 500, types: ['parking'] };
service.search(req_parking, carparks);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
以下是回调函数,它们仅与 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));
}
}
}
此代码 100% 有效,但我想避免为每种不同的地点类型重复回调(大约有 10 个)。有什么方法可以将标记 URL 传递给回调函数?然后我只需要一个回调......