Google地图API v3地方搜索 - 传递另一个参数到回调函数 [英] Google maps API v3 places search - pass in another parameter to callback function

查看:151
本文介绍了Google地图API v3地方搜索 - 传递另一个参数到回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Maps places API v3来传回多个「类型」地点,每个地点都是由地图上的不同标记所代表。

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.

google.maps.places.PlacesService对象,然后对每个地点类型调用搜索方法一次。每次,我使用不同的回调函数(搜索的第二个参数),因为我需要为每种类型选择不同的MarkerImage。

I create a google.maps.places.PlacesService object, and then call the "search" method once per place type. Each time, I use a different callback function (the second parameter of "search"), because I need to choose 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);

        // 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不同:

Here are the callback functions, which differ only by the 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));
        }
    }
}

但我想避免重复的回调为每个不同的地方类型(将有大约10)。 有没有什么方法可以将标记URL传递到回调函数?然后我只需要一个回调...

This code works 100%, BUT I would like to avoid duplicating the callback for each different place type (there will be around 10). Is there any way I can pass the marker URL into the callback function? Then I would only need a single callback...

推荐答案

以下内容:

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
  }
}

这篇关于Google地图API v3地方搜索 - 传递另一个参数到回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