gmaps.js - 如何使用select过滤标记?

时间:2013-03-24 15:34:34

标签: jquery google-maps google-fusion-tables gmaps.js

我可以使用Google Maps JS过滤没有gmaps.js的标记。但是,下面的代码与gmaps.js不兼容。错误声明: changeMap不是函数。我的业余JS级别无法弄明白。有什么想法吗?

我目前使用gmaps.js的错误JS

$(document).ready(function(){
var layer;
var tableid = 2796050;
var map = new GMaps({
el: '#map_canvas',
lat: 33.68906,
lng: -78.886694,
zoom: 11
});
infoWindow = new google.maps.InfoWindow({});
map.loadFromFusionTables({
query: {
select: '\'Location\'',
from: '1LNKdPBYZOlszyjQqzUhoqsQXVdkyQ7pP2o2ft9o'
},
suppressInfoWindows: false,
events: {
click: function(point){
infoWindow.setPosition(point.latLng);
}
}
});
// problem area
function changeMap() {
var searchString = document.getElementById('search-string').value.replace(/'/g, "\\'");
if(!searchString) {
layer.setQuery("SELECT 'Latitude' FROM " + tableid);
return;
}
layer.setQuery("SELECT 'Latitude' FROM " + tableid + " WHERE 'Category' = '" + searchString + "'");
}
});

HTML

<!-- HTML -->
<form>
<label>Category </label>
<select id="search-string" onchange="changeMap(this.value);">
<option value="">All Places</option>
<option value="Attractions">Attractions</option>
<option value="Dining">Dining</option>
<option value="Shopping">Shopping</option>
<option value="Services">Services</option>
</select>
</form>

1 个答案:

答案 0 :(得分:0)

要使这个工作正常,您需要在HTML位之前添加<script>块。

由于您仍在使用jQuery,请考虑编写事件而不是changeMap函数。首先,从onchange中删除select属性。并改变这一点:

function changeMap() {
    var searchString = document.getElementById('search-string').value.replace(/'/g, "\\'");
    if(!searchString) {
        layer.setQuery("SELECT 'Latitude' FROM " + tableid);
        return;
    }
    layer.setQuery("SELECT 'Latitude' FROM " + tableid + " WHERE 'Category' = '" + searchString + "'");
}

......对此:

$('#search-string').change( function() {
    var searchString = document.getElementById('search-string').value.replace(/'/g, "\\'");
    if(!searchString) {
        layer.setQuery("SELECT 'Latitude' FROM " + tableid);
        return;
    }
    layer.setQuery("SELECT 'Latitude' FROM " + tableid + " WHERE 'Category' = '" + searchString + "'");
})

更多信息:http://api.jquery.com/change/