@graphhopper/directions-api-js-client
Version:
A Javascript Client for the GraphHopper Directions API
53 lines (44 loc) • 1.52 kB
JavaScript
GHInput = function (input, input2) {
this.set(input, input2);
};
GHInput.prototype.round = function (val, precision) {
if (precision === undefined)
precision = 1e6;
return Math.round(val * precision) / precision;
};
GHInput.prototype.setCoord = function (lat, lng) {
this.lat = this.round(lat);
this.lng = this.round(lng);
this.input = this.toString();
};
GHInput.isObject = function (value) {
var stringValue = Object.prototype.toString.call(value);
return (stringValue.toLowerCase() === "[object object]");
};
GHInput.isString = function (value) {
var stringValue = Object.prototype.toString.call(value);
return (stringValue.toLowerCase() === "[object string]");
};
GHInput.prototype.set = function (strOrObject, input2) {
if (input2) {
this.setCoord(strOrObject, input2);
return;
}
// either text or coordinates or object
this.input = strOrObject;
if (GHInput.isObject(strOrObject)) {
this.setCoord(strOrObject.lat, strOrObject.lng);
} else if (GHInput.isString(strOrObject)) {
var index = strOrObject.indexOf(",");
if (index >= 0) {
this.lat = this.round(parseFloat(strOrObject.substr(0, index)));
this.lng = this.round(parseFloat(strOrObject.substr(index + 1)));
}
}
};
GHInput.prototype.toString = function () {
if (this.lat !== undefined && this.lng !== undefined)
return this.lat + "," + this.lng;
return undefined;
};
module.exports = GHInput;