Skip to content

Instantly share code, notes, and snippets.

@sepulchered
Last active December 11, 2015 13:29
Show Gist options
  • Save sepulchered/4608020 to your computer and use it in GitHub Desktop.
Save sepulchered/4608020 to your computer and use it in GitHub Desktop.
Simple latitude/longitude object. It uses fraction 1/1000 of minute, but you can easily change code to use seconds instead of it. Value can be set in 3 different ways. You can provide float number and this object will get degrees, minutes and fractions. You can pass object with corresponding values and finally string (as in maskedValueRe variabl…
function LatitudeLongitude(lat){
this.direction = 1;
this.degrees = 0;
this.minutes = 0;
this.fraction = 0;
this.fValue = 0;
this.latitude = (lat != null) ? lat : true;
this.maxValue = ( this.latitude ) ? 90 : 180;
this.minValue = -this.maxValue;
}
LatitudeLongitude.prototype.setValue = function(lonlat){
if ( !isNaN( lonlat ) ){
this.convertFromVal(lonlat);
} else if ( typeof lonlat == 'string' ){
this.convertFromStr(lonlat);
} else {
this.convertFromObj(lonlat);
}
};
LatitudeLongitude.prototype.convertFromVal = function(val){
this.direction = ( val >= 0 ) ? 1 : -1;
this.degrees = Math.floor( val * this.direction ) ;
this.minutes = Math.floor( (val * this.direction - parseFloat(this.degrees) ) * 60 ) ;
this.fraction = (val * this.direction - this.degrees - ( this.minutes / 60 )) * 6000 ;
this.fraction = this.fraction.toPrecision(4);
this.setVal();
};
LatitudeLongitude.prototype.convertFromObj = function(obj){
this.direction = (obj.direction) ? obj.direction : 1;
this.degrees = (obj.degrees) ? obj.degrees : 0;
this.minutes = (obj.minutes) ? obj.minutes : 0;
this.fraction = (obj.fraction) ? obj.fraction : 0;
this.setVal();
};
LatitudeLongitude.prototype.convertFromStr = function(st){
var maskedValueRe = new RegExp('^\\s*(\\d{1,3})°\\s*(\\d{1,2})′\\s*(\\d+\\.?\\d*)(″?)\\s*(N|S|E|W)$'),
results = maskedValueRe.exec(st);
if ( results ){
this.latitude = ( st.indexOf('N') >= 0 || st.indexOf( 'S' ) >= 0 );
this.direction = ((results[5] == 'N') || (results[5] == 'E')) ? 1 : -1;
this.degrees = parseFloat( results[1] );
this.minutes = parseFloat( results[2] );
this.fraction = parseFloat( results[3] ).toPrecision(4);
this.setVal();
} else {
this.setDefaults();
}
};
LatitudeLongitude.prototype.setDefaults = function(){
this.direction = 1;
this.degrees = 0;
this.minutes = 0;
this.fraction = 0;
this.fValue = 0;
this.latitude = true;
};
LatitudeLongitude.prototype.setVal = function(){
var add_min = (this.fraction - (this.fraction % 6000)) / 6000;
this.fraction = this.fraction % 6000;
this.minutes = this.minutes + add_min;
this.degrees = this.degrees + (this.minutes - (this.minutes % 60)) / 60;
this.minutes = this.minutes % 60;
if (this.degrees >= this.maxValue()){
this.degrees = this.maxValue();
this.minutes = 0;
this.fraction = 0;
}
if (this.degrees <= this.minValue()){
this.degrees = this.minValue();
this.minutes = 0;
this.fraction = 0;
}
this.fValue = ((this.fraction / 6000) + (this.minutes / 60) + this.degrees) * this.direction;
};
LatitudeLongitude.prototype.getTextValue = function(){
return '' + this.degrees + "° " + this.minutes + "′ " + this.fraction.toPrecision(4) + ' ' + this.getDirTextValue();
};
LatitudeLongitude.prototype.getDirTextValue = function(){
if ( this.latitude ){
return (this.direction > 0) ? 'N' : 'S';
} else {
return (this.direction > 0) ? 'E' : 'W';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment