我發現這在實踐中很難做到。我採取的方法是使用here來簡單地創建一個鼠標事件,當使用縮放按鈕時觸發縮放。該事件創建在地圖的中心。
下面是相關代碼:
.on("click", function() {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent(
'dblclick', // in DOMString typeArg,
true, // in boolean canBubbleArg,
true, // in boolean cancelableArg,
window,// in views::AbstractView viewArg,
120, // in long detailArg,
width/2, // in long screenXArg,
height/2, // in long screenYArg,
width/2, // in long clientXArg,
height/2, // in long clientYArg,
0, // in boolean ctrlKeyArg,
0, // in boolean altKeyArg,
(by > 0 ? 0 : 1), // in boolean shiftKeyArg,
0, // in boolean metaKeyArg,
0, // in unsigned short buttonArg,
null // in EventTarget relatedTargetArg
);
this.dispatchEvent(evt);
});
整個事情是一個黑客位,但它在實踐中的工作,我已經發現了這個更容易,而不是計算每個偏移/變焦正確中心。
mbostock現在有專注於這裏的中心變焦按鈕的例子(四月9,2015):http://bl.ocks.org/mbostock/7ec977c95910dd026812 – airwwwave