我正在尝试按照此示例和文档添加单击并悬停到 CartoDB 地图的信息窗口,但我很挣扎。
这是我的 HTML:
<div id="map"></div>
<script type="infowindow/html" id="infowindow_template">
<span> custom </span>
<div class="cartodb-popup">
<a href="#close" class="cartodb-popup-close-button close">x</a>
<div class="cartodb-popup-content-wrapper">
<div class="cartodb-popup-content">
<h4>{{content.data.postcode}}</h4>
</div>
</div>
<div class="cartodb-popup-tip-container"></div>
</div>
</script>
这是我的 JavaScript:
window.onload = function() {
var options = {
center: [53.2, 0.0],
zoom: 5
};
var map_object = new L.Map('map', options);
var cartocss = '#combined { marker-fill-opacity: 0.9; marker-line-color: #FFF;marker-line-width: 1;marker-line-opacity: 1; marker-placement: point; marker-type: ellipse; marker-width: 10; marker-fill: #FF6600; marker-allow-overlap: true; }';
cartodb.createLayer(map_object, {
user_name: 'myname',
type: 'cartodb',
sublayers: [
{
type: "http",
urlTemplate: "http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png",
subdomains: [ "a", "b", "c" ]
},
{
sql: 'SELECT * FROM combined',
cartocss: cartocss
},
]
})
.addTo(map_object)
.on('done', function(layer) {
var sublayer = layer.getSubLayer(1);
cartodb.vis.Vis.addInfowindow(map_object, sublayer, [ 'postcode'], {
infowindowTemplate: $('#infowindow_template').html(),
templateType: 'mustache'
});
})
.error(function(err) {
console.log("error: " + err);
});
});
这会在地图上显示我的点,但是当我单击它们时,什么也没有发生。控制台中没有错误。我究竟做错了什么?
更新:奇怪的是,如果我删除第一层并将子层选择器更改为var sublayer = layer.getSubLayer(0)
,则会出现信息窗口。但我确实需要两层!