我知道这是一个相当古老的问题,但我想我可以根据我最近的gmap3
和Google Maps API的经验添加一些输入。
而不是沿着使用infobox
库的路线(我发现有一匹母马可以与gmap3库一起工作),我决定通过绑定标记点击事件来产生一个我可以设计的覆盖图然而我想要的。
所以,我对像这样
// JSON
var data = JSON.parse('[
{
"address":"New Street, Salisbury, UK",
"content":"hello world from salisbury"
},
{
"address":"86000 Poitiers, France" ,
"content":"hello world from poitiers"
},
{
"address":"66000 Perpignam, France",
"content":"hello world from perpignam"
}
]');
设置我的GMAP默认
// Gmap Defaults
$map.gmap3({
map:{
options:{
center:[46.578498,2.457275],
zoom: 5
}
}
});
我依次通过我的JSON我用我所有的地址JSON和建立映射
// Json Loop
$.each(data, function(key, val) {
$map.gmap3({
marker:{
values:[{
address:val.address,
events: {
click: function() {
gmap_clear_markers();
$(this).gmap3({
overlay:{
address:val.address,
options:{
content: '<div class="infobox">'+val.content+'</div>',
offset:{
y:-32,
x:12
}
}
}
});
}
}
}]
}
});
});
gmap_clear_markers()
函数触发所有其他信息框(覆盖)实例被删除。
// Function Clear Markers
function gmap_clear_markers() {
$('.infobox').each(function() {
$(this).remove();
});
}
现在,我想我的JSON,我可以添加尽可能多的地址,它会与刚带班的信息框一个普通的div包装,我可以相应风格的信息框。
我不确定这是否是最好的方法 - 我只是说这对我有用。
这里是JSFIDDLE example