import { IInfoWindow, ILngLat, IMap, IMapOptions, IMarker, IMarkOptions, IMouseTool, IPixel, ISelf, ITileLayer } from './map' import * as ObjectID from 'bson-objectid'; declare var KMap: any; class KedaBasic implements ISelf { self: any; discriminator: string = "ISelf"; } export class KeDaMap extends KedaBasic implements IMap { constructor(container: string, options: IMapOptions) { //地图初始化 super(); let that = this let opt = Object.assign({}, { containerId: container }, options) as any; opt.configUrl = "/assets/kmap/Kmap.config.json"; opt.targetCoordinateType = "WGS84"; let mapLayer = function () { if (opt.viewMode && opt.viewMode == "3D") { let data = { type: 'FeatureCollection', features: [ { type: 'Feature', geometry: { type: 'Point', coordinates: [121.495126354, 31.241993148] }, properties: { height: 30 } } ] }; that.self.add3DLayer({ data: data }); } } opt.onLoadMap = mapLayer this.self = new KMap(opt); } containerToLngLat(e: IPixel): ILngLat { let d = null; this.self.getGeoPointByPixel({ ePoint:e.self, callback:(e)=>{ d = new KeDaLngLat(e.data.lng,e.data.lat); } }) return d; } setAdministrativeAreaStyle(conponent: any, getData?: Function, setData?: Function) { //自定义 行政区划 样式 throw new Error('Method not implemented.'); } setBounds(zoom?: any, x?: any, y?: any, is?: boolean) { throw new Error('Method not implemented.'); } setFitView(options: any) { throw new Error('Method not implemented.'); } clearMap() { throw new Error('Method not implemented.'); } distance(a: number[], b: number[]) { throw new Error('Method not implemented.'); } getCity(callback: Function) { throw new Error('Method not implemented.'); } setZoomAndCenter(zoom: number, pos: number[]) { this.self.flyTo({ zoom: zoom, point: pos, }); } plugin(eventName: string[], callback: Function) { throw new Error('Method not implemented.'); } getBounds() { throw new Error('Method not implemented.'); } add(obj: any) { if(obj && obj.typeName === "KeDaMarker"){ let _marker = (obj as KeDaMarker); _marker.map = this; _marker.startBindEvent(); } if (obj == "卫星图层") { this.self.setImageLayer({ display: true }) } else if (obj == "路网图层") { this.self.setMapStyle({ solution: '8888' }); } } remove(obj: any) { if (obj == "卫星图层") { this.self.setImageLayer({ display: false }) } else if (obj == "路网图层") { this.self.setMapStyle({ solution: '9999' }); } } setCity(city: string) { let that = this if (city.includes("上海")) { window.setTimeout(function () { that.self.flyTo({ zoom: 9, point: [121.495126354, 31.241993148] }); }, 10000) } } setZoom(zoom: number) { this.self.zoomTo({ zoom: zoom }); } getZoom(): number { let num this.self.getZoom({ callback: (e) => { num = e.data; } }) return num } setCenter(pos: number[] | ILngLat) { let position = [] if (pos instanceof Array) { position = pos } else { position = [pos.lng, pos.lat]; } this.self.flyTo({ point: position }) } getCenter() { let center this.self.getCenter({ callback: function (data) { console.log(data); } }); return center } on(eventName: string, callback: Function): void { let eventMapProfile = { complete: "load", click: "click", rightclick: "contextmenu" }; this.self.addEventOnMap({ event: eventMapProfile[eventName], handler: callback }); } } export class KeDaPixel extends KedaBasic implements IPixel { constructor(x: number, y: number) { super(); this.x = x; this.y = y; this.self = [x, y]; } getArray(): number[] { return this. self; } x: number; y: number; } export class KeDaMarker extends KedaBasic implements IMarker { public map:KeDaMap; public typeName:string = "KeDaMarker"; constructor(options: IMarkOptions) { super(); let pos = [] if (options.position instanceof Array) { pos = options.position } else { pos = options.position.getArray() } let d = { url: '/assets/images/dingwei.png', point: pos, offset: options.offset? options.offset.self : null, ended: (res) => {this.id = res.data;} }; this.self = d; this._position = pos if (options.map) { this.map = options.map as KeDaMap this.map.self.addMarker(this.self); } } _position: number[]; setContent(html: string) { throw new Error('Method not implemented.'); } setPosition(x: number[]) { throw new Error('Method not implemented.'); } setMap() { throw new Error('Method not implemented.'); } get id(): string { return this.self.id } set id(str: string) { this.self.id = str } public bindObj=[]; // event on(eventName: string, callback: Function): void { this.bindObj.push({ eventName:eventName, callback:callback, }); this.startBindEvent(); } startBindEvent(){ if(this.map!==undefined){ this.bindObj.forEach((item,index)=>{ this.map.self.addEventOnMarkers({ selector: `#${this.id}`, event: item.eventName, handler: item.callback, }); }) this.bindObj = []; } } } export class KeDaLngLat extends KedaBasic implements ILngLat { constructor(lng: number, lat: number) { super(); this.lng = lng this.lat = lat this.self = this; } offset(x: number, y: number): ILngLat{ return x = 2 * Math.asin(Math.sin(Math.round(x)/12756274) / Math.cos(this.kT* Math.PI / 180)), x = this.KL + 180 *x / Math.PI, y = 2*Math.asin(Math.round(y)/12756274), new KeDaLngLat(x,y); } typeName="LngLat"; lng: number; lat: number; get KL():number{ return this.lng; } get kT():number{ return this.lat; } getArray(): number[] { return [this.lng,this.lat]; } } export class KedaInfoWindow extends KedaBasic implements IInfoWindow { //信息窗体 constructor(options) { super(); let opt = { htmlText: options.content, anchor: 'bottom', point: options.position, closeButton: true, } this.self = opt } open(map: IMap) { map.self.addPopup(this.self); } listen(html: any, event: string, callback: Function) { html.addEventListener(event,callback) } } export class KedaTileLayer extends KedaBasic implements ITileLayer { //图层切换 constructor() { super(); } Satellite() { //卫星图层 return "卫星图层" } RoadNet() { //路网图层 return "路网图层" } } export class KedaMouseTool extends KedaBasic implements IMouseTool { //地图工具 constructor(map: IMap) { super(); this.self = map.self } layerIds = []; layerIdAreas = []; rule(options: any) { let that = this this.self.measureDistance({ units: 'kilometers', callback: (result) => { var startText, sClass = that.self.mapType === 'AG' ? 'ag-popup-tools' : 'mm-popup-tools'; var result = result.data; if (!result.isEnd) { if (result.isStart) { that.self.addTexts({ points: [{ point: result.point, htmlText: '起点', class: sClass }], anchor: 'left', ended: function (res) { console.log(res); that.layerIds.push(res.data); startText = res.data; } }); } else { that.self.addTexts({ points: [{ point: result.point, htmlText: result.distance.toFixed(4) + 'km', class: sClass }], anchor: 'left', ended: function (res) { console.log(res); that.layerIds.push(res.data); } }); } } else { if (result.index >= 1) { if (result.isFailure) { that.layerIds.forEach((layerId) => { that.self.removeTextsByType({ textType: layerId }); }); return; } that.layerIds.push(result.layerId); if (that.self.mapType === 'BM') { that.self.addTexts({ points: [{ point: result.point, htmlText: '总距离:' + result.distance.toFixed(4) + 'km', class: sClass }], anchor: 'left', ended: function (res) { console.log(res); that.layerIds.push(res.data); } }); } } else { that.layerIds.forEach((layerId) => { that.self.removeTextsByType({ textType: layerId }); }); } } } }); } measureArea(options: any) { let that = this this.self.measureArea({ units: 'meters', callback: (result) => { var sClass = that.self.mapType === 'AG' ? 'ag-popup-tools' : 'mm-popup-tools'; var result = result.data; if (!result.isEnd) { if (result.index >= 2) { that.self.addTexts({ points: [{ point: result.point, htmlText: result.area + '平方米', class: sClass }], anchor: 'left', ended: function (res) { console.log(res); that.layerIdAreas.push(res.data); } }); } } else { if (result.index >= 2) { if (result.isFailure) { that.layerIdAreas.forEach((layerId) => { that.self.removeTextsByType({ textType: layerId }); }); return; } that.layerIdAreas.push(result.layerId); if (that.self.mapType === 'BM') { that.self.addTexts({ points: [{ point: result.point, htmlText: '总面积:' + result.area + '平方米', class: sClass }], anchor: 'left', ended: function (res) { console.log(res); that.layerIdAreas.push(res.data); } }); } } } } }); } close(isTrue: boolean) { if (isTrue) { this.self.clear(); for (var i in this.layerIds) { this.self.removeTextsByType({ textType: this.layerIds[i] }); } for (var i in this.layerIdAreas) { this.self.removeTextsByType({ textType: this.layerIdAreas[i] }); } this.layerIdAreas = []; this.layerIds = []; } } }