Developer Reference - Postal Code Radius & Distance Calculation
Why Distance Calculation?
Most IGEOCODE internet geo-location data products include latitude, longitude and elevation to help our customer estimate geographical coordinates of their website visitor. Other than estimates website visitor geography location, latitude and longitude can be utilized to calculate the distance between 2 locations. This calculation usually used in store/dealer locator application or delivery schedule & cost estimation etc.
Great-circle Distance Introduction & Formula
In regards to above common usage, IGEOCODE is presenting the distance calculation formula as well as sample code of
various programming language to assist our customer in implementation of such application.
The great-circle distance is the shortest distance between any two points on the surface of a sphere measured
along a path on the surface of the sphere (as opposed to going through the sphere's interior).
Sample Codes
<?php /* ------------------------------------------------------------------ * This function calculates the distance between two coordinates in * langitude and longitude. It is used for calculating distance * between two ZIP Codes or Postal Codes using IGEOCODE ZIP Code or * Postal Code data. * * Arguments: * lat1 : Latitude of 1st coordinate in decimal degrees * lon1 : Longitude of 1st coordinate in decimal degrees * lat2 : Latitude of 2nd coordinate in decimal degrees * lon2 : Longitude of 2nd coordinate in decimal degrees * unit : The return result unit, 'M' for miles, * 'K' for kilometers and 'N' for nautical miles. * Default return unit is kilometer. * Returns: * distance between 2 coordinates in the specified unit * * For enquiries, please contact support@igeocode.com * IGEOCODE Web site: http://www.igeocode.com * * Smetis Inc. © All Rights Reserved 2010 * * ------------------------------------------------------------------ */ function Distance($lat1, $lon1, $lat2, $lon2, $unit) { $radius = 6371.0090667; // earth mean radius defined by IUGG $dlon = $lon1 - $lon2; $distance = acos( sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($dlon))) * $radius; if ($unit == "K") { return ($distance); } else if ($unit == "M") { return ($distance * 0.621371192); } else if ($unit == "N") { return ($distance * 0.539956803); } else { return 0; } } echo Distance(37.771008, -122.41175, 37.295906, -121.618652, "K") . " kilometers<br>"; echo Distance(37.771008, -122.41175, 37.295906, -121.618652, "M") . " miles<br>"; echo Distance(37.771008, -122.41175, 37.295906, -121.618652, "N") . " nautical miles<br>"; ?>
/* ------------------------------------------------------------------ * This function calculates the distance between two coordinates in * langitude and longitude. It is used for calculating distance * between two ZIP Codes or Postal Codes using IGEOCODE ZIP Code or * Postal Code data. * * Arguments: * lat1 : Latitude of 1st coordinate in decimal degrees * lon1 : Longitude of 1st coordinate in decimal degrees * lat2 : Latitude of 2nd coordinate in decimal degrees * lon2 : Longitude of 2nd coordinate in decimal degrees * unit : The return result unit, 'M' for miles, * 'K' for kilometers and 'N' for nautical miles. * Default return unit is kilometer. * Returns: * distance between 2 coordinates in the specified unit * * For enquiries, please contact support@igeocode.com * IGEOCODE Web site: http://www.igeocode.com * * Smetis Inc. © All Rights Reserved 2010 * * ------------------------------------------------------------------ */ #include <stdio.h> #include <Math.h> #define PI 3.14159265358979323846 double Distance(double lat1, double lon1, double lat2, double lon2, char unit) { double deg2radMultiplier = PI / 180; lat1 = lat1 * deg2radMultiplier; lon1 = lon1 * deg2radMultiplier; lat2 = lat2 * deg2radMultiplier; lon2 = lon2 * deg2radMultiplier; double radius = 6371.0090667; // earth mean radius defined by IUGG double dlon = lon2 - lon1; double distance = acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlon)) * radius; if (unit == 'K') { return (distance); } else if (unit == 'M') { return (distance * 0.621371192); } else if (unit == 'N') { return (distance * 0.539956803); } else { return 0; } } int main(int argc, char* argv[]) { printf("%f", Distance(37.771008, -122.41175, 37.295906, -121.618652, 'K')); printf("%f", Distance(37.771008, -122.41175, 37.295906, -121.618652, 'M')); printf("%f", Distance(37.771008, -122.41175, 37.295906, -121.618652, 'N')); }
/* ------------------------------------------------------------------ * This function calculates the distance between two coordinates in * langitude and longitude. It is used for calculating distance * between two ZIP Codes or Postal Codes using IGEOCODE ZIP Code or * Postal Code data. * * Arguments: * lat1 : Latitude of 1st coordinate in decimal degrees * lon1 : Longitude of 1st coordinate in decimal degrees * lat2 : Latitude of 2nd coordinate in decimal degrees * lon2 : Longitude of 2nd coordinate in decimal degrees * unit : The return result unit, 'M' for miles, * 'K' for kilometers and 'N' for nautical miles. * Default return unit is kilometer. * Returns: * distance between 2 coordinates in the specified unit * * For enquiries, please contact support@igeocode.com * IGEOCODE Web site: http://www.igeocode.com * * Smetis Inc. © All Rights Reserved 2010 * * ------------------------------------------------------------------ */ using System; namespace DistanceCalculatorCs { class Program { static double Distance(double lat1, double lon1, double lat2, double lon2, char unit) { double deg2radMultiplier = Math.PI / 180; lat1 = lat1 * deg2radMultiplier; lon1 = lon1 * deg2radMultiplier; lat2 = lat2 * deg2radMultiplier; lon2 = lon2 * deg2radMultiplier; double radius = 6371.0090667; // earth mean radius defined by IUGG double dlon = lon2 - lon1; double distance = Math.Acos(Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(dlon)) * radius; if (unit == 'K') { return (distance); } else if (unit == 'M') { return (distance * 0.621371192); } else if (unit == 'N') { return (distance * 0.539956803); } else { return 0; } } static void Main(string[] args) { Console.WriteLine(Distance(37.771008, -122.41175, 37.295906, -121.618652, 'K')); Console.WriteLine(Distance(37.771008, -122.41175, 37.295906, -121.618652, 'M')); Console.WriteLine(Distance(37.771008, -122.41175, 37.295906, -121.618652, 'N')); } } }
'* ------------------------------------------------------------------ '* This function calculates the distance between two coordinates in '* langitude and longitude. It is used for calculating distance '* between two ZIP Codes or Postal Codes using IGEOCODE ZIP Code or '* Postal Code data. '* '* Arguments: '* lat1 : Latitude of 1st coordinate in decimal degrees '* lon1 : Longitude of 1st coordinate in decimal degrees '* lat2 : Latitude of 2nd coordinate in decimal degrees '* lon2 : Longitude of 2nd coordinate in decimal degrees '* unit : The return result unit, 'M' for miles, '* 'K' for kilometers and 'N' for nautical miles. '* Default return unit is kilometer. '* Returns: '* distance between 2 coordinates in the specified unit '* '* For enquiries, please contact support@igeocode.com '* IGEOCODE Web site: http://www.igeocode.com '* '* Smetis Inc. © All Rights Reserved 2010 '* '* -------------------------------------------------------------------- const pi = 3.14159265358979323846 const radius = 6371.0090667 Function Distance(lat1, lon1, lat2, lon2, unit) Dim dlon, distance lat1 = cdbl(lat1 * pi / 180) lon1 = cdbl(lon1 * pi / 180) lat2 = cdbl(lat2 * pi / 180) lon2 = cdbl(lon2 * pi / 180) dlon = lon1 - lon2 distance = acos (sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlon) ) * radius Select Case lcase(unit) Case "k" Distance = distance Case "m" Distance = distance * 0.621371192 Case "n" Distance = distance * 0.539956803 End Select End Function '* -------------------------------------------------------------------- '* this function get the arccos function using arctan function '* -------------------------------------------------------------------- Function acos(rad) If abs(rad) <> 1 Then acos = pi/2 - atn(rad / sqr(1 - rad * rad)) ElseIf rad = -1 Then acos = pi End If End function msgbox(Distance(37.771008, -122.41175, 37.295906, -121.618652, "m") & " miles<br>") msgbox(Distance(37.771008, -122.41175, 37.295906, -121.618652, "k") & " kilometers<br>") msgbox(Distance(37.771008, -122.41175, 37.295906, -121.618652, "n") & " nautical miles<br>")
# ------------------------------------------------------------------ # This function calculates the distance between two coordinates in # langitude and longitude. It is used for calculating distance # between two ZIP Codes or Postal Codes using IGEOCODE ZIP Code or # Postal Code data. # # Arguments: # lat1 : Latitude of 1st coordinate in decimal degrees # lon1 : Longitude of 1st coordinate in decimal degrees # lat2 : Latitude of 2nd coordinate in decimal degrees # lon2 : Longitude of 2nd coordinate in decimal degrees # unit : The return result unit, 'M' for miles, # 'K' for kilometers and 'N' for nautical miles. # Default return unit is kilometer. # Returns: # distance between 2 coordinates in the specified unit # # For enquiries, please contact support@igeocode.com # IGEOCODE Web site: http://www.igeocode.com # # Smetis Inc. © All Rights Reserved 2010 # # ------------------------------------------------------------------ use Math::Trig; $pi = 3.14159265358979323846; $radius = 6371.0090667; sub Distance { my ($lat1, $lon1, $lat2, $lon2, $unit) = @_; $lat1 = deg2rad($lat1); $lon1 = deg2rad($lon1); $lat2 = deg2rad($lat2); $lon2 = deg2rad($lon2); my $dlon = $lon1 - $lon2; my $distance = acos (sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($dlon)) * $radius; if ($unit eq "K") { return $distance; } elsif ($unit eq "M") { return ($distance * 0.621371192); } elsif ($unit eq "N") { return ($distance * 0.539956803); } else { return 0; } } print Distance(37.771008, -122.41175, 37.295906, -121.618652, "K") . " Kilometers\n"; print Distance(37.771008, -122.41175, 37.295906, -121.618652, "M") . " Miles\n"; print Distance(37.771008, -122.41175, 37.295906, -121.618652, "N") . " Nautical Miles\n";
/* ------------------------------------------------------------------ * This function calculates the distance between two coordinates in * langitude and longitude. It is used for calculating distance * between two ZIP Codes or Postal Codes using IGEOCODE ZIP Code or * Postal Code data. * * Arguments: * lat1 : Latitude of 1st coordinate in decimal degrees * lon1 : Longitude of 1st coordinate in decimal degrees * lat2 : Latitude of 2nd coordinate in decimal degrees * lon2 : Longitude of 2nd coordinate in decimal degrees * unit : The return result unit, 'M' for miles, * 'K' for kilometers and 'N' for nautical miles. * Default return unit is kilometer. * Returns: * distance between 2 coordinates in the specified unit * * For enquiries, please contact support@igeocode.com * IGEOCODE Web site: http://www.igeocode.com * * Smetis Inc. © All Rights Reserved 2010 * * ------------------------------------------------------------------ */ private double Distance(double lat1, double lon1, double lat2, double lon2, char unit) { double radius = 6371.0090667; lat1 = lat1 * Math.PI / 180.0; lon1 = lon1 * Math.PI / 180.0; lat2 = lat2 * Math.PI / 180.0; lon2 = lon2 * Math.PI / 180.0; double dlon = lon1 - lon2; double distance = Math.acos( Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(dlon))) * radius; if (unit == "K") { return distance; } else if (unit == "M") { return (distance * 0.621371192); } else if (unit == "N") { return (distance * 0.539956803); } else { return 0; } } system.println(Distance(37.771008, -122.41175, 37.295906, -121.618652, "K") + " Kilometers\n"); system.println(Distance(37.771008, -122.41175, 37.295906, -121.618652, "M") + " Miles\n"); system.println(Distance(37.771008, -122.41175, 37.295906, -121.618652, "N") + " Nautical Miles\n");