Google maps api 3 向谷歌地图请求添加上下文(此)

Google maps api 3 向谷歌地图请求添加上下文(此),google-maps-api-3,Google Maps Api 3,我正在为一个网站开发这个页面,它根据当前登录的用户生成搜索结果。我的目标是环游每一所房子,并给它分配一段距离 我的问题是,我似乎无法在距离矩阵请求/响应中使用上下文 $('.houseRapper')。每个(函数(){ var origin=$(this).children('.houseMeta').attr('data-origin'); var destination=$(this).children('.houseMeta').attr('data-destination'); var

我正在为一个网站开发这个页面,它根据当前登录的用户生成搜索结果。我的目标是环游每一所房子,并给它分配一段距离

我的问题是,我似乎无法在距离矩阵请求/响应中使用上下文

$('.houseRapper')。每个(函数(){
var origin=$(this).children('.houseMeta').attr('data-origin');
var destination=$(this).children('.houseMeta').attr('data-destination');
var service=new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
来源:[来源],
目的地:[目的地],
travelMode:google.maps.travelMode.DRIVING,
unitSystem:google.maps.unitSystem.METRIC,
避免:错误,
避免:错误,
},回调);
函数回调(响应、状态){
if(status==google.maps.DistanceMatrixStatus.OK){
var origins=response.originAddresses;
var destinations=response.destinationaddress;
对于(变量i=0;i

尝试:

var that = this;
var origin = $(this).children('.houseMeta').attr('data-origin');
....
随后:

console.log($(that).html());
我会将“this”作为参数传递给函数,有点像部分函数的工作方式

var callback = function(this) {
    return function(response, status) {
        // ...
    }
}(this);

有两个参数将被传递到
$。每个()都是
-回调、元素的索引和元素

只需使用以下参数:

$('.houseWrapper').each(function(index,node) {
   //somewhere
   console.log($(node).html());
});

绝对有效!这正是我的工作。我希望有一种方法可以通过请求传递它,就像某种AJAX选项一样。