File

projects/common/lib/services/geocoder.service.ts

Extends

AbstractHttpService

Index

Properties
Methods

Constructor

constructor(http: HttpClient)
Parameters :
Name Type Optional
http HttpClient No

Methods

Protected handleError
handleError(error: HttpErrorResponse)
Parameters :
Name Type Optional
error HttpErrorResponse No
Returns : any
lookup
lookup(address: string)
Parameters :
Name Type Optional
address string No
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 :
Name Optional Description
obj No

The response from ADDRESS_URL

Returns : GeoAddressResult[]
Protected generateUUID
generateUUID()
Inherited from AbstractHttpService
Returns : any
Protected get
get(url, queryParams?: HttpParams)
Inherited from AbstractHttpService
Type parameters :
  • T

Makes a GET request to the specified URL, using headers and HTTP options specified in their respective methods.

Parameters :
Name Type Optional Description
url No

Target URL to make the GET request

queryParams HttpParams Yes
Returns : Observable<T>
Protected Abstract handleError
handleError(error: HttpErrorResponse)
Inherited from AbstractHttpService

Handles all failed requests that throw either a server error (400/500) or a client error (e.g. lost internet).

Parameters :
Name Type Optional
error HttpErrorResponse No
Returns : any
Protected post
post(url, body)
Inherited from AbstractHttpService
Type parameters :
  • T
Parameters :
Name Optional
url No
body No
Returns : Observable<T>
Protected setupRequest
setupRequest(observable: Observable)
Inherited from AbstractHttpService
Type parameters :
  • T
Parameters :
Name Type Optional
observable Observable<any> No
Returns : Observable<T>
Protected uploadAttachment
uploadAttachment(relativeUrl: string, attachment: CommonImage)
Inherited from AbstractHttpService

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 :
Name Type Optional Description
relativeUrl string No

URL to hit, must include UUIDs of application and CommonImage

attachment CommonImage No

CommonImage to upload

Returns : any

Properties

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

The headers to send along with every GET and POST.

Protected logHTTPRequestsToConsole
Type : boolean
Default value : false
Inherited from AbstractHttpService
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');
    }
}

result-matching ""

    No results matching ""