Skip to content

Instantly share code, notes, and snippets.

@ryanhanwu
Created October 22, 2014 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanhanwu/d39fa58180ff23c72012 to your computer and use it in GitHub Desktop.
Save ryanhanwu/d39fa58180ff23c72012 to your computer and use it in GitHub Desktop.
GeoConverter. Meter, KiloMeter, Radian
(function() {
var GeoConverter;
GeoConverter = (function() {
var EARTH_MILES = 24902,
EARTH_KM = 40076,
DEGREE_KM = EARTH_KM / 360,
DEGREE_MI = EARTH_MILES / 360,
DEGREE_M = DEGREE_KM * 1000,
MILE_METER = 1609.34,
RADIAN_DEGREE = 57.2957795;
// X_Y = Z means 1 X = Z * Y;
function GeoConverter(distance, unit) {
if (!(this instanceof GeoConverter)) {
return new GeoConverter(distance, unit);
}
this.distance = distance;
this.unit = unit;
}
GeoConverter.prototype.convertToDegree = function() {
if (this.unit === 'm') {
return this.distance / DEGREE_M;
} else if(this.unit === 'km') {
return this.distance / DEGREE_KM;
} else if(this.unit === 'radians' || this.unit === 'radian') {
return this.distance * RADIAN_DEGREE;
}
return NaN;
};
GeoConverter.prototype.convertToRadians = function() {
if (this.unit === 'degree') {
return this.distance / RADIAN_DEGREE;
}
return NaN;
};
GeoConverter.prototype.convertToMeter = function() {
if (this.unit === 'radians' || this.unit === 'radian') {
return this.distance * RADIAN_DEGREE * DEGREE_M;
}
return NaN;
};
return GeoConverter;
})();
if(typeof window !== "undefined" && window !== null) {
window.GeoConverter = GeoConverter;
} else {
module.exports = GeoConverter;
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment