使用 jQuery Ui Map 进行过滤



我使用的是旧版本的jQuery UI Map。所以当时过滤工作得很好。以下是我用于旧版本的代码:

$('#map_canvas').gmap({ 'center':new google.maps.LatLng(43.730531,-79.416927), 'callback': function() {
            $.getJSON( 'path to json', 'category=activity', function(data) { 
                $.each( data.markers, function(i, m) {
                    $('#map_canvas').gmap('addMarker', { 'tag': m.area, 'position': new google.maps.LatLng(m.latitude, m.longitude), 'center': new google.maps.LatLng(m.latitude, m.longitude), 'bounds': true } )
                    .click(function() { $('#map_canvas').gmap('openInfoWindow', { 'content': m.content }, this); });

                });
            });
            $("#some").change(function() {
                                    var bounds = new google.maps.LatLngBounds();
                                    var tag = $(this).val();
                                    if ( tag == '*' ) {
                                        $('#map_canvas').gmap('findMarker', 'tag', tag, function(found, marker) {
                                            marker.setVisible(true); 
                                            bounds.extend(marker.position);
                                            marker.map.fitBounds(bounds);   
                                        });
                                    } else {
                                        $('#map_canvas').gmap('findMarker', 'tag', tag, function(found, marker) {
                                            if (found) {
                                                marker.setVisible(true); 
                                                bounds.extend(marker.position);
                                                marker.map.fitBounds(bounds);
                                            } else { 
                                                marker.setVisible(false); 
                                            }
                                        });
                                    }
                                    $('#map_canvas').gmap('option', 'center', bounds.getCenter());
                                });
        }

但是当我切换到新版本时,我无法进行过滤。我用于过滤新版本的代码如下:

 $('#map_canvas').gmap({ 'center':new google.maps.LatLng(43.730531,-79.416927), 'callback': function() {
$.getJSON( 'path to json', 'category=activity', function(data) { 
    $.each( data.markers, function(i, m) {
        $('#map_canvas').gmap('addMarker', { 'tag': m.area, 'position': new google.maps.LatLng(m.latitude, m.longitude), 'center': new google.maps.LatLng(m.latitude, m.longitude), 'bounds': true } )
        .click(function() { $('#map_canvas').gmap('openInfoWindow', { 'content': m.content }, this); });

    });
});
$("#some").change(function() {
                        var bounds = new google.maps.LatLngBounds();
                        var tag = $(this).val();
                        if ( tag == '*' ) {
                            $('#map_canvas').gmap('find', 'markers', { 'property': 'tag', 'value': tag }, function(marker, isFound) {
                                marker.setVisible(true); 
                                bounds.extend(marker.position);
                                marker.map.fitBounds(bounds);   
                            });
                        } else { 
                            $('#map_canvas').gmap('find', 'markers', { 'property': 'tag', 'value': tag }, function(marker, isFound) {
                                if (isFound) {
                marker.setVisible(true); 
                                    bounds.extend(marker.position);
                                    marker.map.fitBounds(bounds); 
                                } else {  
                                    marker.setVisible(false);
                                }
                            });
                        }
                        $('#map_canvas').gmap('option', 'center', bounds.getCenter());
                    });

}

});

上面的代码适用于所有标记(其标签值为 * )。但对于其他财产,它不起作用。当我尝试调试它时,我发现它没有进入if(isFound)部分。但是有标签。所以它应该去。

您可以在此处找到其文档。

文档似乎不正确/不是最新的。

属性(在本例中为tag)必须是数组,否则过滤将始终失败。

在标记创建中替换以下内容:

'tag': m.area

'tag': [m.area]

最新更新