標籤:not 方式 satellite and type 做了 init style normal
最近一個人外出旅行 順便在那座城市買了一本關於JS設計模式的書 供無聊的時候閱讀
卻發現小有收穫 旅遊回來以後立馬寫下心得 做了一個地圖 並以此總結此次旅行的經過的地方
嘗試1:
嘗試寫JS類
1 var TianDiTu = function (lat, lng, zoom) { 2 if (this instanceof TianDiTu) { 3 var zoomSize = { maxZoom: 18, minZoom: 5 }, 4 normalm = L.tileLayer.chinaProvider(‘TianDiTu.Normal.Map‘, zoomSize), 5 normala = L.tileLayer.chinaProvider(‘TianDiTu.Normal.Annotion‘, zoomSize), 6 imgm = L.tileLayer.chinaProvider(‘TianDiTu.Satellite.Map‘, zoomSize), 7 imga = L.tileLayer.chinaProvider(‘TianDiTu.Satellite.Annotion‘, zoomSize); 8 9 this.normal = L.layerGroup([normalm, normala]);10 this.image = L.layerGroup([imgm, imga]);11 this.center = [lat, lng];12 this.zoom = zoom;13 } else {14 return new TianDiTu(lat, lng, zoom);15 }16 };17 18 var tdt = TianDiTu(34.618129, 112.454059, 12);
嘗試2:
嘗試將調用地圖的方式放入原廠模式:
1 var CreateMap = function () { 2 if (!(this instanceof CreateMap)) 3 return new CreateMap(); 4 } 5 CreateMap.prototype = { 6 init: function () { 7 map = L.map("map", { 8 center: tdt.center, 9 zoom: tdt.zoom,10 layers: [tdt.normal]11 });12 13 return this;14 },15 createControl: function () {16 var baseLayers = {17 "地圖": tdt.normal,18 "影像": tdt.image,19 }20 21 var overlayLayers = {22 }23 24 L.control.layers(baseLayers, overlayLayers).addTo(map);25 }26 }27 28 var createMap = new CreateMap();29 createMap.init().createControl();
js 設計模式學習(1)