Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import * as L from 'leaflet';
  3.  
  4.  
  5.  
  6. class Nsmap {
  7.     private maCarte: any;
  8.  
  9.     constructor(lat:any, lon:any ,options:any, element:HTMLElement) {
  10.         this.initMap(lat, lon ,options, element);
  11.     }
  12.     initMap = (lat:any, lon:any ,options:any, element:HTMLElement) => {
  13.  
  14.         let zoom = 9;
  15.         if(options.hasOwnProperty('zoom')) {
  16.             zoom = options.zoom;
  17.         }
  18.         let maCarte = L.map(element).setView([lat, lon], zoom);
  19.  
  20.         maCarte.scrollWheelZoom.disable();
  21.         maCarte.once('focus', function() { maCarte.scrollWheelZoom.enable(); });
  22.         let enableScrollView = options.hasOwnProperty("enableScrollView") && ! options.disableScrollView;
  23.         console.log(enableScrollView);
  24.  
  25.         if(options.hasOwnProperty("markers")) {
  26.             for (let i = 0; i <  options.markers.length; i++) {
  27.                 let marker = options.markers[i];
  28.  
  29.                 L.marker([marker.lat, marker.long]).addTo(maCarte).bindPopup(marker.title)
  30.             }
  31.         }
  32.  
  33.         if(!enableScrollView) {
  34.             maCarte.on('click', function() {
  35.                 if (maCarte.scrollWheelZoom.enabled()) {
  36.                     maCarte.scrollWheelZoom.disable();
  37.                 }
  38.                 else {
  39.                     maCarte.scrollWheelZoom.enable();
  40.                 }
  41.             });
  42.         }
  43.  
  44.  
  45.         // 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
  46.         L.tileLayer('https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png', {
  47.             // Il est toujours bien de laisser le lien vers la source des données
  48.             attribution: 'données © <a href="//osm.org/copyright">OpenStreetMap</a>/ODbL - rendu <a href="//openstreetmap.fr">OSM France</a>',
  49.             minZoom: 1,
  50.             maxZoom: 20
  51.         }).addTo(maCarte);
  52.         this.maCarte = maCarte;
  53.     }
  54.  
  55.     addMarker = (markers: []) => {
  56.         for (let i = 0; i <  markers.length; i++) {
  57.             let marker = markers[i];
  58.  
  59.             L.marker([45.450444, 1.777218]).addTo(this.maCarte).bindPopup("test")
  60.         }
  61.     }
  62.  
  63. }
  64.  
  65.  
  66.  
  67.  
  68. Object.defineProperty(Element.prototype, 'nsmap', { value: function(lat:any, lon:any ,options:any) {
  69.         let element = this;
  70.         return new Nsmap(lat, lon, options, element);
  71.  
  72.     }
  73. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement