Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as L from 'leaflet';
- class Nsmap {
- private maCarte: any;
- constructor(lat:any, lon:any ,options:any, element:HTMLElement) {
- this.initMap(lat, lon ,options, element);
- }
- initMap = (lat:any, lon:any ,options:any, element:HTMLElement) => {
- let zoom = 9;
- if(options.hasOwnProperty('zoom')) {
- zoom = options.zoom;
- }
- let maCarte = L.map(element).setView([lat, lon], zoom);
- maCarte.scrollWheelZoom.disable();
- maCarte.once('focus', function() { maCarte.scrollWheelZoom.enable(); });
- let enableScrollView = options.hasOwnProperty("enableScrollView") && ! options.disableScrollView;
- console.log(enableScrollView);
- if(options.hasOwnProperty("markers")) {
- for (let i = 0; i < options.markers.length; i++) {
- let marker = options.markers[i];
- L.marker([marker.lat, marker.long]).addTo(maCarte).bindPopup(marker.title)
- }
- }
- if(!enableScrollView) {
- maCarte.on('click', function() {
- if (maCarte.scrollWheelZoom.enabled()) {
- maCarte.scrollWheelZoom.disable();
- }
- else {
- maCarte.scrollWheelZoom.enable();
- }
- });
- }
- // Leaflet ne récupère pas les cartes (tiles) sur un serveur par défaut. Nous devons lui préciser où nous souhaitons les récupérer. Ici, openstreetmap.fr
- L.tileLayer('https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png', {
- // Il est toujours bien de laisser le lien vers la source des données
- attribution: 'données © <a href="//osm.org/copyright">OpenStreetMap</a>/ODbL - rendu <a href="//openstreetmap.fr">OSM France</a>',
- minZoom: 1,
- maxZoom: 20
- }).addTo(maCarte);
- this.maCarte = maCarte;
- }
- addMarker = (markers: []) => {
- for (let i = 0; i < markers.length; i++) {
- let marker = markers[i];
- L.marker([45.450444, 1.777218]).addTo(this.maCarte).bindPopup("test")
- }
- }
- }
- Object.defineProperty(Element.prototype, 'nsmap', { value: function(lat:any, lon:any ,options:any) {
- let element = this;
- return new Nsmap(lat, lon, options, element);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement