projects/common/lib/services/geocoder.service.ts
Properties |
|
Methods |
|
constructor(http: HttpClient)
|
||||||
Parameters :
|
Protected handleError | ||||||
handleError(error: HttpErrorResponse)
|
||||||
Parameters :
Returns :
any
|
lookup | ||||||
lookup(address: string)
|
||||||
Parameters :
Returns :
Observable<GeoAddressResult[]>
|
Protected processResponse | ||||||
processResponse(obj)
|
||||||
Formats the response from ADDRESS_URL, trimming irrelevant fields. This works for other requests for the same API too, however it may error out on some items if matchPrecisionNot is not set.
Parameters :
Returns :
GeoAddressResult[]
|
Protected generateUUID |
generateUUID()
|
Inherited from
AbstractHttpService
|
Defined in
AbstractHttpService:64
|
Returns :
any
|
Protected get | ||||||||||||
get(url, queryParams?: HttpParams)
|
||||||||||||
Inherited from
AbstractHttpService
|
||||||||||||
Defined in
AbstractHttpService:24
|
||||||||||||
Type parameters :
|
||||||||||||
Makes a GET request to the specified URL, using headers and HTTP options specified in their respective methods.
Parameters :
Returns :
Observable<T>
|
Protected Abstract handleError | ||||||
handleError(error: HttpErrorResponse)
|
||||||
Inherited from
AbstractHttpService
|
||||||
Defined in
AbstractHttpService:61
|
||||||
Handles all failed requests that throw either a server error (400/500) or a client error (e.g. lost internet).
Parameters :
Returns :
any
|
Protected post | ||||||
post(url, body)
|
||||||
Inherited from
AbstractHttpService
|
||||||
Defined in
AbstractHttpService:32
|
||||||
Type parameters :
|
||||||
Parameters :
Returns :
Observable<T>
|
Protected setupRequest | ||||||
setupRequest(observable: Observable
|
||||||
Inherited from
AbstractHttpService
|
||||||
Defined in
AbstractHttpService:40
|
||||||
Type parameters :
|
||||||
Parameters :
Returns :
Observable<T>
|
Protected uploadAttachment | ||||||||||||
uploadAttachment(relativeUrl: string, attachment: CommonImage)
|
||||||||||||
Inherited from
AbstractHttpService
|
||||||||||||
Defined in
AbstractHttpService:75
|
||||||||||||
Uploads an individual attachment. All you need to do is set the url. Note: urls often include UUIDs, so this must be an application decision.
Parameters :
Returns :
any
|
Protected _headers |
Type : HttpHeaders
|
Default value : new HttpHeaders()
|
Protected ADDRESS_URL |
Default value : `${this.BASE_URL}/addresses.json?`
|
Protected BASE_URL |
Type : string
|
Default value : 'https://geocoder.api.gov.bc.ca'
|
Protected Abstract _headers |
Type : HttpHeaders
|
Inherited from
AbstractHttpService
|
Defined in
AbstractHttpService:18
|
The headers to send along with every GET and POST. |
Protected logHTTPRequestsToConsole |
Type : boolean
|
Default value : false
|
Inherited from
AbstractHttpService
|
Defined in
AbstractHttpService:13
|
import { Injectable } from '@angular/core';
import { AbstractHttpService } from './abstract-api-service';
import { HttpClient, HttpHeaders, HttpErrorResponse, HttpParams } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable, throwError } from 'rxjs';
import { CANADA } from '../components/country/country.component';
export interface GeoAddressResult {
/** String from the API that includes street, city, province, and country. */
fullAddress: string;
city: string;
street: string;
// Set to defaults in response
country: string;
province: string;
}
@Injectable({
providedIn: 'root'
})
export class GeocoderService extends AbstractHttpService {
protected _headers: HttpHeaders = new HttpHeaders();
protected BASE_URL = 'https://geocoder.api.gov.bc.ca';
protected ADDRESS_URL = `${this.BASE_URL}/addresses.json?`;
constructor(protected http: HttpClient) {
super(http);
}
// https://github.com/bcgov/api-specs/blob/master/geocoder/geocoder-developer-guide.md
lookup(address: string): Observable<GeoAddressResult[]> {
const params = new HttpParams()
.set('minScore', '50')
.set('maxResults', '10')
.set('echo', 'true')
.set('interpolation', 'adaptive')
.set('addressString', address);
return this.get(this.ADDRESS_URL, params).pipe(map(this.processResponse));
}
/**
* Formats the response from ADDRESS_URL, trimming irrelevant fields.
*
* This works for other requests for the same API too, however it may error
* out on some items if matchPrecisionNot is not set.
*
* @param obj The response from ADDRESS_URL
*/
protected processResponse(obj): GeoAddressResult[] {
return obj.features.map(feature => {
const props = feature.properties;
const city = props.localityName;
// We get street just by trimming everything before city, more
// stable than looking for commas, etc.
const cityIndex = props.fullAddress.indexOf(`, ${city}`);
const street = props.fullAddress.slice(0, cityIndex);
const province = props.provinceCode;
const country = CANADA; // ALWAYS return Canada
return {
fullAddress: props.fullAddress.replace('--', '-'),
city,
street: street.replace('--', '-'),
province,
country
};
});
}
protected handleError(error: HttpErrorResponse) {
console.error('GeoCoder network error', { error });
return throwError('Geocoder error');
}
}