"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Add custom layer to embe-leaflet

Add custom layer to embe-leaflet

Published on 2024-11-04
Browse:259

Add custom layer to embe-leaflet

The problem

ember-leaflet is a very popular addon from EmberJS ecosystem that allows a lot of flexibility.

But what if I want to extend it's functionality so that it can do even more? And what if I want it as a new yielded component directly from the s layers?

The solution

At first we will need our new component. For simplicity this component will just extend an existing layer component from the addon. Let's use the marker component and make it so that it just ignores location argument and sets a fake, hardcoded value:

// app/components/fake-marker-layer.gts

import MarkerLayer from 'ember-leaflet/components/marker-layer';

export default class FakeMarkerLayer extends MarkerLayer {
  get location() {
    return this.L.latLng(46.68, 7.85);
  }
}

After this we will need to register the component with ember-leaflet service:

// app/instance-initializers/leaflet.ts

import FakeMarkerLayer from '../components/fake-marker-layer';
import type Owner from '@ember/owner';

export function initialize(owner: Owner) {
  const emberLeafletService = owner.lookup('service:ember-leaflet');

  if (emberLeafletService) {
    emberLeafletService.registerComponent('fake-marker-layer', {
      as: 'fake-marker',
      component: FakeMarkerLayer,
    });
  }
}

export default {
  initialize,
};

And now we can use it:

import LeafletMap from 'ember-leaflet/components/leaflet-map';


Notes

You can read on this technique also on the official ember-leaflet documentation page.

Release Statement This article is reproduced at: https://dev.to/michalbryxi/add-custom-component-to-embe-leaflet-5475?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3