import { Injectable } from '@angular/core' ; import arcgisUtils = require ( 'esri/arcgis/utils' ) ; import esriBasemaps = require ( 'esri/basemaps' ) ; import Legend = require ( 'esri/dijit/Legend' ) ; import Search = require ( 'esri/dijit/Search' ) ; import { LegendOptions } from "esri" ; @ Injectable ( ) export class MapService { _basemaps : any [ ] ; createMap ( itemIdOrInfo , domNodeOrId , options ) { return arcgisUtils . createMap ( itemIdOrInfo , domNodeOrId , options ) . then ( response => { response . layerInfos = arcgisUtils . getLegendLayers ( response ) ; response . basemapName = this . getBasemapName ( response . map ) ; return response ; } ) ; } ; createSearch ( options , domNodeOrId ) { return new Search ( options , domNodeOrId ) ; } ; createLegend ( options , domNodeOrId ) { return new Legend ( options , domNodeOrId ) ; } ; getBasemaps ( ) { if ( ! this . _basemaps ) { this . _basemaps = Object . keys ( esriBasemaps ) . map ( ( name ) => { let basemap = esriBasemaps [ name ] ; basemap . name = name ; return basemap ; } ) ; } return this . _basemaps ; } getBasemapName ( map ) { let basemapName = map . getBasemap ( ) ; if ( basemapName ) { return basemapName ; } map . layerIds . some ( layerId => { const layerUrl = map . getLayer ( layerId ) . url ; return this . getBasemaps ( ) . some ( basemapDef => { return basemapDef . baseMapLayers . some ( basemapDefLayer => { const match = basemapDefLayer . url . toLowerCase ( ) === layerUrl . toLowerCase ( ) ; if ( match ) { basemapName = basemapDef . name ; } return match ; } ) ; } ) ; } ) ; return basemapName ; } clearBasemap ( map ) { if ( map . basemapLayerIds && map . basemapLayerIds . length > 0 ) { map . basemapLayerIds . forEach ( function ( lid ) { map . removeLayer ( map . getLayer ( lid ) ) ; } ) ; map . basemapLayerIds = [ ] ; } else { map . removeLayer ( map . getLayer ( map . layerIds [ 0 ] ) ) ; } } selectLayer ( response , selectedLayer ) { response . layerInfos . forEach ( layerId => { if ( selectedLayer . name === layerId . title ) layerId . layer . setVisibility ( ! selectedLayer . checked ) ; } ) ; } }