# Aurelia 2 Leaflet Maps (afterAttach problem)

**URL:** <https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650>\
**Category:** vNext\
**Created:** [July 17, 2020, 11:16am UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650 "2020-07-17T11:16:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 17, 2020, 11:16am UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/1 "2020-07-17T11:16:48Z")

</div>

Hi all,

Enjoying exploring Aurelia 2. When porting an app from 1 to 2 ([this app](https://oileain.netlify.app/)) - ran into early difficulties attempting to initialise at third party component (leaflet maps). Here is a minimal Aurelia 1 example that works fine:

- [https://github.com/edeleastar/leaflet-demo-au-1](https://github.com/edeleastar/leaflet-demo-au-1)

In the above, successfully creating a map component associated with a div element on the view in the attached() lifecycle look like this:

```auto
  map: LeafletMap;

  attached() {
    this.map = new LeafletMap(this.mapDescriptor);
  }

```

This is a minimal Au 2 version of the same application:

- [https://github.com/edeleastar/leaflet-demo-au-2](https://github.com/edeleastar/leaflet-demo-au-2)

The equivalent initialisation

```auto
  map: LeafletMap;

  afterAttach() {
    this.map = new LeafletMap(this.mapDescriptor);
  }

```

However, the above yields exceptions:

```auto
leaflet-src.js:4115 Uncaught Error: Map container not found.
    at NewClass._initContainer (leaflet-src.js:4115)
    at NewClass.initialize (leaflet-src.js:3149)
    at new NewClass (leaflet-src.js:301)
    at Object.createMap [as map] (leaflet-src.js:4740)
    at new LeafletMap (leaflet-map.ts:18)
    at MyApp.afterAttach (my-app.ts:23)
    at Controller.afterAttach (controller.js:317)
    at AttachedQueue.process (lifecycle.js:231)
    at AttachedQueue.end (lifecycle.js:190)
    at Controller.attachCustomElement (controller.js:580)

```

I have tried all of the other lifecycle methods to no effect.

The element in the view is defined like this in both applications:

```auto
<div id="${mapDescriptor.id}" style="height:${mapDescriptor.height}px"></div>

```

I also tried various combinations of a hard coded id in the view to no effect. It seems that the element is not available on the DOM when the leaflet component attempts to create itself?

Any help appreciated - and I understand if this is still too early in Au2 to be a valid application.

Regards  
Eamonn

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [July 17, 2020, 12:25pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/2 "2020-07-17T12:25:52Z")

</div>

Shouldn’t this:

> [@edel020](#):
>
> ```auto
> afterAttach() {
> this.map = new LeafletMap(this.mapDescriptor);
> }
> 
> ```

Be:

```auto
afterAttach() {
    this.map = new LeafletMap(this.mapDescriptor.id); //<- id added
  }

```

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 17, 2020, 12:56pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/3 "2020-07-17T12:56:35Z")

</div>

Thanks for the suggestion - but the constructor is expecting a LeafletMapDescriptor -

- [https://github.com/edeleastar/leaflet-demo-au-2/blob/master/src/services/leaflet-map.ts](https://github.com/edeleastar/leaflet-demo-au-2/blob/master/src/services/leaflet-map.ts)

from which the Id is extracted. I am using a wrapper around the leaflet objects as in the above source.

Regards  
Eamon

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [July 17, 2020, 1:11pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/4 "2020-07-17T13:11:34Z")

</div>

Ah I see now your using a different version, I just use the leafletjs map directly, well, not directly I created a custom component to use it.

There I just use:

```auto
<template>
<require from="leaflet/dist/leaflet.css"></require>
<div id="mapid" style="height: 100%"></div>
</template>

```

and in attached()

```auto
    attached()
    {
        this.map = L.map('mapid').setView([0, 0], 2);
    }

```

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 17, 2020, 1:16pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/5 "2020-07-17T13:16:17Z")

</div>

I think I am doing more or less the same as you in my leaflet wrapper - but your example might be Aurelia 1 I think? Have you tried Leaflet in Aurelia 2 (using afterAttach instead)? This is where I am having trouble.  
Thanks

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [July 17, 2020, 1:39pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/6 "2020-07-17T13:39:13Z")

</div>

I just created a dumber gist from my custom element and it seems to init just fine.  
The map doesn’t show because I didn’t add in the tile server stuff.

[https://gist.dumber.app/?gist=30d5dff05eab14b6f5c732a3454d96e1](https://gist.dumber.app/?gist=30d5dff05eab14b6f5c732a3454d96e1)

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 17, 2020, 1:42pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/7 "2020-07-17T13:42:53Z")

</div>

Thanks!  
They issue may be in my wrapper - back to the drawing board for the moment.  
Thanks for your time  
Eamonn

---

<div class="post-metadata">

**Author:** ![atpw25](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@atpw25](https://discourse.aurelia.io/u/atpw25)\
**Post date:** [July 17, 2020, 4:03pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/8 "2020-07-17T16:03:51Z")

</div>

Just tried the au2 demo. The map is showing up fine without error in chrome and brave. I extended it a bit to add markers and everything seem to work.

Did not know about ArcGIS Online service for satellite view. Very useful to know. Thanks.

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 19, 2020, 6:45am UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/9 "2020-07-19T06:45:35Z")

</div>

Thanks @atpw25 - yes it now works! I think I had errors in my leaflet wrapper.  
Thanks for the ArcGIS recommendation.

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 23, 2020, 7:37am UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/10 "2020-07-23T07:37:39Z")

</div>

Hi all,

thanks for comments and help to date.

While a leaflet map from the ‘main’ page is successfully created - I am not having much luck replicating same in a ‘component’ - particularly a component triggered by the new au2 router. This is illustrated here:

> **[edeleastar/leaflet-demo-au-2](https://github.com/edeleastar/leaflet-demo-au-2)**
>
> Contribute to edeleastar/leaflet-demo-au-2 development by creating an account on GitHub.

The map displays fine in the my-app component. However, the identical code in routes/home fails.

This is currently commented out in routes/home.ts:

```auto
export class Home implements IViewModel {
  ...
  map: LeafletMap;

  afterAttach() {
    //this.map = new LeafletMap(this.mapDescriptor);
  }

```

But when included, container not found exception thrown from leaflet

```auto
Uncaught (in promise) Error: Map container not found.
    at NewClass._initContainer (entry-bundle.js:39606)
    at NewClass.initialize (entry-bundle.js:38640)
    ...

```

Perhaps a lifecycle issue ? Not sure.

Any help/observations appreciated.

Eamonn

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [July 23, 2020, 1:59pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/11 "2020-07-23T13:59:19Z")

</div>

It appears to me that the document object leaflet is using is the global document before Aurelia 2 does its magic of composing.

When I trace through the Leaflet initialization to the point where it is doing a document.getElementById() everything is correct up to that point (i.e. the map id passed is available and correct). However, the document is just the raw index.html as viewed under document.body.innerHtml.

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 23, 2020, 3:27pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/12 "2020-07-23T15:27:58Z")

</div>

Thanks - and this is in “afterAttach”, where the DOM should be ready for this type of component?

Other attempts to update the Dom (via values in the view model) seem to work ok.

Perhaps too early AU2 for third party components in the views loaded by the new router?

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [July 23, 2020, 6:27pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/13 "2020-07-23T18:27:56Z")

</div>

I tried to get it to fail in dumber calling document.getElementId directly, and wasn’t able to get it to fail…  
[https://gist.dumber.app/?gist=b05534a798a3b2e865f0540fce04d35f](https://gist.dumber.app/?gist=b05534a798a3b2e865f0540fce04d35f)

The same with a local project (not using Leaflet…

However, when I tried with a simplified leaflet app it failed. Still playing with it.

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [July 23, 2020, 7:02pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/14 "2020-07-23T19:02:18Z")

</div>

So my local app with routing is working now. (not exactly sure what I did, or if I did anything)

Here is the simplified leaflet element I used.

leaflet-map.html

```auto
<import from="leaflet/dist/leaflet.css"></import>
<div id="mapid" style="height: 400px; width: 400px;"></div>

```

leaflet-map.ts

```auto
import * as L from 'leaflet';

export class LeafletMap
{
    map: L.Map;

    constructor() { console.log("create LeafletMap"); }

    afterAttach()
    {
            let el = document.getElementById("mapid");
            console.log("LeaftletMap", el);

            this.map = L.map('mapid').setView([0, 0], 2);

            L.tileLayer(
                "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}",
                {
                    attribution:
                        "Tiles &copy; Esri &mdash; Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community",
                }
            ).addTo(this.map);
    }
}

```

routed to page content

```auto
<import from="leaflet-map"></import>

<h1>The about page.</h1>

<leaflet-map></leaflet-map>

```

---

<div class="post-metadata">

**Author:** ![atpw25](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@atpw25](https://discourse.aurelia.io/u/atpw25)\
**Post date:** [July 23, 2020, 7:18pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/15 "2020-07-23T19:18:02Z")

</div>

This seems to only happen if router is involved. I think it may be related to the issue below.

> <https://github.com/aurelia/aurelia/issues/841>
>
> 🐛 Bug Report
> In my afterAttach function Im trying to get an HTMLElement(ById). It is empty so it seems the afterAttach function...

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 24, 2020, 7:31am UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/16 "2020-07-24T07:31:12Z")

</div>

Hi again,

I have reconfigured my demo to implement your leaflet-map element precisely (discarding my wrapper):

> **[edeleastar/leaflet-demo-au-2](https://github.com/edeleastar/leaflet-demo-au-2)**
>
> Contribute to edeleastar/leaflet-demo-au-2 development by creating an account on GitHub.

Still getting:

```auto
create LeafletMap
leaflet-map.ts:19 LeaftletMap null
leaflet-src.js:4115 Uncaught (in promise) Error: Map container not found.
    at NewClass._initContainer (leaflet-src.js:4115)
   ...
   at Controller.attach (controlle

```

I have even tried a delay as suggested in

> <https://github.com/aurelia/aurelia/issues/841>
>
> 🐛 Bug Report
> In my afterAttach function Im trying to get an HTMLElement(ById). It is empty so it seems the afterAttach function...

The delay doesn’t seem to have an effect.

Quite puzzling - perhaps we need to ‘await’ the bug above to be resolved?

Thanks again for your efforts  
Eamonn

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [July 24, 2020, 2:04pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/17 "2020-07-24T14:04:32Z")

</div>

Here is a repo with the sample I have working locally here.

> **[airboss001/Aurelia2-LeafletMap](https://github.com/airboss001/Aurelia2-LeafletMap)**
>
> Test project using vanilla 'npx makes aurelia' project and adding leaflet custom element - airboss001/Aurelia2-LeafletMap

---

<div class="post-metadata">

**Author:** ![atpw25](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@atpw25](https://discourse.aurelia.io/u/atpw25)\
**Post date:** [July 25, 2020, 10:00am UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/18 "2020-07-25T10:00:52Z")

</div>

From my brief tests using either versions (with or without leaflet wrapper), adding the `await` statement in `afterAttach()` allow the map to render.

```auto
async afterAttach() {
    await new Promise(resolve => setTimeout(resolve));
    let el = document.getElementById("mapid");
    console.log("LeaftletMap", el);
    ... 
 }

```

Although, it does caused another error, `Map container is already initialized.`. But that is a different issue.

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 26, 2020, 7:44am UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/19 "2020-07-26T07:44:40Z")

</div>

Thanks!  
Most helpful - and I can confirm it works as expected.

---

<div class="post-metadata">

**Author:** ![edel020](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/edel020/32/1342_2.png) [@edel020](https://discourse.aurelia.io/u/edel020)\
**Post date:** [July 29, 2020, 4:32pm UTC](https://discourse.aurelia.io/t/aurelia-2-leaflet-maps-afterattach-problem/3650/20 "2020-07-29T16:32:39Z")

</div>

Hi all,

thanks for your help on getting Aurelia 2 up and running. The app is more or less complete now:

- [https://oileain-2.netlify.app/](https://oileain-2.netlify.app/)

Source: [https://github.com/edeleastar/oileain-2](https://github.com/edeleastar/oileain-2)

The Aurelia 1 version is here:

- [https://oileain.netlify.app](https://oileain.netlify.app)

Source : [https://github.com/edeleastar/oileain](https://github.com/edeleastar/oileain)

It was an interesting experience - apart from the afterAttach issue, the port was straightforward, with a modest code reduction overall.

Thanks again  
Eamonn
