简体   繁体   English

谷歌地图API v3放置搜索 - 将另一个参数传递给回调函数

[英]Google maps API v3 places search - pass in another parameter to callback function

I am using the Google Maps places API v3 to return a number of 'types' of places, each represented by a different marker on the map. 我使用谷歌地图放置API v3来返回许多“类型”的地方,每个地点都由地图上的不同标记表示。

I create a google.maps.places.PlacesService object, and then call the "search" method once per place type. 我创建了一个google.maps.places.PlacesService对象,然后按地点类型调用“搜索”方法一次。 Each time, I use a different callback function (the second parameter of "search"), because I need to choose a different MarkerImage for each type. 每次,我使用不同的回调函数(“搜索”的第二个参数),因为我需要为每种类型选择不同的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);
    }
});

Here are the callback functions, which differ only by the MarkerImage: 以下是回调函数,它们仅由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 duplicating the callback for each different place type (there will be around 10). 这段代码100%工作,但我想避免重复每个不同地方类型的回调(大约10个)。 Is there any way I can pass the marker URL into the callback function? 有什么办法可以将标记URL传递给回调函数吗? Then I would only need a single callback... 然后我只需要一个回调......

How about the following: 以下内容如何:

service.search(req_bank, function (results, status) {
  locations(results, status, "bank");
});

function locations(results, status, type) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    // check the type to determine the marker, or pass a url to the marker icon
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Maps API v3此回调函数如何工作? - Google Maps API v3 How can this callback function work? Google Maps JavaScript API v3:多个地方的getDetails(请求,回调) - Google Maps JavaScript API v3: getDetails(request, callback) for multiple places 使用地方信息库搜索Google Maps V3上的现有点 - Search existing points on Google Maps V3 using Places Library 样式的地图和地方资料库Google Map API v3 - styled maps and places library Google Map API v3 是否可以在 google maps api v3 中添加个人地点以自动完成? - Is possible add personal places to autocomplete in google maps api v3? Google Maps API V3 - 最接近边界内的路线 - Google Maps API V3 - Closest places to route within boundary 如何减少Google Maps Places API v3 InfoWindow的填充 - How to Reduce the padding of the Google Maps Places API v3 InfoWindow 异步加载谷歌地图V3时,将数据传递给回调 - Pass data to callback when asynchronously loading google maps V3 谷歌地图 Places API V3 自动完成搜索框在搜索词/所选地点之间的差异 - Google maps Places API V3 auto-complete search box diff between search term / place selected 如何在两点之间的Google Maps API V3和Google Places API中显示位置? - How do i show places, in google maps api v3 and google places api, between two points?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM