按照本指南,我可以正确运行项目,现在我想为绘制的点添加一个事件。所以这是我的代码:
<!doctype html>
<html>
<head>
<title>PolymerVizCodelab</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="bower_components/google-map/google-map.html">
<link rel="import" href="bower_components/point-overlay/point-overlay.html">
<link rel="import" href="bower_components/paper-card/paper-card.html">
<link rel="import" href="bower_components/paper-slider/paper-slider.html">
<link rel="import" href="bower_components/iron-ajax/iron-ajax.html">
<link rel="stylesheet" href="styles.css">
</head>
<body unresolved>
<template is="dom-bind" id="t">
<google-map map="{{map}}" latitude="38.6" longitude="-95.8" zoom="5"
styles='[{"stylers":[{"saturation":-85}]},{"featureType":"water","stylers":[{"lightness":-20}]}]'
on-google-map-ready="setPointSize"
on-zoom-changed="setPointSize">
</google-map>
<point-overlay on-click="clickHandler" map="[[map]]" uniforms="{{uniforms}}" data="{{data}}" id="p">
</point-overlay>
<iron-ajax auto handle-as="json"
url="bower_components/point-overlay/tornadoes-1950-2014.json"
last-response="{{data}}">
</iron-ajax>
<!-- <paper-card elevation="2">
<paper-slider min="5" max="100" value="30"></paper-slider>
</paper-card> -->
</template>
<script>
var t = document.querySelector('#t');
t.setPointSize = function(e) {
this.uniforms.pointSize = Math.exp(0.3 * this.map.getZoom());
this.notifyPath('uniforms.pointSize', this.uniforms.pointSize);
};
function clickHandler(e) {
console.log("click");
}
</script>
</body>
</html>
但clickHandler
永远不会被解雇。
如何为地图上的每个点绑定和事件?
答案 0 :(得分:1)
clickHandler
需要成为dom-bind
模板的一部分(就像您使用setPointSize
一样)。改变这个:
function clickHandler(e) {
console.log("click");
}
到此:
t.clickHandler = function(e) {
console.log("click");
};
答案 1 :(得分:1)
不幸的是,point-overlay
不会公开任何事件,此外point-overlay
地图图层可供canvas-layer.js
用于在Google地图上显示数据,以防止鼠标事件触发事件监听器。
但是,对CanvasLayer
进行了一些修改后,它可以完成,如下所示:
在CanvasLayer
档案中:
1)注释阻止鼠标事件触发覆盖元素(canvas.style.pointerEvents = 'none';
)中的事件侦听器的行:
point-overlay
修改point-overlay.html
元素(overlayMouseTarget
文件)
2)将地图窗格设置为this.overlay.setPaneName("overlayMouseTarget");
click
3)为canvas元素引入var me = this;
google.maps.event.addDomListener(this.overlay.canvas, 'click', function() {
me.fire('overlay-click');
});
事件处理程序:
index.html
最后在clickHandler
档案中:
4)注册<point-overlay on-overlay-click="clickHandler" map="[[map]]" uniforms="{{uniforms}}" data="{{data}}" id="p">
</point-overlay>
事件处理程序
var t = document.querySelector('#t');
t.clickHandler = function(e) {
console.log('clicked');
};
,其中
{{1}}