All files LatLng.js

100% Statements 28/28
100% Branches 39/39
100% Functions 2/2
100% Lines 27/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                        9x               22x                 615x 205x 205x 205x               7x 7x               19x 19x 19x 19x               9x 9x 9x 9x                 20x 10x 10x 10x                 16x 8x 8x 8x                 29x               14x                   22x  
/**
 * @author Michael Duve <mduve@designmail.net>
 * @file represents latitude and longitude coordinates in a geographic coordinate system
 * @copyright Michael Duve 2016
 */
export class LatLng {
 
    /**
     * length of a latlng
     * @return {Number} length of latlng
     */
    get length() {
        return Math.sqrt(Math.pow(this.lat, 2) + Math.pow(this.lng, 2));
    }
 
    /**
     * gets a clone of this latlng
     * @return {LatLng} create a copy
     */
    get clone() {
        return LatLng.createFromLatLng(this);
    }
 
    /**
     * @constructor
     * @param  {Number} lat = 0 - representation of latitude
     * @param  {Number} lng = 0 - representation of longitude
     * @return {LatLng} instance of LatLng for chaining
     */
    constructor(lat = 0, lng = 0) {
        this.lat = lat;
        this.lng = lng;
        return this;
    }
 
    /**
     * calculates distance between this and specified latlng
     * @param  {LatLng} latlng = new LatLng() - specified latlng
     * @return {Number} distance
     */
    distance(latlng = new LatLng()) {
        return this.clone.substract(latlng).length;
    }
 
    /**
     * substract specified coord from this coordinate
     * @param  {LatLng} coord = new LatLng() - specified coordinate to substract from this coord
     * @return {LatLng} instance of LatLng for chaining
     */
    substract(coord = new LatLng()) {
        this.lat -= coord.lat;
        this.lng -= coord.lng;
        return this;
    }
 
    /**
     * add specified coord to this coordinate
     * @param  {LatLng} coord = new LatLng() - specified coordinate to add to this coord
     * @return {LatLng} instance of LatLng for chaining
     */
    add(coord = new LatLng()) {
        this.lat += coord.lat;
        this.lng += coord.lng;
        return this;
    }
 
    /**
     * divides a latlng with a given factor
     * @param  {Number} factorLat = 1 - factor to divide lat with
     * @param  {Number} factorLng = factorLat - factor to divide lng with
     * @return {LatLng} instance of LatLng for chaining
     */
    divide(factorLat = 1, factorLng = factorLat) {
        this.lat /= factorLat;
        this.lng /= factorLng;
        return this;
    }
 
    /**
     * multiplicates a latlng with a given factor
     * @param  {Number} factorLat = 1 - factor to multiplicate lat with
     * @param  {Number} factorLng = factorLat - factor to multiplicate lng with
     * @return {LatLng} instance of LatLng for chaining
     */
    multiply(factorLat = 1, factorLng = factorLat) {
        this.lat *= factorLat;
        this.lng *= factorLng;
        return this;
    }
 
    /**
     * checks if specified coord equals this coord
     * @param  {LatLng} coord - specified coord to check against
     * @return {Boolean} Returns if specified coord equals this coord
     */
    equals(coord) {
        return (coord instanceof LatLng) && this.lat === coord.lat && this.lng === coord.lng;
    }
 
    /**
     * converts a LatLng to string
     * @return {String} representing LatLng
     */
    toString() {
        return `(${this.lat}, ${this.lng})`;
    }
 
}
 
/**
 * Creates a LatLng from specified LatLng
 * @param  {LatLng} LatLng - specified LatLng
 * @return {LatLng} the LatLng specified
 */
LatLng.createFromLatLng = (latlng) => new LatLng(latlng.lat, latlng.lng);