import { SetPopupPositionAction, SelectMultipleLinksAction, SelectMultipleSitesAction } from '../actions/popupActions';
 import { Feature } from '../model/Feature';
 import { HighlightLinkAction, HighlightSiteAction, SetCoordinatesAction, SetStatistics } from '../actions/mapActions';
-import { addDistance, getUniqueFeatures } from '../utils/mapUtils';
+import { addDistance, getUniqueFeatures, increaseBoundingBox } from '../utils/mapUtils';
 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
 import connect, { IDispatcher, Connect } from '../../../../framework/src/flux/connect';
 import SearchBar from './searchBar';
 import { verifyResponse, IsTileServerReachableAction, handleConnectionError } from '../actions/connectivityAction';
 import ConnectionInfo from './connectionInfo'
-import { showIconLayers, addBaseLayers } from '../utils/mapLayers';
-import lamp from '../../icons/lamp.png';
-import apartment from '../../icons/apartment.png';
-import datacenter from '../../icons/datacenter.png';
-import factory from '../../icons/factory.png';
+import { showIconLayers, addBaseLayers, addBaseSources, addIconLayers } from '../utils/mapLayers';
 import Statistics from './statistics';
 import IconSwitch from './iconSwitch';
+import { addImages } from '../services/mapImagesService';
 
 type coordinates = { lat: number, lon: number, zoom: number }
 
 
         map.on('load', (ev) => {
 
-            addBaseLayers(map, this.props.selectedSite, this.props.selectedLink);
-            map.loadImage(
-                lamp,
-                function (error: any, image: any) {
-                    if (error) throw error;
-                    map.addImage('lamp', image);
-                });
-
-            map.loadImage(
-                datacenter,
-                function (error: any, image: any) {
-                    if (error) throw error;
-                    map.addImage('data-center', image);
-                });
-
-            map.loadImage(
-                apartment,
-                function (error: any, image: any) {
-                    if (error) throw error;
-                    map.addImage('house', image);
-                });
-
-                map.loadImage(
-                    factory,
-                    function (error: any, image: any) {
-                        if (error) throw error;
-                        map.addImage('factory', image);
-                    });               
+            addBaseSources(map, this.props.selectedSite, this.props.selectedLink);
+                
+            addImages(map, (result: boolean)=>{
+                if(map.getZoom()>11)
+                {
+                    addIconLayers(map, this.props.selectedSite?.properties.id)
+                }else{
+                    addBaseLayers(map, this.props.selectedSite, this.props.selectedLink);
+                }
+            });             
 
             const boundingBox = map.getBounds();
 
                 .then(result => verifyResponse(result))
                 .then(result => result.json())
                 .then(features => {
-                    if (map.getLayer('fibre-lines')) {
+                    if (map.getSource('lines')) {
                         (map.getSource('lines') as mapboxgl.GeoJSONSource).setData(features);
                     }
                 })
                 .catch(error => this.props.handleConnectionError(error));
 
 
-            fetch(`${URL_API}/sites/geoJson/${boundingBox.getWest()},${boundingBox.getSouth()},${boundingBox.getEast()},${boundingBox.getNorth()}`)
+                fetch(`${URL_API}/sites/geoJson/${boundingBox.getWest()},${boundingBox.getSouth()},${boundingBox.getEast()},${boundingBox.getNorth()}`)
                 .then(result => verifyResponse(result))
                 .then(result => result.json())
                 .then(features => {
-                    if (map.getLayer('points')) {
+                    if (map.getSource('points')) {
                         (map.getSource('points') as mapboxgl.GeoJSONSource).setData(features);
                     }
                 })
 
             } else { // data is shown as icons
 
-                const clickedLamps = getUniqueFeatures(map.queryRenderedFeatures(e.point, { layers: ['point-lamps'] }), "id");
-                const buildings = getUniqueFeatures(map.queryRenderedFeatures(e.point, { layers: ['point-building'] }), "id");
-                const houses = getUniqueFeatures(map.queryRenderedFeatures(e.point, { layers: ['point-data-center'] }), "id");
-                const factories = getUniqueFeatures(map.queryRenderedFeatures(e.point, { layers: ['point-factory'] }), "id");
-
-                const combinedFeatures = [...clickedLamps, ...buildings, ...houses, ...factories];
-
+                const clickedSites = getUniqueFeatures(map.queryRenderedFeatures(e.point, { layers: ['point-lamps', 'point-building', 'point-data-center', 'point-factory', 'point-remaining'] }), "id");
                 const clickedLines = getUniqueFeatures(map.queryRenderedFeatures([[e.point.x - 5, e.point.y - 5],
                 [e.point.x + 5, e.point.y + 5]], {
                     layers: ['microwave-lines', 'fibre-lines']
                 }), "id");
 
-                if (combinedFeatures.length > 0)
-                    this.showSitePopup(combinedFeatures, e.point.x, e.point.y);
+                if (clickedSites.length > 0)
+                    this.showSitePopup(clickedSites, e.point.x, e.point.y);
                 else if (clickedLines.length != 0) {
                     this.showLinkPopup(clickedLines, e.point.x, e.point.y);
                 }
 
             const currentUrl = window.location.href;
             const parts = currentUrl.split(URL_BASEPATH);
-            const detailsPath = parts[1].split("/details/");
+            if(parts.length>0){
 
-            if (detailsPath[1] !== undefined && detailsPath[1].length > 0) {
-                this.props.history.replace(`/${URL_BASEPATH}/${map.getCenter().lat.toFixed(4)},${map.getCenter().lng.toFixed(4)},${mapZoom.toFixed(2)}/details/${detailsPath[1]}`)
-            }
-            else {
-                this.props.history.replace(`/${URL_BASEPATH}/${map.getCenter().lat.toFixed(4)},${map.getCenter().lng.toFixed(4)},${mapZoom.toFixed(2)}`)
+                const detailsPath = parts[1].split("/details/");
+
+                if (detailsPath[1] !== undefined && detailsPath[1].length > 0) {
+                    this.props.history.replace(`/${URL_BASEPATH}/${map.getCenter().lat.toFixed(4)},${map.getCenter().lng.toFixed(4)},${mapZoom.toFixed(2)}/details/${detailsPath[1]}`)
+                }
+                else {
+                    this.props.history.replace(`/${URL_BASEPATH}/${map.getCenter().lat.toFixed(4)},${map.getCenter().lng.toFixed(4)},${mapZoom.toFixed(2)}`)
+                }
             }
+           
             
             //switch icon layers if applicable
             showIconLayers(map, this.props.showIcons, this.props.selectedSite?.properties.id);
                         map.setFilter('point-lamps', ['==', 'type', 'street lamp']);
                         map.setFilter('point-data-center', ['==', 'type', 'data center']);
                         map.setFilter('point-building', ['==', 'type', 'high rise building']);
-                        map.setFilter('point-factory', ['==', 'type', 'factory'])
+                        map.setFilter('point-factory', ['==', 'type', 'factory']);
 
                         if (this.props.selectedSite?.properties.type !== undefined) {
                             switch (this.props.selectedSite?.properties.type) {
                                     break;
                                 case 'factory':
                                     map.setFilter('point-factory', ["all", ['==', 'type', 'factory'], ['!=', 'id', this.props.selectedSite.properties.id]]);
-                                    break;
+                                    break;    
                             }
                         }
                     }
 
                     lastBoundingBox = bbox;
 
-                    const distance = map.getCenter().distanceTo(bbox.getNorthEast()); // radius of visible area (center -> corner) (in meters)
-
                     //calculate new boundingBox
-                    const increasedBoundingBox = addDistance(bbox.getSouth(), bbox.getWest(), bbox.getNorth(), bbox.getEast(), (distance / 1000) / 2)
+                    const increasedBoundingBox = increaseBoundingBox(map);
 
                     await this.draw('lines', `${URL_API}/links/geoJson/${increasedBoundingBox.west},${increasedBoundingBox.south},${increasedBoundingBox.east},${increasedBoundingBox.north}`);
                     await this.draw('points', `${URL_API}/sites/geoJson/${increasedBoundingBox.west},${increasedBoundingBox.south},${increasedBoundingBox.east},${increasedBoundingBox.north}`);
                     // bbox is contained in last one, do nothing
                     isLoadingInProgress = false;
 
-                } else { // bbox is not fully contained in old one, extend 
+                } else { // bbox is not fully contained in old one, extend
 
                     lastBoundingBox.extend(bbox);
 
 
 const fibreLinkColor = "#1154d9";
 const microwaveLinkColor="#039903";
 
+export const addBaseSources = (map: mapboxgl.Map, selectedPoint: Feature|null, selectedLine: Feature|null) =>{
+    
+
+    // make sure the sources don't already exist
+    // (if the networkmap app gets opened quickly within short time periods, the prior sources might not be fully removed)
+
+    if(!map.getSource("lines")){
+        
+        map.addSource('lines', {
+            type: 'geojson',
+            data: { type: "FeatureCollection", features: [] }
+        });
+    }
+    
+    if(!map.getSource("selectedLine"))
+    {
+        const features = selectedLine !== null ? [selectedLine] : [];
+        map.addSource('selectedLine', {
+            type: 'geojson',
+            data: { type: "FeatureCollection", features: features }
+        });
+    }
+
+    if(!map.getSource("points"))
+    {
+        map.addSource('points', {
+            type: 'geojson',
+            data: { type: "FeatureCollection", features: [] }
+        });
+    }
+
+    if(!map.getSource("selectedPoints"))
+    {
+        const selectedPointFeature = selectedPoint !== null ? [selectedPoint] : [];
+        map.addSource('selectedPoints', {
+            type: 'geojson',
+            data: { type: "FeatureCollection", features: selectedPointFeature }
+    
+        });
+    }
+
+    if(!map.getSource("alarmedPoints"))
+    {
+        map.addSource("alarmedPoints", {
+            type: 'geojson',
+            data: {type:"FeatureCollection", features:[]}
+        });
+    }
+}
 
 export const addBaseLayers = (map: mapboxgl.Map, selectedPoint: Feature|null, selectedLine: Feature|null) => {
-       
-    const boundingBox = map.getBounds();
-    map.addSource('lines', {
-        type: 'geojson',
-        data: { type: "FeatureCollection", features: [] }
+
+    addCommonLayers(map);
+
+    map.addLayer({
+        id: 'points',
+        source: 'points',
+        type: 'circle',
+        paint: {
+            'circle-color': '#11b4da',
+            'circle-radius': 7,
+            'circle-stroke-width': 1,
+            'circle-stroke-color': '#fff'
+        }
     });
 
-    const features = selectedLine !== null ? [selectedLine] : [];
+    map.addLayer({
+        id: 'selectedPoints',
+        source: 'selectedPoints',
+        type: 'circle',
+        paint: {
+            'circle-color': '#116bda',
+            'circle-radius': 9,
+            'circle-stroke-width': 1,
+            'circle-stroke-color': '#fff'
+        }
+    });
 
-    map.addSource('selectedLine', {
-        type: 'geojson',
-        data: { type: "FeatureCollection", features: features }
+    map.addLayer({
+        id: 'alarmedPoints',
+        source: 'alarmedPoints',
+        type: 'circle',
+        paint: {
+            'circle-color': '#CC0000',
+            'circle-radius': 9,
+            'circle-stroke-width': 1,
+            'circle-stroke-color': '#fff'
+        }
     });
+}
 
-    map.addSource('points', {
-        type: 'geojson',
-        data: { type: "FeatureCollection", features: [] }
+export const addIconLayers = (map: mapboxgl.Map, selectedSiteId?: string) =>{
+
+    addCommonLayers(map);
+    createIconLayers(map, selectedSiteId);
+}
+
+const createIconLayers =(map: mapboxgl.Map, selectedSiteId?: string) =>{
+    map.addLayer({
+        'id': 'point-lamps',
+        'type': 'symbol',
+        'source': 'points',
+        'layout': {
+            'icon-allow-overlap': true,
+            'icon-image': 'lamp',
+            'icon-size': 0.1
+
+        },
+        'filter': createFilter("street lamp", selectedSiteId),
+    });
+
+    map.addLayer({
+        'id': 'point-building',
+        'type': 'symbol',
+        'source': 'points',
+        'filter': createFilter("high rise building", selectedSiteId),
+        'layout': {
+            'icon-allow-overlap': true,
+            'icon-image': 'house',
+            'icon-size': 0.1
+        }
     });
 
-    const selectedPointFeature = selectedPoint !== null ? [selectedPoint] : [];
+    map.addLayer({
+        'id': 'point-data-center',
+        'type': 'symbol',
+        'source': 'points',
+        'filter': createFilter("data center", selectedSiteId),
+        'layout': {
+            'icon-allow-overlap': true,
+            'icon-image': 'data-center',
+            'icon-size': 0.1
+        } });
+
+        map.addLayer({
+            'id': 'point-factory',
+            'type': 'symbol',
+            'source': 'points',
+            'filter': createFilter("factory", selectedSiteId),
+            'layout': {
+                'icon-allow-overlap': true,
+                'icon-image': 'factory',
+                'icon-size': 0.2
+            }
+        });
 
+        //alarm layers
+
+        map.addLayer({
+            'id': 'point-lamps-alarm',
+            'type': 'symbol',
+            'source': 'alarmedPoints',
+            'layout': {
+                'icon-allow-overlap': true,
+                'icon-image': 'lamp-red',
+                'icon-size': 0.1
+
+            },
+            'filter': createFilter("street lamp"),
+        });
+
+        map.addLayer({
+            'id': 'point-building-alarm',
+            'type': 'symbol',
+            'source': 'alarmedPoints',
+            'filter': createFilter("high rise building"),
+            'layout': {
+                'icon-allow-overlap': true,
+                'icon-image': 'house-red',
+                'icon-size': 0.1
+            }
+        });
+
+        map.addLayer({
+            'id': 'point-data-center-alarm',
+            'type': 'symbol',
+            'source': 'alarmedPoints',
+            'filter': createFilter("data center"),
+            'layout': {
+                'icon-allow-overlap': true,
+                'icon-image': 'data-center_red',
+                'icon-size': 0.1
+            } });
+
+            map.addLayer({
+                'id': 'point-factory-alarm',
+                'type': 'symbol',
+                'source': 'alarmedPoints',
+                'filter': createFilter("factory"),
+                'layout': {
+                    'icon-allow-overlap': true,
+                    'icon-image': 'factory-red',
+                    'icon-size': 0.2
+                }
+            });
 
-    map.addSource('selectedPoints', {
-        type: 'geojson',
-        data: { type: "FeatureCollection", features: selectedPointFeature }
 
+
+    map.addLayer({
+        id: 'point-remaining',
+        source: 'points',
+        type: 'circle',
+        'filter': ['none', ['==', 'type', "high rise building"], ['==', 'type', "data center"], ['==', 'type', "factory"], ['==', 'type', "street lamp"] ],
+        paint: {
+            'circle-color': '#11b4da',
+            'circle-radius': 7,
+            'circle-stroke-width': 1,
+            'circle-stroke-color': '#fff'
+        }
+    });
+
+    map.addLayer({
+        'id': 'select-point-lamps',
+        'type': 'symbol',
+        'source': 'selectedPoints',
+        'layout': {
+            'icon-allow-overlap': true,
+            'icon-image': 'lamp',
+            'icon-size': 0.15
+
+        },
+        'filter': ['==', 'type', 'street lamp'],
     });
 
+    map.addLayer({
+        'id': 'select-point-buildings',
+        'type': 'symbol',
+        'source': 'selectedPoints',
+        'filter': ['==', 'type', 'high rise building'],
+        'layout': {
+            'icon-allow-overlap': true,
+            'icon-image': 'house',
+            'icon-size': 0.15
+        }
+    });
+
+    map.addLayer({
+        'id': 'select-point-data-center',
+        'type': 'symbol',
+        'source': 'selectedPoints',
+        'filter': ['==', 'type', 'data center'],
+        'layout': {
+            'icon-allow-overlap': true,
+            'icon-image': 'data-center',
+            'icon-size': 0.15
+        }
+    });
+
+
+    map.addLayer({
+        'id': 'select-point-factory',
+        'type': 'symbol',
+        'source': 'selectedPoints',
+        'filter': ['==', 'type', 'factory'],
+        'layout': {
+            'icon-allow-overlap': true,
+            'icon-image': 'factory',
+            'icon-size': 0.3
+        }
+    });
+}
+
+ const addCommonLayers = (map: mapboxgl.Map) =>{
+    
     map.addLayer({
         'id': 'microwave-lines',
         'type': 'line',
         },
         'filter': ['==', 'type', 'fibre']
     });
-
-
-
-    map.addLayer({
-        id: 'points',
-        source: 'points',
-        type: 'circle',
-        paint: {
-            'circle-color': '#11b4da',
-            'circle-radius': 7,
-            'circle-stroke-width': 1,
-            'circle-stroke-color': '#fff'
-        }
-    });
-
-    map.addLayer({
-        id: 'selectedPoints',
-        source: 'selectedPoints',
-        type: 'circle',
-        paint: {
-            'circle-color': '#116bda',
-            'circle-radius': 9,
-            'circle-stroke-width': 1,
-            'circle-stroke-color': '#fff'
-        }
-    });
-
-    map.addSource("alarmedPoints", {
-        type: 'geojson',
-        data: {type:"FeatureCollection", features:[]}
-    })
-
-    map.addLayer({
-        id: 'alarmedPoints',
-        source: 'alarmedPoints',
-        type: 'circle',
-        paint: {
-            'circle-color': '#CC0000',
-            'circle-radius': 9,
-            'circle-stroke-width': 1,
-            'circle-stroke-color': '#fff'
-        }
-    });
 }
 
 export const removeBaseLayers = (map: mapboxgl.Map) => {
     map.removeLayer("lines");
     map.removeLayer('selectedPoints');
     map.removeLayer('selectedLine');
+}
 
-    map.removeSource("points");
-    map.removeSource("lines");
-    map.removeSource('selectedPoints');
-    map.removeSource('selectedLine');
+const removeIconLayers = (map: mapboxgl.Map) =>{
+
+    map.removeLayer('point-building');
+    map.removeLayer('point-lamps');
+    map.removeLayer('point-data-center');
+    map.removeLayer('point-factory');
+    map.removeLayer('point-remaining');
+    map.removeLayer('select-point-data-center');
+    map.removeLayer('select-point-buildings');
+    map.removeLayer('select-point-lamps');
+    map.removeLayer('select-point-factory');
+    map.removeLayer('point-building-alarm');
+    map.removeLayer('point-lamps-alarm');
+    map.removeLayer('point-data-center-alarm');
+    map.removeLayer('point-factory-alarm');
 }
 
 let checkedLayers = false;
             map.removeLayer('points');
             map.setLayoutProperty('alarmedPoints', 'visibility', 'none');
             map.setLayoutProperty('selectedPoints', 'visibility', 'none');
+            createIconLayers(map,selectedSiteId);
+            //map.moveLayer('point-remaining','selectedPoints');
 
-            map.addLayer({
-                'id': 'point-lamps',
-                'type': 'symbol',
-                'source': 'points',
-                'layout': {
-                    'icon-allow-overlap': true,
-                    'icon-image': 'lamp',
-                    'icon-size': 0.1
-
-                },
-                'filter': createFilter("street lamp", selectedSiteId),
-            });
-
-            map.addLayer({
-                'id': 'point-building',
-                'type': 'symbol',
-                'source': 'points',
-                'filter': createFilter("high rise building", selectedSiteId),
-                'layout': {
-                    'icon-allow-overlap': true,
-                    'icon-image': 'house',
-                    'icon-size': 0.1
-                }
-            });
-
-            map.addLayer({
-                'id': 'point-data-center',
-                'type': 'symbol',
-                'source': 'points',
-                'filter': createFilter("data center", selectedSiteId),
-                'layout': {
-                    'icon-allow-overlap': true,
-                    'icon-image': 'data-center',
-                    'icon-size': 0.1
-                } });
-
-                map.addLayer({
-                    'id': 'point-factory',
-                    'type': 'symbol',
-                    'source': 'points',
-                    'filter': createFilter("factory", selectedSiteId),
-                    'layout': {
-                        'icon-allow-overlap': true,
-                        'icon-image': 'factory',
-                        'icon-size': 0.2
-                    }
-                });
-
-                //alarm layers
-
-                map.addLayer({
-                    'id': 'point-lamps-alarm',
-                    'type': 'symbol',
-                    'source': 'alarmedPoints',
-                    'layout': {
-                        'icon-allow-overlap': true,
-                        'icon-image': 'lamp-red',
-                        'icon-size': 0.1
-    
-                    },
-                    'filter': createFilter("street lamp"),
-                });
-    
-                map.addLayer({
-                    'id': 'point-building-alarm',
-                    'type': 'symbol',
-                    'source': 'alarmedPoints',
-                    'filter': createFilter("high rise building"),
-                    'layout': {
-                        'icon-allow-overlap': true,
-                        'icon-image': 'house-red',
-                        'icon-size': 0.1
-                    }
-                });
-    
-                map.addLayer({
-                    'id': 'point-data-center-alarm',
-                    'type': 'symbol',
-                    'source': 'alarmedPoints',
-                    'filter': createFilter("data center"),
-                    'layout': {
-                        'icon-allow-overlap': true,
-                        'icon-image': 'data-center_red',
-                        'icon-size': 0.1
-                    } });
-
-                    map.addLayer({
-                        'id': 'point-factory-alarm',
-                        'type': 'symbol',
-                        'source': 'alarmedPoints',
-                        'filter': createFilter("factory"),
-                        'layout': {
-                            'icon-allow-overlap': true,
-                            'icon-image': 'factory-red',
-                            'icon-size': 0.2
-                        }
-                    });
-
-
-
-            map.addLayer({
-                id: 'point-remaining',
-                source: 'points',
-                type: 'circle',
-                'filter': ['==', 'type', ''],
-                paint: {
-                    'circle-color': '#11b4da',
-                    'circle-radius': 7,
-                    'circle-stroke-width': 1,
-                    'circle-stroke-color': '#fff'
-                }
-            });
-
-            map.addLayer({
-                'id': 'select-point-lamps',
-                'type': 'symbol',
-                'source': 'selectedPoints',
-                'layout': {
-                    'icon-allow-overlap': true,
-                    'icon-image': 'lamp',
-                    'icon-size': 0.15
-
-                },
-                'filter': ['==', 'type', 'street lamp'],
-            });
-
-            map.addLayer({
-                'id': 'select-point-buildings',
-                'type': 'symbol',
-                'source': 'selectedPoints',
-                'filter': ['==', 'type', 'high rise building'],
-                'layout': {
-                    'icon-allow-overlap': true,
-                    'icon-image': 'house',
-                    'icon-size': 0.15
-                }
-            });
-
-            map.addLayer({
-                'id': 'select-point-data-center',
-                'type': 'symbol',
-                'source': 'selectedPoints',
-                'filter': ['==', 'type', 'data center'],
-                'layout': {
-                    'icon-allow-overlap': true,
-                    'icon-image': 'data-center',
-                    'icon-size': 0.15
-                }
-            });
-
-
-            map.addLayer({
-                'id': 'select-point-factory',
-                'type': 'symbol',
-                'source': 'selectedPoints',
-                'filter': ['==', 'type', 'factory'],
-                'layout': {
-                    'icon-allow-overlap': true,
-                    'icon-image': 'factory',
-                    'icon-size': 0.3
-                }
-            });
             }
         }
         }
     }
 }else{
     swapLayersBack(map);
-
 }
-    
 }
 
 export const swapLayersBack = (map: mapboxgl.Map) =>{
     checkedLayers=false;
-    if (map.getLayer('points') === undefined) {
 
-        map.setLayoutProperty('selectedPoints', 'visibility', 'visible');
-        map.setLayoutProperty('alarmedPoints', 'visibility', 'visible');
+    if(map.getLayer('selectedPoints') === undefined){
+        map.addLayer({
+            id: 'selectedPoints',
+            source: 'selectedPoints',
+            type: 'circle',
+            paint: {
+                'circle-color': '#116bda',
+                'circle-radius': 9,
+                'circle-stroke-width': 1,
+                'circle-stroke-color': '#fff'
+            }
+        });
+    }
 
+    if(map.getLayer('alarmedPoints') === undefined){
+        map.addLayer({
+            id: 'alarmedPoints',
+            source: 'alarmedPoints',
+            type: 'circle',
+            paint: {
+                'circle-color': '#CC0000',
+                'circle-radius': 9,
+                'circle-stroke-width': 1,
+                'circle-stroke-color': '#fff'
+            }
+        });
+    }
 
-        map.removeLayer('point-building');
-        map.removeLayer('point-lamps');
-        map.removeLayer('point-data-center');
-        map.removeLayer('point-factory');
-        map.removeLayer('point-remaining');
-        map.removeLayer('select-point-data-center');
-        map.removeLayer('select-point-buildings');
-        map.removeLayer('select-point-lamps');
-        map.removeLayer('select-point-factory');
-        map.removeLayer('point-building-alarm');
-        map.removeLayer('point-lamps-alarm');
-        map.removeLayer('point-data-center-alarm');
-        map.removeLayer('point-factory-alarm');
 
+    if (map.getLayer('points') === undefined) {
 
+        map.setLayoutProperty('selectedPoints', 'visibility', 'visible');
+        map.setLayoutProperty('alarmedPoints', 'visibility', 'visible');
+        removeIconLayers(map);
 
         map.addLayer({
             id: 'points',
             }
         });
 
-        map.moveLayer('points', map.getLayer('selectedPoints').id)
-
-
+        map.moveLayer('points', map.getLayer('selectedPoints').id);
     }
 }