按照本指南,我可以正确运行项目,现在我想为绘制的点添加一个事件。这是我的代码:

<!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从未触发。

如何为地图上的每个点绑定和事件?

最佳答案

clickHandler必须是dom-bind模板的一部分(就像您对setPointSize所做的一样)。更改此:

function clickHandler(e) {
  console.log("click");
}


对此:

t.clickHandler = function(e) {
  console.log("click");
};

关于javascript - polymer 点叠加点击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40226660/

10-10 22:13