Calculating distance between 2 points based on latitude and longitude
► JavaScript
function getDistance(Lat1, Long1, Lat2, Long2, Unit)
{
   var x, KM
   x = Math.acos((Math.sin(degToRads(Lat1)) * Math.sin(degToRads(Lat2)) + Math.cos(degToRads(Lat1)) * Math.cos(degToRads(Lat2)) * Math.cos(Math.abs((degToRads(Long2))-(degToRads(Long1))))))
   KM = 1.852 * 60.0 * ((x/Math.PI)*180)
   switch(Unit.toUpperCase())
   {
      case "M":
         return (KM / 1.609344)
      break
      case "N":
          return (KM / 1.852)
      break
      default:
         return KM
   }
}
function degToRads(deg)
{
   return (parseFloat(deg * Math.PI / 180))
}
	
► VBScript

const pi = 3.14159265358979323846

function GetDistance(Lat1, Long1, Lat2, Long2, Unit)

	dim result
	
	result = (sin(DegToRads(Lat1)) * sin(DegToRads(Lat2)) + cos(DegToRads(Lat1)) * cos(DegToRads(Lat2)) * cos(abs((DegToRads(long2))-(DegToRads(long1)))))
	result = atn((sqr(1-x^2))/x)
	
	GetDistance = 1.852 * 60.0 * ((result/pi)*180)
	
	select case ucase(Unit)
		case "M"      
	    	GetDistance = GetDistance / 1.609344
		case "N"
	    	GetDistance = GetDistance / 1.852
	end select

end function

function DegToRads(Deg)

	DegToRads = cdbl(Deg * pi / 180)

end function

	
► C#
using System;

namespace GlobalCitiesDatabase
{
	/// <summary>
	/// DistanceCalculator class contains static methods & fields
	/// for calculating distances according to latitude & longitude data.
	/// </summary>
	public class DistanceCalculator
	{
		/// <summary>
		/// KM_IN_MILE - Provides a constant to convert km. distance
		/// to miles (value=1.609344)
		/// </summary>
		public const double KM_IN_MILE=1.609344;
		/// <summary>
		/// KM_IN_NORDIC_MILE - Provides a constant to convert km. distance
		/// to nordic miles (value=1.852)
		/// </summary>
		public const double KM_IN_NORDIC_MILE=1.852;

		
		private DistanceCalculator()
		{
			//
			// No need to instantiate this class
			//
		}

		/// <summary>
		/// Returns the distance between 2 points, specified by
		/// latitude & longitude coordinates.
		/// </summary>
		/// <param name="latitude1">The latitude of the 1st point</param>
		/// <param name="longitude1">The longitude of the 1st point</param>
		/// <param name="latitude2">The latitude of the 2nd point</param>
		/// <param name="longitude2">The longitude of the 2nd point</param>
		/// <param name="unit">The DistanceUnit in which to return the result in</param>
		/// <returns>The distance in the specified unit</returns>

		public static double GetDistance(double latitude1, double longitude1, double latitude2, double longitude2, DistanceUnit unit)
		{
			double x, km;
			x = Math.Acos((Math.Sin(DegreesToRadians(latitude1)) * Math.Sin(DegreesToRadians(latitude2)) + Math.Cos(DegreesToRadians(latitude1)) * Math.Cos(DegreesToRadians(latitude2)) * Math.Cos(Math.Abs((DegreesToRadians(longitude2))-(DegreesToRadians(longitude1))))));
   
			km = KM_IN_NORDIC_MILE * 60.0 * ((x/Math.PI)*180);
	
			switch(unit)
			{
				case DistanceUnit.Mile:
					return (km / KM_IN_MILE);
				case DistanceUnit.NordicMile:
					return (km / KM_IN_NORDIC_MILE);
				default:
					return km;
			}
			
		}
/// <summary>
/// Converts degrees unit to radians
/// </summary>
/// <param name="degrees">degrees</param>
/// <returns>radians</returns>
		public static double DegreesToRadians(double degrees)
		{
			return (degrees * Math.PI / 180);
		}
	}
/// <summary>
/// Specifies a Distance Unit to use in DistanceCalculator class.
/// </summary>
	public enum DistanceUnit{Km, Mile, NordicMile}
}
	
Calculating latitude and longitude for retriving
list of points within any given radius from a single point
► JavaScript
function getRadiusCords(radius, startLat, startLong)
{

   startLat  = parseFloat(startLat)
   startLong = parseFloat(startLong)
   radius    = parseInt(radius)
   
   var latRange  = radius / ((6076 / 5280) * 60)
   var longRange = radius / (((Math.cos(parseFloat(startLat * Math.PI / 180)) * 6076) / 5280) * 60)

   return({
         latitude_low:startLat - latRange,
         longitude_low:startLong - longRange,
         latitude_hight:startLat + latRange,
         longitude_hight:startLong + longRange
      })
}
	
► VBScript
const pi = 3.14159265358979323846
	
function getRadiusCords(radius, startLat, startLong)

	dim latRange, longRange, values(4)

	startLat  = CDbl(startLat)
	startLong = CDbl(startLong)
	radius    = Int(radius)
	
	latRange  = radius / ((6076 / 5280) * 60)
	longRange = radius / (((cos(CDbl(startLat * pi / 180)) * 6076) / 5280) * 60)
	
	values(1) = startLat - latRange
	values(2) = startLong - longRange
	values(3) = startLat + latRange
	values(4) = startLong + longRange
	
	getRadiusCords = values
	
end function
	
► C#
using System;

