有没有办法将标记中心的地图居中?在标记之间建立中心,考虑到标记是从远程dataSource
加载的?
答案 0 :(得分:3)
这可以根据此处的示例完成:http://www.kendoui.io/kendo-ui/dataviz/map/how-to/zoom-on-area.html
代码如下
<button id="center">Center on markers</button>
<div id="map"></div>
<script>
var markers = [
{"latlng":[30.2675,-97.7409], "name": "Zevo Toys"},
{"latlng": [30.2707,-97.7490],"name": "Foo Bars"},
{"latlng": [30.2705,-97.7409],"name": "Mainway Toys"},
{"latlng": [30.2686,-97.7494], "name": "Acme Toys"}];
$("#map").kendoMap({
layers: [{
type: "tile",
urlTemplate: "http://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png",
subdomains: ["a", "b", "c"],
attribution: "© <a href='http://osm.org/copyright'>OpenStreetMap contributors</a>."
}, {
type: "marker",
dataSource: {
data: markers
},
locationField: "latlng",
titleField: "name"
}]
});
function centerMap() {
var map = $("#map").getKendoMap();
var layer = map.layers[1];
var markers = layer.items;
var extent;
for (var i = 0; i < markers.length; i++) {
var loc = markers[i].location();
if (!extent) {
extent = new kendo.dataviz.map.Extent(loc, loc);
} else {
extent.include(loc);
}
}
map.extent(extent);
}
$("#center").click(centerMap);
</script>