Guest User

Untitled

a guest
Jul 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import { Directive, ElementRef, OnInit, Output, EventEmitter } from '@angular/core';
  2.  
  3. declare var google:any;
  4.  
  5. @Directive({
  6. selector: '[google-place]'
  7. })
  8. export class GooglePlacesDirective implements OnInit {
  9. @Output() onSelect: EventEmitter<any> = new EventEmitter();
  10. private element: HTMLInputElement;
  11.  
  12. constructor(elRef: ElementRef) {
  13. this.element = elRef.nativeElement;
  14. }
  15.  
  16. getFormattedAddress(place) {
  17. let location_obj = {};
  18. for (let i in place.address_components) {
  19. let item = place.address_components[i];
  20.  
  21. location_obj['formatted_address'] = place.formatted_address;
  22. if(item['types'].indexOf("locality") > -1) {
  23. location_obj['locality'] = item['long_name']
  24. } else if (item['types'].indexOf("administrative_area_level_1") > -1) {
  25. location_obj['admin_area_l1'] = item['short_name']
  26. } else if (item['types'].indexOf("street_number") > -1) {
  27. location_obj['street_number'] = item['short_name']
  28. } else if (item['types'].indexOf("route") > -1) {
  29. location_obj['route'] = item['long_name']
  30. } else if (item['types'].indexOf("country") > -1) {
  31. location_obj['country'] = item['long_name']
  32. } else if (item['types'].indexOf("postal_code") > -1) {
  33. location_obj['postal_code'] = item['short_name']
  34. }
  35.  
  36. }
  37. return location_obj;
  38. }
  39.  
  40. ngOnInit() {
  41. const autocomplete = new google.maps.places.Autocomplete(this.element);
  42. google.maps.event.addListener(autocomplete, 'place_changed', () => {
  43. this.onSelect.emit(this.getFormattedAddress(autocomplete.getPlace()));
  44. });
  45. }
  46.  
  47. }
Add Comment
Please, Sign In to add comment