Monday, 30 September 2013

How to calculate the distance between two latitude and longitude points using C# .NET

How to calculate the distance between two latitude and longitude points using C# .NET

Today am going to explain how to find the distance between two specified latitude and longitude points.

What Is Latitude?

Latitude is an angle which ranges from 00 at the equator to 900 at the poles (North or South). It is a geographic coordinate which specifies the north-south position of a point on the earth surface.

What Is Longitude?

Longitude is a geographic coordinate which specifies the east-west position of a point on the earth surface. By convention one of these longitude called Prime Meridian, which passes through Royal Observatory, Greenwich England is considered as zero degree longitude. The longitude of other places from Greenwich is measured as angle ranging from 00 at Prime Meridian to 1800 eastwards and -1800 westwards.


Both the latitude and longitudes are imaginary lines drawn across the globe.

Now we will learn how to find the distance between given latitude and longitude points.

For this we need to take four text boxes.

The design will look like this

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LatAndLong.aspx.cs" Inherits="HospitalApplication.LatAndLong" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
<table>
                <tr>
                    <td>
                        <asp:Label ID="lblLatitude1" runat="server" Text="Latitude 1"></asp:Label>
                    </td>
                    <td></td>
                    <td>
                        <asp:TextBox ID="txtLatitude1" runat="server"></asp:TextBox>
                    </td>
                    <td></td>
                    <td>
                        <asp:Label ID="lblLongitude1" runat="server" Text="Longitude 1"></asp:Label>
                    </td>
                    <td></td>
                    <td>
                        <asp:TextBox ID="txtLongitude1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblLatitude2" runat="server" Text="Latitude 2"></asp:Label>
                    </td>
                    <td></td>
                    <td>
                        <asp:TextBox ID="txtLatitude2" runat="server"></asp:TextBox>
                    </td>
                    <td></td>
                    <td>
                        <asp:Label ID="lblLongitude2" runat="server" Text="Longitude 2"></asp:Label>
                    </td>
                    <td></td>
                    <td>
                        <asp:TextBox ID="txtLongitude2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td>
                        <asp:Button ID="btnDistance" runat="server" Text="Calculate" />
                    </td>
                </tr>
<tr>
                    <td></td>
                    <td></td>
                    <td>
                        <asp:Label ID="lblDistance" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


The screen shot will look like this



First we will call all the text boxes in the button click event. We have to change all the textboxes to double.
protected void btnDistance_Click(object sender, EventArgs e)
{
double Lat1 = Convert.ToDouble(txtLatitude1.Text);
double Long1 = Convert.ToDouble(txtLatitude2.Text);
double Lat2 = Convert.ToDouble(txtLongitude1.Text);
double Long2 = Convert.ToDouble(txtLongitude2.Text);
getDistance(Lat1, Long1, Lat2, Long2);
}
By default textboxes are strings. So we need to convert them to double.
Then we will call a method called get distance. There we will pass the latitude and longitude values.
Now we will see what will happen in getDistance method
void getDistance(double Lat1, double Long1, double Lat2, double Long2)
{
// Here R is the radius of the earth in kms
var R = 6373;
double Latitude1 = deg2rad(Lat1);
double Longitude1 = deg2rad(Long1);
double Latitude2 = deg2rad(Lat2);
double Longitude2 = deg2rad(Long2);
double dlon = Longitude2 - Longitude1;
double dlat = Latitude2 - Latitude1;
double a = Math.Pow((Math.Sin(dlat / 2)), 2) + Math.Cos(Latitude1) * Math.Cos(Latitude2) * Math.Pow((Math.Sin(dlon / 2)), 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var d = R * c;
lblDistance.Text = d.ToString();
}
We have to convert all the values to radians. For this we will call a method deg2rad. We will now have a look at the code in deg2rad method.
private double deg2rad(double d)
{
double rad = d * Math.PI / 180;
return rad;
}
Now the entire code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HospitalApplication
{
public partial class LatAndLong : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnDistance_Click(object sender, EventArgs e)
{
double Lat1 = Convert.ToDouble(txtLatitude1.Text);
double Long1 = Convert.ToDouble(txtLatitude2.Text);
double Lat2 = Convert.ToDouble(txtLongitude1.Text);
double Long2 = Convert.ToDouble(txtLongitude2.Text);
getDistance(Lat1, Long1, Lat2, Long2);
}
void getDistance(double Lat1, double Long1, double Lat2, double Long2)
{
var R = 6373;
double Latitude1 = deg2rad(Lat1);
double Longitude1 = deg2rad(Long1);
double Latitude2 = deg2rad(Lat2);
double Longitude2 = deg2rad(Long2);
double dlon = Longitude2 - Longitude1;
double dlat = Latitude2 - Latitude1;
double a = Math.Pow((Math.Sin(dlat / 2)), 2) + Math.Cos(Latitude1) * Math.Cos(Latitude2) * Math.Pow((Math.Sin(dlon / 2)), 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var d = R * c;
lblDistance.Text = d.ToString();
}
private double deg2rad(double d)
{
double rad = d * Math.PI / 180;
return rad;
}
}
}
The output screen



1 comment: