mirror of
https://github.com/akveo/ngx-admin.git
synced 2025-12-16 23:40:14 +01:00
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { Component, ElementRef, EventEmitter, NgZone, OnInit, Output, ViewChild } from '@angular/core';
|
|
import { PositionModel } from '../entity/position.model';
|
|
|
|
@Component({
|
|
selector: 'ngx-search',
|
|
templateUrl: './search.component.html',
|
|
})
|
|
export class SearchComponent implements OnInit {
|
|
|
|
@Output()
|
|
public readonly positionChanged: EventEmitter<PositionModel> = new EventEmitter<PositionModel>();
|
|
|
|
@ViewChild('search', { static: true })
|
|
public searchElementRef: ElementRef;
|
|
|
|
constructor(private readonly ngZone: NgZone) {}
|
|
|
|
ngOnInit() {
|
|
const autocomplete = new google.maps.places.Autocomplete(
|
|
this.searchElementRef.nativeElement, { types: ['address'] },
|
|
);
|
|
|
|
autocomplete.addListener('place_changed', () => {
|
|
this.ngZone.run(() => {
|
|
// get the place result
|
|
const place: google.maps.places.PlaceResult = autocomplete.getPlace();
|
|
|
|
// verify result
|
|
if (place.geometry === undefined || place.geometry === null) {
|
|
return;
|
|
}
|
|
|
|
this.positionChanged.emit(new PositionModel(
|
|
place.geometry.location.lat(),
|
|
place.geometry.location.lng(),
|
|
));
|
|
});
|
|
});
|
|
}
|
|
}
|