javascript - 过滤 Google map 标记

标签 javascript google-maps google-maps-api-3

我已将不同的信息传递到我的自定义 Google map 中,如下所示;

marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    icon:image,
    title: locations[i][0],
    price: locations[i][3],
    occupancy: locations[i][4],
});

我试图在单击按钮时过滤标记;

$(document).on('click', '#price-filter', function(){
    $.each(map.markers, function(i, marker) {
        if(marker.price <= '80.00')
            marker.visible = false;
        else
            marker.visible = true;
    });
});

这没有运行,我已经尝试了 function initMap() {} 内部和外部的单击,但它不起作用。有什么想法吗?

编辑请找到位于 https://jsfiddle.net/g19avj3e/ 的 JSFiddle

最佳答案

如果将单击函数移至 initMap 函数内(以便渲染 DOM 并且可以找到 #price-filter 复选框),它将运行。但没有 map.markers 数组。您可以创建该数组并将标记插入其中:

// before loop
map.markers = [];

// inside loop
map.markers.push(marker);

下一个问题是没有记录的标记的 .visible 属性,您需要使用记录的 .setVisible() 方法。

$(document).on('click', '#price-filter', function(){
    $.each(map.markers, function(i, marker) {
        if(marker.price <= '80.00')
            marker.setVisible(false);
        else
            marker.setVisible(true);
    });
});

proof of concept fiddle

关于javascript - 过滤 Google map 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35065861/

相关文章:

javascript - 我怎样才能在python vue js中的views.py中处理GET和POST请求?

javascript - 为什么 a 显示 "function getFullYear() { [native code for Date.getFullYear, arity=0] }"而不是方法返回的值?

java - Google map v2 通过 Google Play 获取我的位置

javascript - 将多个谷歌地图绘制到页面

ruby-on-rails - 在第三页上没有获取下一页 token google place api?

Jquery+XML+Google map 适用于 FF/IE,但不适用于 Chrome/Opera

javascript - 我可以将 chrome 扩展应用程序嵌入到我的主要 AngularJS 应用程序中吗?

javascript - webdriverIO - 添加 junit 记者

javascript - 获取谷歌地图中距离中心最近的 N 个标记

javascript - 从外部访问二维数组中的 Google map 标记