File

src/app/services/geolocation/geolocation.service.ts

Description

A service to work with geolocation stuff

Example

Index

Properties
Methods

Constructor

constructor(http: HttpClient)

Constructor

Parameters :
Name Type Optional Description
http HttpClient

Http Client

Methods

Public calculateGpsDistance
calculateGpsDistance(latA: number, lonA: number, latB: number, lonB: number)

calculates distance between two locations

Parameters :
Name Type Optional Description
latA number

latA

lonA number

lonA

latB number

latB

lonB number

lonB

Returns : number
Public degreesToRadians
degreesToRadians(degrees: number)

Calculate degrees to radiant

Parameters :
Name Type Optional Description
degrees number

Degree

Returns : number
getBrowserLocation
getBrowserLocation()

Returns the browser location

Returns : Promise<any>
getCoordinatesFromAdress
getCoordinatesFromAdress(adress: Address)

Get location by address from the Google Maps Api

Parameters :
Name Type Optional Description
adress Address

Adress

Returns : Promise<any>
getCoordinatesFromZip
getCoordinatesFromZip(zip: string)

Get location by zip

Parameters :
Name Type Optional Description
zip string
Returns : Promise<any>

Properties

Private log
log:
Default value : Log.create('GeolocationService')

Logger

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Log } from 'ng2-logger';

import { environment } from '../../../environments/environment';
import { Address } from '../../interfaces/address';

/**
 * A service to work with geolocation stuff
 * @author Daniel Sogl, Tim Krießler
 */
@Injectable()
export class GeolocationService {
  /** Logger */
  private log = Log.create('GeolocationService');

  /**
   * Constructor
   * @param  {HttpClient} http Http Client
   */
  constructor(private http: HttpClient) {}

  /**
   * Get location by address from the Google Maps Api
   * @param  {Address} adress Adress
   * @returns {Promise<any>}
   */
  getCoordinatesFromAdress(adress: Address): Promise<any> {
    return this.http
      .get(
        `https://maps.googleapis.com/maps/api/geocode/json?address=${
          adress.zip
        }+${adress.street}+${adress.streetnumber}&key=${environment.agmKey}`
      )
      .toPromise();
  }

  /**
   * Get location by zip
   * @param  {string} zip
   * @returns {Promise<any}
   */
  getCoordinatesFromZip(zip: string): Promise<any> {
    return this.http
      .get(
        `https://maps.googleapis.com/maps/api/geocode/json?address=${zip},+Germany&key=${
          environment.agmKey
        }`
      )
      .toPromise();
  }

  /**
   * Returns the browser location
   * @returns {Promise<any>}
   */
  getBrowserLocation(): Promise<any> {
    return new Promise(function(resolve, reject) {
      navigator.geolocation.getCurrentPosition(resolve, reject);
    });
  }

  /**
   * calculates distance between two locations
   * @param  {number} latA latA
   * @param  {number} lonA lonA
   * @param  {number} latB latB
   * @param  {number} lonB lonB
   * @returns {number}
   */
  public calculateGpsDistance(
    latA: number,
    lonA: number,
    latB: number,
    lonB: number
  ): number {
    const earthRadiusKm = 6371;

    const dLat = this.degreesToRadians(latB - latA);
    const dLon = this.degreesToRadians(lonB - lonA);

    latA = this.degreesToRadians(latA);
    latB = this.degreesToRadians(latB);

    const a =
      Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(latA) * Math.cos(latB);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return earthRadiusKm * c;
  }

  /**
   * Calculate degrees to radiant
   * @param  {number} degrees Degree
   * @returns {number}
   */
  public degreesToRadians(degrees: number): number {
    return degrees * Math.PI / 180;
  }
}

results matching ""

    No results matching ""