Guest User

Untitled

a guest
Feb 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. import {Component, Input, OnInit} from '@angular/core';
  2. import {ElementRef, NgZone, ViewChild} from '@angular/core';
  3. import {FormControl} from '@angular/forms';
  4. import {} from 'googlemaps';
  5. import {MapsAPILoader} from '@agm/core';
  6. import {CoreService} from './services/core.service';
  7. import {Observable} from 'rxjs/Observable';
  8. import {debounceTime, switch, fromEvent}'rxjs/operators';
  9.  
  10. @Component({
  11. selector: 'app-search',
  12. templateUrl: './search.component.html',
  13. styleUrls: ['./search.component.scss']
  14. })
  15. export class SearchComponent implements OnInit {
  16. public latitude: number;
  17. public longitude: number;
  18. public searchControl: FormControl;
  19. @ViewChild('search') public searchElementRef: ElementRef;
  20.  
  21. public constructor(
  22. private _core: CoreService,
  23. private _el: ElementRef,
  24. private mapsAPILoader: MapsAPILoader,
  25. private _ngZone: NgZone,
  26. ) {}
  27.  
  28. public ngOnInit(): void {
  29. // some default hardcoded values
  30. this.latitude = 39.8282;
  31. this.longitude = -98.5795;
  32.  
  33. this.searchControl = new FormControl();
  34.  
  35. // example of connecting google autocomplete to input with promises
  36. this.mapsAPILoader.load().then(() => {
  37. const autocomplete: google.maps.places.Autocomplete = new google.maps.places.Autocomplete(
  38. this.searchElementRef.nativeElement, {
  39. componentRestrictions: {country: 'ch'},
  40. types: ['address']
  41. });
  42.  
  43. autocomplete.addListener('place_changed', () => {
  44. this._ngZone.run(() => {
  45. const place: google.maps.places.PlaceResult = autocomplete.getPlace();
  46. this._core.savePlace(place);
  47. if (place.geometry === undefined || place.geometry === null) {
  48. return;
  49. }
  50.  
  51. this.latitude = place.geometry.location.lat();
  52. this.longitude = place.geometry.location.lng();
  53. });
  54. });
  55. });
  56.  
  57. // example of RxJS usage with some kind of an instant search
  58. Observable.fromEvent(this._el.nativeElement, 'keyup')
  59. .pipe(
  60. .map((e: any) => e.target.value),
  61. .filter((text: string) => text.length > 1),
  62. .debounceTime(250),
  63. .map(() => this._core.getZip(this.latitude, this.longitude)),
  64. .switch()
  65. )
  66. .subscribe(
  67. (zip: Zip) => {
  68. this._core.saveZip(zip);
  69. },
  70. (err: Error) => {
  71. this._core.handleError(err);
  72. }
  73. );
  74. }
  75. }
Add Comment
Please, Sign In to add comment