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.
IGEOCODE Canada Postal Code Premium Edition & Gold Edition. Try the free online demo now!
IGEOCODE US ZIP Code Premium Edition & Gold Edition. Try the free online demo now!
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).
( ( ))
( )
<?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@zipcodeworld.com
* IGEOCODE Web site: http://www.igeocode.com
*
* Smetis Inc. © All Rights Reserved 2010
*
* ------------------------------------------------------------------ */
?>
/* ------------------------------------------------------------------
* 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@zipcodeworld.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'));
}
C# SAMPLE CODE
/* ------------------------------------------------------------------
* 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@zipcodeworld.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@zipcodeworld.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
# ------------------------------------------------------------------
# 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@zipcodeworld.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;
/* ------------------------------------------------------------------
* 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@zipcodeworld.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;