File

packages/location/src/lib/geocode.service.ts

Description

Wraps google maps api to handle geocode operations

Index

Methods

Constructor

constructor(mapLoader: MapsAPILoader, ngZone: NgZone)
Parameters :
Name Type Optional
mapLoader MapsAPILoader No
ngZone NgZone No

Methods

Public autocompleteAddress
autocompleteAddress(el)

Turns an input element to an maps autocomplete searchbar.

Parameters :
Name Optional
el No
Returns : Observable<any>
geocodeLatLng
geocodeLatLng(geocoder, location)

Reverse address lookup for a given location

Parameters :
Name Optional
geocoder No
location No
Returns : Promise<any>
Public getNearestAddress
getNearestAddress(location: literal type)

Returns the nearest address for a given location

Parameters :
Name Type Optional
location literal type No
Returns : Promise<Array<any>>
Public observeElement
observeElement(el)

Observes a given input element, transforming it into an autocomplete

Parameters :
Name Optional
el No
Returns : any
import { MapsAPILoader } from '@agm/core';
import { Injectable, NgZone } from '@angular/core';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { from } from 'rxjs';

/** declares google namespace */
declare var google: any;

/** Wraps google maps api to handle geocode operations */
@Injectable()
export class GeocodeService {
  constructor(private mapLoader: MapsAPILoader, private ngZone: NgZone) {}

  /** Observes a given input element, transforming it into an autocomplete */
  public observeElement(el) {
    return new Observable((observer) => {
      const autocomplete = new google.maps.places.Autocomplete(el, {
        types: ['address'],
      });
      autocomplete.addListener('place_changed', () => {
        this.ngZone.run(() => {
          // get the place result
          const place = autocomplete.getPlace();
          // verify result
          if (place.geometry === undefined || place.geometry === null) {
            return;
          }
          const coords = {
            latitude: place.geometry.location.lat(),
            longitude: place.geometry.location.lng(),
          };
          observer.next(coords);
          /* observer.complete(); */
        });
      });
    });
  }

  /** Turns an input element to an maps autocomplete searchbar. */
  public autocompleteAddress(el): Observable<any> {
    return from(this.mapLoader.load()).pipe(switchMap(() => this.observeElement(el)));
  }

  /** Reverse address lookup for a given location */
  geocodeLatLng(geocoder, location): Promise<any> {
    return new Promise((resolve, reject) => {
      geocoder.geocode({ location }, (results, status) => {
        if (status === 'OK') {
          resolve(results);
        } else {
          reject(status);
        }
      });
    });
  }

  /** Returns the nearest address for a given location */
  public getNearestAddress(location: { latitude: number; longitude: number }): Promise<Array<any>> {
    return this.mapLoader.load().then(() => {
      return this.geocodeLatLng(new google.maps.Geocoder(), {
        lat: location.latitude,
        lng: location.longitude,
      });
    });
  }
}

result-matching ""

    No results matching ""