Skip to content

Commit

Permalink
fix(search-bar): Adding a pinpoint on locate XY by searchbar (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Aug 16, 2018
1 parent b74b910 commit 3380ccf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions projects/geo/src/lib/map/shared/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ export class IgoMap {
this.overlayDataSource.ol.addFeature(feature);
}

removeOverlayByID(id) {
if (this.overlayDataSource.ol.getFeatureById(id)) {
this.overlayDataSource.ol.removeFeature(
this.overlayDataSource.ol.getFeatureById(id)
);
}
}

clearOverlay() {
if (this.overlayDataSource && this.overlayDataSource.ol) {
this.overlayDataSource.ol.clear();
Expand Down
32 changes: 31 additions & 1 deletion projects/geo/src/lib/search/search-bar/search-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import { FloatLabelType } from '@angular/material';
import { Subject, Subscription } from 'rxjs';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';

import olFeature from 'ol/Feature';
import olPoint from 'ol/geom/Point';
import * as olproj from 'ol/proj';

import { IgoMap } from '../../map/shared/map';
import { MapService } from '../../map/shared/map.service';
import { FeatureService } from '../../feature/shared/feature.service';
import {
SourceFeatureType,
Expand Down Expand Up @@ -105,6 +111,7 @@ export class SearchBarComponent implements OnInit, OnDestroy {
private _searchIcon = false;

private readonly invalidKeys = ['Control', 'Shift', 'Alt'];
private locateID: string = 'locateXY';
private stream$ = new Subject<string>();
private stream$$: Subscription;
private selectedFeature$$: Subscription;
Expand All @@ -113,8 +120,13 @@ export class SearchBarComponent implements OnInit, OnDestroy {

@ViewChild('input') input: ElementRef;

get map(): IgoMap {
return this.mapService.getMap();
}

constructor(
private searchService: SearchService,
private mapService: MapService,
private featureService: FeatureService,
private changeDetectorRef: ChangeDetectorRef
) {}
Expand Down Expand Up @@ -172,12 +184,29 @@ export class SearchBarComponent implements OnInit, OnDestroy {
this.input.nativeElement.focus();
}

private addOverlay(coordinates: [number, number]) {
const geometry = new olPoint(
olproj.transform(coordinates, 'EPSG:4326', this.map.projection)
);
const extent = geometry.getExtent();
const feature = new olFeature({ geometry: geometry });
feature.setId(this.locateID);
// TODO: SETTING A NEW COLOR AND TEXT BASED ON PR 166
// feature.setStyle([this.map.setPointOverlayStyleWithParams('yellow', coordinates)]);
// https://github.com/infra-geo-ouverte/igo2-lib/
// blob/6d0e9a2a5d3fd2290339c123b674aca7ca9e7102/src/lib/map/shared/map.ts#L135
this.map.removeOverlayByID(this.locateID);
this.map.moveToExtent(extent);
this.map.addOverlay(feature);
}

private keyIsValid(key: string) {
return this.invalidKeys.indexOf(key) === -1;
}

private handleTermChanged(term: string) {
if (term !== undefined || term !== '') {
this.map.removeOverlayByID(this.locateID);
this.featureService.clear();
this.search.emit(term);
// tslint:disable-next-line:max-line-length
Expand All @@ -197,7 +226,8 @@ export class SearchBarComponent implements OnInit, OnDestroy {
}
xy = JSON.parse('[' + term + ']');
}
const r = this.searchService.locate(xy);
this.addOverlay(xy);
const r = this.searchService.locate(xy, this.map.getZoom());
if (r) {
r.filter(res => res !== undefined).map(res =>
res.subscribe(features =>
Expand Down

0 comments on commit 3380ccf

Please sign in to comment.