Polymer leaflet maps distortion

对着背影说爱祢 提交于 2019-12-12 04:17:02

问题


I'm new with polymer and leaflet.js, when try load the map this is distortionated

my code

<link rel="import" href="../bower_components/polymer/polymer.html">

<script src="https://unpkg.com/leaflet@1.0.2/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.2/dist/leaflet.css" />

<dom-module id="my-view4">
  <template>
    <style is="custom-style">
    #map {position: absolute;
     top: 0;
     right: 0;
     bottom: 0;
     left: 0; }

    </style>
                <div id="map"></div>
  </template>

  <script>
    Polymer({
        is: 'my-view4',
        ready: function() {
            var map = L.map(this.$.map).
             setView([41.66, -4.72],
             15);

            L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
            maxZoom: 18
            }).addTo(map);

            L.marker([41.66, -4.71],{draggable: true}).addTo(map);

        }

    });
  </script>
</dom-module>

i try without polymer and leaflet work, but try with polymer and this error map


回答1:


Use attached callback instead of ready.

Polymer({
        is: 'my-view4',
        attached: function() {
            var map = L.map(this.$.map).
             setView([41.66, -4.72],
             15);

            L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
            maxZoom: 18
            }).addTo(map);

            L.marker([41.66, -4.71],{draggable: true}).addTo(map);

        }

The attached callback is called when the element is attached to the document.For more information on Polymer lifecycle callbacks take a look here.



来源:https://stackoverflow.com/questions/41154141/polymer-leaflet-maps-distortion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!