Browse Source

autocomplate封装完成

上海一张图
陈鹏飞 4 years ago
parent
commit
ff3e2fbb43
  1. 4
      src/app/gis-management/gis-labeling/gis-labeling.component.ts
  2. 4
      src/app/key-unit/router-gis/router-gis.component.ts
  3. 3
      src/app/searchComponent.service.ts
  4. 1
      src/modules/map/declare/component/SearchDownListPlugins/SearchDownList.html
  5. 2
      src/modules/map/declare/component/SearchDownListPlugins/SearchDownList.scss
  6. 46
      src/modules/map/declare/component/SearchDownListPlugins/SearchDownList.ts
  7. 35
      src/modules/map/declare/gaode-map.ts
  8. 68
      src/modules/map/declare/keda-map.ts
  9. 19
      src/modules/map/declare/map.d.ts

4
src/app/gis-management/gis-labeling/gis-labeling.component.ts

@ -1431,7 +1431,7 @@ export class GisLabelingComponent implements OnInit {
this.snackBar.open('未查询到终点坐标信息,请输入有效地址', '确定', config); this.snackBar.open('未查询到终点坐标信息,请输入有效地址', '确定', config);
return return
} else { } else {
this.routeGIS.search(this.startCoordinate, this.endCoordinate, this.routeGIS.search(this,this.startCoordinate, this.endCoordinate,
function (status, result) { function (status, result) {
if (status === 'complete') { if (status === 'complete') {
that.routes = result.routes[0] that.routes = result.routes[0]
@ -1454,7 +1454,7 @@ export class GisLabelingComponent implements OnInit {
}); });
// 根据起终点名称规划驾车导航路线 // 根据起终点名称规划驾车导航路线
this.routeGIS.search(this.startCoordinate, this.endCoordinate, this.routeGIS.search(this,this.startCoordinate, this.endCoordinate,
function (status, result) { function (status, result) {
if (status === 'complete') { if (status === 'complete') {
that.routes = result.routes[0] that.routes = result.routes[0]

4
src/app/key-unit/router-gis/router-gis.component.ts

@ -215,7 +215,7 @@ export class RouterGISComponent implements OnInit {
this.snackBar.open('未查询到终点坐标信息,请输入有效地址','确定',config); this.snackBar.open('未查询到终点坐标信息,请输入有效地址','确定',config);
return return
}else{ }else{
this.routeGIS.search(this.startCoordinate,this.endCoordinate, this.routeGIS.search(this,this.startCoordinate,this.endCoordinate,
function(status, result) { function(status, result) {
if (status === 'complete') { if (status === 'complete') {
that.routes = result.routes[0] that.routes = result.routes[0]
@ -242,7 +242,7 @@ export class RouterGISComponent implements OnInit {
policy: e==true? AMap.DrivingPolicy.LEAST_TIME : AMap.DrivingPolicy.REAL_TRAFFIC policy: e==true? AMap.DrivingPolicy.LEAST_TIME : AMap.DrivingPolicy.REAL_TRAFFIC
}); });
// 根据起终点名称规划驾车导航路线 // 根据起终点名称规划驾车导航路线
this.routeGIS.search(this.startCoordinate,this.endCoordinate, this.routeGIS.search(this,this.startCoordinate,this.endCoordinate,
function(status, result) { function(status, result) {
if (status === 'complete') { if (status === 'complete') {
that.routes = result.routes[0] that.routes = result.routes[0]

3
src/app/searchComponent.service.ts

@ -6,7 +6,7 @@ import { ApplicationRef, ComponentFactoryResolver, ComponentRef, EmbeddedViewRef
export class SearchService { export class SearchService {
constructor(private componentFactoryResolver: ComponentFactoryResolver, private applicationRef: ApplicationRef, private injector: Injector,) { } constructor(private componentFactoryResolver: ComponentFactoryResolver, private applicationRef: ApplicationRef, private injector: Injector,) { }
//angular 动态组件 //angular 动态添加组件
appendComponentToBody<T>(component: any): ComponentRef<T> { appendComponentToBody<T>(component: any): ComponentRef<T> {
//create a component reference //create a component reference
const componentRef = this.componentFactoryResolver.resolveComponentFactory(component).create(this.injector); const componentRef = this.componentFactoryResolver.resolveComponentFactory(component).create(this.injector);
@ -21,6 +21,7 @@ export class SearchService {
return componentRef as ComponentRef<T>; return componentRef as ComponentRef<T>;
} }
//删除组件
removeComponentFromBody(componentRef: ComponentRef<any>) { removeComponentFromBody(componentRef: ComponentRef<any>) {
this.applicationRef.detachView(componentRef.hostView); this.applicationRef.detachView(componentRef.hostView);
componentRef.destroy(); componentRef.destroy();

1
src/modules/map/declare/component/SearchDownListPlugins/SearchDownList.html

@ -1,4 +1,3 @@
<div class="content" [ngStyle]="{'left': leftCss,'top': topCss,'min-width': minWidthCss}" *ngIf="searchList.length"> <div class="content" [ngStyle]="{'left': leftCss,'top': topCss,'min-width': minWidthCss}" *ngIf="searchList.length">
<h1>I'm loaded</h1>
<div *ngFor="let item of searchList" (click)="select(item)" class="item">{{item.name}}</div> <div *ngFor="let item of searchList" (click)="select(item)" class="item">{{item.name}}</div>
</div> </div>

2
src/modules/map/declare/component/SearchDownListPlugins/SearchDownList.scss

@ -12,5 +12,5 @@
padding: 4px; padding: 4px;
} }
.item:hover { .item:hover {
background-color: #007aff; background-color: #7fb9f7;
} }

46
src/modules/map/declare/component/SearchDownListPlugins/SearchDownList.ts

@ -1,55 +1,53 @@
import { Component, OnInit, Inject } from '@angular/core'; import { Component, OnInit, Inject } from '@angular/core';
import { AutocompleteSelect } from '../../map'; import { AutocompleteSelect, HtmlRender } from '../../map';
type ClickHandler = (data: AutocompleteSelect) => void; type ClickHandler = (data: AutocompleteSelect) => void;
@Component({ @Component({
selector: 'SearchDownList', selector: 'SearchDownList',
templateUrl: './SearchDownList.html', templateUrl: './SearchDownList.html',
styleUrls: ['./SearchDownList.scss'] styleUrls: ['./SearchDownList.scss']
}) })
export class SearchDownList implements OnInit { export class SearchDownList implements OnInit {
private $input; constructor() {}
constructor() {
} public $input; //input
public searchList: AutocompleteSelect[] = []; //list public searchList: AutocompleteSelect[] = []; //list
public leftCss: string = '0px'; public leftCss: string = '0px';
public topCss: string = '0px'; public topCss: string = '0px';
public minWidthCss: string = '0px'; public minWidthCss: string = '0px';
private clickHandler: ClickHandler; private clickHandler: ClickHandler;
ngOnInit(): void { ngOnInit(): void {
window.onresize = () => {
if (this.$input) {
let Dom = this.$input.getBoundingClientRect()
this.leftCss = `${Dom.left + 3}px`
this.topCss = `${Dom.top + Dom.height + 3}px`
this.minWidthCss = `${Dom.width}px`
}
}
} }
setClickHander(handler: ClickHandler) { setClickHander(handler: ClickHandler) {
this.clickHandler = handler; this.clickHandler = handler;
} }
clearClickHander() { clearClickHander() {
this.clearClickHander = null; this.clearClickHander = null;
} }
init(elementId: string): void {
this.$input = document.getElementById(elementId);
//this.addDOMEvent();
}
// input 添加监听事件
addDOMEvent() {
// this.left = DOM.left init(inputElement: HtmlRender): void {
// this.top = DOM.top + DOM.height + 2 this.$input = inputElement
// this.minWidth = DOM.width let Dom = this.$input.getBoundingClientRect()
this.leftCss = `${Dom.left + 3}px`
this.topCss = `${Dom.top + Dom.height + 3}px`
this.minWidthCss = `${Dom.width}px`
} }
// 触发 select
select(e) { select(e) {
this.clearClickHander && this.clickHandler(e); this.clearClickHander && this.clickHandler(e);
this.searchList = []; this.searchList = [];
} }
watch() {
//TODO:修改leftCss ,topCss
}
testAddData() {
this.searchList.push({
name: "asfasdfasdf"
} as AutocompleteSelect);
}
} }

35
src/modules/map/declare/gaode-map.ts

@ -1,8 +1,5 @@
import { HtmlRender, ICircle, IIcon, IIconOptions, ILayer, ILngLat, IMap, IMapOptions, IMarker, IMarkerCluster, IMarkOptions, IMouseTool, IInfoWindow, IPixel, IRenderClusterMarker, ISelf, ISize, PixelRender, ITileLayer, IDriving, IDrivingOptions, IAutocomplete, IPlaceSearch, AutocompleteSearchCallback, AutocompleteSelectCallback, PlaceSearchCallback, AutocompleteSearch } from './map' import { HtmlRender, ICircle, IIcon, IIconOptions, ILayer, ILngLat, IMap, IMapOptions, IMarker, IMarkerCluster, IMarkOptions, IMouseTool, IInfoWindow, IPixel, IRenderClusterMarker, ISelf, ISize, PixelRender, ITileLayer, IDriving, IDrivingOptions, IAutocomplete, IPlaceSearch, AutocompleteSearchCallback, AutocompleteSelectCallback, PlaceSearchCallback, AutocompleteSearch, DrivingSearchCallback } from './map'
import MapTools from './map-tools'; import MapTools from './map-tools';
import * as global from 'globals';
import { SearchService } from '@src/app/searchComponent.service';
import { SearchDownList } from './component/SearchDownListPlugins/SearchDownList';
declare var AMap: any; declare var AMap: any;
declare var AMapUI: any; declare var AMapUI: any;
@ -369,8 +366,10 @@ export class GaodeDriving extends GaodeBasic implements IDriving {
this.self = new AMap.Driving(options) this.self = new AMap.Driving(options)
}) })
} }
search(posStart?: ILngLat, posEnd?: ILngLat, callback?: Function) { search(component: any, posStart: ILngLat, posEnd: ILngLat, callback: DrivingSearchCallback) {
return this.self.search(posStart.getArray(), posEnd.getArray(), callback) return this.self.search(posStart.getArray(), posEnd.getArray(), (status, result)=>{
callback.call(component,status,result)
})
} }
clear() { clear() {
return this.self.clear() return this.self.clear()
@ -379,36 +378,20 @@ export class GaodeDriving extends GaodeBasic implements IDriving {
export class GaodeAutocomplete extends GaodeBasic implements IAutocomplete { export class GaodeAutocomplete extends GaodeBasic implements IAutocomplete {
private component: any private component: any
private searchDownList: SearchDownList
constructor(options: any, component: any) { constructor(options: any, component: any) {
super(); super();
this.component = component this.component = component
AMap.plugin('AMap.AutoComplete', () => { AMap.plugin('AMap.AutoComplete', () => {
this.self = new AMap.Autocomplete(options) this.self = new AMap.Autocomplete(options)
}) })
this.searchDownList = global.injector.get<SearchService>(SearchService).appendComponentToBody(SearchDownList).instance;
this.searchDownList.init(options.input);
this.self.on('complete', (e) => {
console.log('search complete', e);
this.searchDownList.searchList = e.tips || [];
})
} }
on(eventName: string, callback: AutocompleteSelectCallback) { on(eventName: string, callback: AutocompleteSelectCallback) {
this.searchDownList.setClickHander(e => { return this.self.on(eventName, (event) => {
alert(e.name); callback.call(this.component, event)
callback(e); })
});
//$().addEventListener
// return this.self.on(eventName, (event) => {
// console.log('on event', event)
// callback.call(this.component, event)
// })
} }
search(address: string, callback: AutocompleteSearchCallback) { search(address: string, callback: AutocompleteSearchCallback) {
return this.self.search(address, (status, result: AutocompleteSearch) => { return this.self.search(address, (status, result) => {
this.searchDownList.searchList = result.tips || [];
callback.call(this.component, status, result) callback.call(this.component, status, result)
}) })
} }

68
src/modules/map/declare/keda-map.ts

@ -1,8 +1,7 @@
import { AutocompleteSearchCallback, AutocompleteSelectCallback, HtmlRender, IAutocomplete, ICircle, IDriving, IInfoWindow, ILayer, ILngLat, IMap, IMapOptions, IMarker, IMarkerCluster, IMarkOptions, IMouseTool, IPixel, IPlaceSearch, IRenderClusterMarker, ISelf, ITileLayer, PixelRender, PlaceSearchCallback } from './map' import { AutocompleteSearchCallback, AutocompleteSelectCallback, DrivingSearchCallback, HtmlRender, IAutocomplete, ICircle, IDriving, IInfoWindow, ILayer, ILngLat, IMap, IMapOptions, IMarker, IMarkerCluster, IMarkOptions, IMouseTool, IPixel, IPlaceSearch, IRenderClusterMarker, ISelf, ITileLayer, PixelRender, PlaceSearchCallback } from './map'
import * as ObjectID from 'bson-objectid';
import { SearchDownList } from './component/SearchDownListPlugins/SearchDownList'; import { SearchDownList } from './component/SearchDownListPlugins/SearchDownList';
import { ApplicationRef, ComponentFactoryResolver, Injector, ReflectiveInjector } from '@angular/core';
import { SearchService } from '@src/app/searchComponent.service'; import { SearchService } from '@src/app/searchComponent.service';
import * as ObjectID from 'bson-objectid';
import * as global from 'globals'; import * as global from 'globals';
declare var KMap: any; declare var KMap: any;
@ -295,15 +294,17 @@ export class KedaDriving extends KedaBasic implements IDriving { //路线导航
this.map = options.map as KeDaMap this.map = options.map as KeDaMap
options.policy === undefined ? this.policy = 0 : this.policy = options.policy options.policy === undefined ? this.policy = 0 : this.policy = options.policy
} }
clear() { search(component: any, posStart: ILngLat, posEnd: ILngLat, callback: DrivingSearchCallback) {
this.map.self.removeRoutePlanning({ id: this.id })
}
search(posStart?: ILngLat, posEnd?: ILngLat, callback?: Function) {
let params = { let params = {
startPoint: posStart.getArray(), startPoint: posStart.getArray(),
endPoint: posEnd.getArray(), endPoint: posEnd.getArray(),
error: () => { alert('路线规划失败!') }, error: () => { alert('路线规划失败!') },
ended: (res) => { this.id = res.data } ended: (res) => {
this.id = res.data
let router = { routes:[] }
console.log(res.data)
callback.call(component,'complete',router)
}
} }
if (this.policy === 0) { //快捷模式 if (this.policy === 0) { //快捷模式
this.map.self.createMinDistanceRoutePlanning(params) this.map.self.createMinDistanceRoutePlanning(params)
@ -311,27 +312,41 @@ export class KedaDriving extends KedaBasic implements IDriving { //路线导航
this.map.self.createMinTimeRoutePlanning(params) this.map.self.createMinTimeRoutePlanning(params)
} }
} }
clear() {
this.map.self.removeRoutePlanning({ id: this.id })
}
} }
export class KedaAutocomplete extends KedaBasic implements IAutocomplete { //Autocomplete 关键字搜索 export class KedaAutocomplete extends KedaBasic implements IAutocomplete { //Autocomplete 关键字搜索
private component: any private component: any;
private searchDownList: SearchDownList;
private $input: any;
constructor(options: any, component: any) { constructor(options: any, component: any) {
super(); super();
this.component = component this.component = component
const injector = Injector.create({ if (options && options.input != undefined) { //绑定input框 搜索事件
providers: this.searchDownList = global.injector.get<SearchService>(SearchService).appendComponentToBody(SearchDownList).instance;
[{ provide: SearchService, deps: [ComponentFactoryResolver, ApplicationRef, Injector] }] this.$input = document.getElementById(options.input);
}); this.searchDownList.init(this.$input)
injector.get<SearchService>(SearchService).appendComponentToBody(SearchDownList); this.$input.oninput = (e) =>{ //监听 input事件
if (options && options.input != undefined) { //绑定input框搜索事件 (this.component.map as KeDaMap).self.queryInfoByType({
this.inputListen(options.input); code: cityCode,
keyword: this.$input.value,
searchType: [],
callback: (res) => {
let list = res.data
this.searchDownList.searchList = list || []
} }
}) // search
} }
on(eventName: string, callback: AutocompleteSelectCallback) {
} }
inputListen(elementId: string) { }
on(eventName: string, callback: AutocompleteSelectCallback) {
this.searchDownList.setClickHander(e => {
console.log(e+'Keda')
callback.call(this.component,e);
});
} }
search(address: string, callback: AutocompleteSearchCallback) { search(address: string, callback: AutocompleteSearchCallback) {
(this.component.map as KeDaMap).self.queryInfoByType({ (this.component.map as KeDaMap).self.queryInfoByType({
@ -340,6 +355,7 @@ export class KedaAutocomplete extends KedaBasic implements IAutocomplete { //Aut
searchType: [], searchType: [],
callback: (res) => { callback: (res) => {
console.log(res) console.log(res)
callback.call(this.component,'complete',res);
} }
}) })
} }
@ -351,8 +367,16 @@ export class KedaPlaceSearch extends KedaBasic implements IPlaceSearch { //Place
super(); super();
this.component = component this.component = component
} }
search(text: string, callback: PlaceSearchCallback) { search(address: string, callback: PlaceSearchCallback) {
throw new Error('Method not implemented.'); (this.component.map as KeDaMap).self.queryInfoByType({
code: cityCode,
keyword: address,
searchType: [],
callback: (res) => {
console.log(res)
callback.call(this.component,'complete',res);
}
})
} }
} }

19
src/modules/map/declare/map.d.ts vendored

@ -104,9 +104,26 @@ export interface IDrivingOptions {
isOutline?: boolean isOutline?: boolean
} }
export class DrivingSearch { //Driving Search() 数据类型
distance: number
time: number
tolls_distance: number
steps: DrivingSearchSteps[]
routes:any[] //伪代码
}
export class DrivingSearchSteps { //Driving Search() Steps 数据类型
instruction:string
orientation:string
road: string
distance: number
time: number
tolls_distance: number
}
type DrivingSearchCallback = (status:string,searchData:DrivingSearch)=>void;
export interface IDriving extends ISelf { export interface IDriving extends ISelf {
clear(); clear();
search(posStart?: ILngLat, posEnd?: ILngLat, callback?:Function); search(component: any,posStart: ILngLat, posEnd: ILngLat, callback:DrivingSearchCallback);
} }
export class AutocompleteSearch { //Autocomplete Search() 数据类型 export class AutocompleteSearch { //Autocomplete Search() 数据类型

Loading…
Cancel
Save