如何访问第二个js文件中的对象传单地图

时间:2019-06-12 12:39:28

标签: javascript leaflet

我是Web开发的初学者,我的代码有问题。我正在使用Leaflet API在我的项目上添加地图,并且正在尝试使用OOP来实现。

我有一个名为Map.js的文件,另一个有名为Marker.js的文件:

  • Map.js:

    var Map = {
        lat: [47.218372, -1.553621],
    
        init(){
            var map = L.map('map').setView(this.lat, 14);
            L.tileLayer('https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png', { maxZoom: 20 }).addTo(map);
        }
    }
    

。 Marker.js:

class Marker {

  constructor(address, emplacement, name, bikeStands, avBikeStands, avBikes){
        this.address = address;
    this.emplacement = emplacement;
        this.name = name;
        this.bikeStands = bikeStands;
        this.avBikeStands = avBikeStands;
        this.avBikes = avBikes;
    // ICONS MAP ->
    var iconGood = L.icon({
        iconUrl : 'img/markGood.png',
        iconSize: [25, 41],
        iconAnchor: [12.5, 41],
        popupAnchor: [0, -41],
        shadowUrl: 'https://unpkg.com/leaflet@1.4.0/dist/images/marker-shadow.png',
    });
    var iconMed = L.icon({
        iconUrl : 'img/markMedium.png',
        iconSize: [25, 41],
        iconAnchor: [12.5, 41],
        popupAnchor: [0, -41],
        shadowUrl: 'https://unpkg.com/leaflet@1.4.0/dist/images/marker-shadow.png',
    });
    var iconLow = L.icon({
        iconUrl : 'img/markLow.png',
        iconSize: [25, 41],
        iconAnchor: [12.5, 41],
        popupAnchor: [0, -41],
        shadowUrl: 'https://unpkg.com/leaflet@1.4.0/dist/images/marker-shadow.png',
    });
    // <- ICONS MAP
    console.log("test +" + addMap)
        if(this.avBikes <= 0){
            this.marker = L.marker(this.emplacement, {icon: iconLow}).addTo(map);
        }
        else if(this.avBikes < 5){
            this.marker = L.marker(this.emplacement, {icon: iconMed}).addTo(map);
        }

        else{
            this.marker = L.marker(this.emplacement, {icon: iconGood}).addTo(map);
        }
  }

我的问题出在我的函数L.Marker中,变量“ map”不起作用,我不知道该怎么做。

0 个答案:

没有答案