namespace GlobalCitiesDatabase
{
	/// <summary>
	/// RadiusCoords class, represents a range
	/// of coordinates.
	/// 
	/// It consists on a triangle, with 2 points of reference:
	/// LatitudeLow, LongitudeLow:LatitudeHigh, LongitudeHigh
	/// 
	/// </summary>
	public class RadiusCoords
	{
		private double latitudeLow;
		private double longitudeLow;
		private double latitudeHigh;
		private double longitudeHigh;
		private double startLatitude;
		private double startLongitude;
		private int radius;
		/// <summary>
		/// FEETS_IN_MILE - Provides a constant to convert feet distance
		/// to miles (value=5280)
		/// </summary>
		public const int FEETS_IN_MILE=5280;
		/// <summary>
		/// FEETS_IN_NAUTICAL_MILE - Provides a constant to convert feet distance
		/// to nautical miles (value=6076)
		/// </summary>
		public const int FEETS_IN_NAUTICAL_MILE=6076;

		/// <summary>
		/// Constructs a new RadiusCoords object, represents a range
		/// of coordinates around the specified starting point & radius.
		/// </summary>
		/// <param name="startLatitude"></param>
		/// <param name="startLongitude"></param>
		/// <param name="radius"></param>
		
		public RadiusCoords(double startLatitude, double startLongitude, int radius)
		{
   
			this.startLatitude = startLatitude;
			this.startLongitude = startLongitude;
			this.radius = radius;
			
			double latRange  = radius / ((FEETS_IN_NAUTICAL_MILE / FEETS_IN_MILE) * 60);
			double longRange = radius / (((Math.Cos(startLatitude * Math.PI / 180) * FEETS_IN_NAUTICAL_MILE) / FEETS_IN_MILE) * 60);


			latitudeLow = startLatitude - latRange;
			longitudeLow = startLongitude - longRange;
			latitudeHigh = startLatitude + latRange;
			longitudeHigh = startLongitude + longRange;

		}

		public double LatitudeLow
		{
			get { return latitudeLow; }
		}

		public double LongitudeLow
		{
			get { return longitudeLow; }
		}

		public double LatitudeHigh
		{
			get { return latitudeHigh; }
		}

		public double LongitudeHigh
		{
			get { return longitudeHigh; }
		}

		public double StartLatitude
		{
			get { return startLatitude; }
		}

		public double StartLongitude
		{
			get { return startLongitude; }
		}

		public int Radius
		{
			get { return radius; }
		}
	}
}
	
This function should return 4 values. Then using these values the foloving SQL statment will retrive list of desired cities:
SELECT cityName
FROM cities
WHERE longitude >= longitude_low 
  AND latitude >= latitude_low 
  AND latitude <= latitude_hight
  AND longitude <= longitude_higth
	
Converting decimal degrees to a Degrees Minuets Seconds display
► JavaScript
function decimalDegToDMS(decimalDegrees)
{
   var temp, d, m, s
   d = parseInt(decimalDegrees)
   temp = (decimalDegrees - d) * 60
   m = parseInt(temp)
   temp = (temp - m) * 60
   s = parseInt(temp)
   return ({d:d, m:m, s:s})
}
	
► VBScript
function DecimalDegToDMS(DecimalDegrees)

	dim temp, d, m, s, values(3)
	
	d = Int(DecimalDegrees)
	temp = (DecimalDegrees - d) * 60.
	m = Int(temp)
	temp = (temp - m) * 60.
	s = Int(temp)

	values(1) = d
	values(2) = m
	values(3) = s

	DecimalDegToDMS = values

end function
	
► C#
using System;

namespace GlobalCitiesDatabase
{
	/// <summary>
	/// DMS struct represents an angle with
	/// a combination of degrees, minutes & seconds.
	/// This object is immutable.
	/// </summary>
	public struct DMS
	{
		private int degrees;
		private int minutes;
		private int seconds;
		/// <summary>
		/// The Degrees value of the current instance
		/// </summary>
		public int Degrees
		{
			get { return degrees; }
		}
		/// <summary>
		/// The Minutes value of the current instance
		/// </summary>
		public int Minutes
		{
			get { return minutes; }
		}
		/// <summary>
		/// The Seconds value of the current instance
		/// </summary>
		public int Seconds
		{
			get { return seconds; }
		}

		/// <summary>
		/// Constructs a new DMS instance with the specified
		/// decimal degrees value
		/// </summary>
		/// <param name="decimalDegrees">decimal degrees</param>
		public DMS(double decimalDegrees)
		{
			int temp;
			degrees = (int)decimalDegrees;
			temp = (int)((decimalDegrees - degrees) * 60);
			minutes = temp;
			temp = (temp - minutes) * 60;
			seconds = temp;
		}
		/// <summary>
		/// Returns the string representation of the current
		/// object in format:dd:mm'ss"
		/// </summary>
		public override string ToString()
		{
			return String.Format("{0}:{1}'{2}\"", Degrees, Minutes, Seconds);
		}

	}
}
	
Convert dotted IP address into IP number in long
► JavaScript
function ipStr2ipNum(ipStr)
{
	var ipArr = ipStr.split(".")
	return ((16777216 * ipArr[0]) + (65536 * ipArr[1]) + (256 * ipArr[2]) + parseInt(ipArr[3]))
}
	
► VBScript
function ipStr2ipNum(ipStr)

	var ipArr = Split(ipStr,".")
	ipStr2ipNum = (16777216 * ipArr(0)) + (65536 * ipArr(1)) + (256 * ipArr(2)) + Int(ipArr(3))

end function
	
Software License | Copyright © 2006 GCDB All Rights Reserved.
בניית אתרים,מדפסת , מכונות צילום, מדפסות לייזר , מיתוג, לוגו, בניית אתרים , Holyland , Holy land , Christian gifts , Jesus Sandals
Disabled