我使用jquery-mobile和gmap-plugin开发PhoneGap应用程序。 我有一些代码和一些麻烦。
str = '';
db.transaction(function(t){
t.executeSql('SELECT name,address FROM table', [], function(t, res) {
for(var i=0; i<res.rows.length; i++) {
str += '<option selected="" value="'+res.rows.item(i).address+'">'+
res.rows.item(i).name + '</option>';
}
});
},null,function(){
$('#selectMenu').html(str);
$('#selectMenu').selectmenu("refresh");
$('#selectMenu').on('change', function () {
setMarks();
});
});
function setMarks (){
$('#map_canvas').gmap('clear', 'markers');
var el;
$("#selectMenu option:selected").each(function () {
el = $(this);
$('#map_canvas').gmap('search', { 'address': el.val() }, function(results, status) {
if ( status === 'OK' ) {
$('#map_canvas').gmap('addMarker', { 'position': results[0].geometry.location, 'bounds': true,
'html': "<h3>"+el.text()+"</h3><p>"+el.val()+"</p>"},
function(map, marker) {
$(marker).click(function() {
$('#map_canvas').gmap('openInfoWindow',{'content': $(this).attr('html')}, this);
return false;
});
});
}
});
});
}
在方法搜索对象的回调函数中, el 是我所选元素的集合中的最后一个,但我希望看到每个对象。 也许有人面临像这样的问题。谢谢!
答案 0 :(得分:0)
我决定改变。不确定这是最好的方式,但它对我有用。 我使用全局标记数组。在标记点击事件的回调函数中,我得到当前标记的索引。通过此索引,我从 selectmenu 获取选项,然后进入我需要的 infoWindow 信息。
var arMarkers = new Array();
function arMarkersIndexOf(o) {
for (var i = 0; i < arMarkers.length; i++) {
if (arMarkers[i].getPosition() == o.getPosition()) {
return i;
}
}
return -1;
}
var str = '';
db.transaction(function(t){
t.executeSql('SELECT name,address FROM table', [], function(t, res) {
for(var i=0; i<res.rows.length; i++) {
str += '<option selected="" value="' +
res.rows.item(i).address + '" data-index="' + i.toString() + '" data-seq="' + res.rows.item(i).MainOrg + '">' +
res.rows.item(i).name + '</option>';
$('#map_canvas').gmap('search', { 'address': res.rows.item(i).address },
function(results, status) {
if ( status === 'OK' ) {
$('#map_canvas').gmap('addMarker', { 'position': results[0].geometry.location, 'bounds': true},
function(map, marker) {
arMarkers.push(marker);
$(marker).click(function() {
var i = arMarkersIndexOf(this);
$('#map_canvas').gmap('openInfoWindow', {'content': '<h3>' + $('#selectMenu option[data-index="' + i.toString() + '"]').text() + '</h3>' +
'<h4>' + $('#selectMenu option[data-index="' + i.toString() + '"]').data('seq') + '</h4>' +
'<p>' + $('#selectMenu option[data-index="' + i.toString() + '"]').val() + '</p>'}, this);
return false;
});
});
}
});
}
});
},null,function(){
$('#selectMenu').html(str);
$('#selectMenu').selectmenu("refresh");
});