6

I would like to use Leaflet.js API with Sencha Touch 2.3.1 and leaflet.js gives this error:

Uncaught Error: Map container not found.

These links are included in index.html

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>

Here is my mainview code:

Ext.define('App.view.Main', {

    extend: 'Ext.Container',
    xtype: 'main',
    requires: [
        'App.view.MapView',
    ],

    config: {
        layout: 'card',
        items: [
        {   
            itemId: 'mapview',
            xtype: 'mapview',
            id : 'map'
        }
        ]
    }
});

This is the 'App.view.MapView' code:

Ext.define("App.view.MapView", {
  extend: 'Ext.Container',
  requires: ['Ext.device.Geolocation'],
  xtype: 'mapview',
  initialize: function(){

          var map = L.map('map').setView([47.36865, 8.539183], 13);
          var layer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

          map.on('click', function() {
            console.log('Click-Event on map / Time: ' + Date.now());
          });

  },
});

What am I doing wrong? Please help.

Andrea Casaccia
  • 4,802
  • 4
  • 29
  • 54
terreb
  • 1,417
  • 2
  • 23
  • 40

3 Answers3

13

Leaflet is searching for a DOM element, map, that isn't there (yet...).

This problem occurs when L.map('map') is called before the DOM has finished loading.

sfletche
  • 47,248
  • 30
  • 103
  • 119
5

Guessing the js was executed before the DOM was ready. Try wrapping your code in a function and setting window.onload equal to your function.

exaybachay
  • 51
  • 1
  • 1
2

Ok, seems I found a solution. I changed this line:

var map = L.map('map').setView([47.36865, 8.539183], 13);

to

var map = L.map(this.element.dom).setView([47.36865, 8.539183], 13);
terreb
  • 1,417
  • 2
  • 23
  • 40