public static String asMapLength ( MapWidget map , double length ) { double unitLength = map . getUnitLength ( ) ; double distance = length * unitLength ; String unit = "m" ; if ( map . getMapModel ( ) . getMapInfo ( ) . getDisplayUnitType ( ) == UnitType . METRIC ) { // Right now, the distance is expressed in meter. Switch to km? if ( distance > 10000 ) { distance /= METERS_IN_KM ; unit = "km" ; } } else if ( map . getMapModel ( ) . getMapInfo ( ) . getDisplayUnitType ( ) == UnitType . ENGLISH ) { if ( distance / METERS_IN_YARD > 10000 ) { // More than 10000 yard; switch to mile: distance = distance / METERS_IN_MILE ; unit = "mi" ; } else { distance /= METERS_IN_YARD ; // use yards. unit = "yd" ; } } else if ( map . getMapModel ( ) . getMapInfo ( ) . getDisplayUnitType ( ) == UnitType . ENGLISH_FOOT ) { if ( distance * FEET_IN_METER > FEET_IN_MILE ) { // More than 1 mile (5280 feet); switch to mile: distance = distance / METERS_IN_MILE ; unit = "mi" ; } else { distance *= FEET_IN_METER ; // use feet. unit = "ft" ; } } else if ( map . getMapModel ( ) . getMapInfo ( ) . getDisplayUnitType ( ) == UnitType . CRS ) { unit = "u" ; } String formatted = NumberFormat . getDecimalFormat ( ) . format ( distance ) ; return formatted + unit ; }
Distance formatting method . Requires a length as parameter expressed in the CRS units of the given map .