[英]How do I zoom into Google Maps with just javascript?
I'm challenging myself to navigate Google Maps ( http://maps.google.com ) without my mouse and just by entering commands into Chrome Developer Console. 我正在挑战自己,不用鼠标,只需在Chrome开发者控制台中输入命令即可浏览Google Maps( http://maps.google.com )。
I found that the class name of the main canvas that Google Maps uses is widget-scene-canvas
, and I've successfully obtained that canvas with 我发现Google Maps使用的主画布的类名是
widget-scene-canvas
,并且我已经成功地通过
document.getElementsByClassName('widget-scene-canvas')[0]
Hence, I tried to trigger a click event to zoom in with 因此,我尝试触发点击事件以放大
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 300, 300, 300, 300, false, false, false, false, 0,
document.getElementsByClassName('widget-scene-canvas')[0]);
document.body.dispatchEvent(evt);
evt.initMouseEvent("click", true, true, window,
0, 300, 300, 300, 300, false, false, false, false, 0,
document.getElementsByClassName('widget-scene-canvas')[0]);
document.body.dispatchEvent(evt);
However, it doesn't even seem like Google Maps is detecting a click. 但是,似乎Google Maps似乎都没有检测到点击。
What am I doing wrong? 我究竟做错了什么? Anyone other adventurous people want to give this a shot?
还有其他喜欢冒险的人吗?
This works for me: 这对我有用:
Zoom in: $(".widget-zoom-in").click();
放大:
$(".widget-zoom-in").click();
Zoom out: $(".widget-zoom-out").click();
缩小:
$(".widget-zoom-out").click();
Invoking clicks on the zoom buttons in vanilla 调用香草中的缩放按钮
function zoom(out) {
document.querySelector('.widget-zoom-' + (out ? 'out' : 'in')).click();
}
zoom();
setTimeout(zoom.bind(null, true), 1e3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.