Friday, February 13, 2009

Distance Calculation Between Two Address using Free API , c#, ASP.Net 2.0

Distance calculation Between two address using API , c#,ASP.Net 2.0
 
Distance calculation Between two address using API , c#,ASP.Net 2.0
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="distcalcul.aspx.cs" Inherits="distcalcul" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table align="center">
            <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                    <asp:Label ID="Label8" runat="server" Text="From Address"></asp:Label></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Label ID="Label1" runat="server" Text="Address1"></asp:Label></td>
                <td style="width: 100px">
                    <asp:TextBox ID="TextBox1" runat="server">71 Fortune Drive</asp:TextBox></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Label ID="Label2" runat="server" Text="City:"></asp:Label></td>
                <td style="width: 100px">
                    <asp:TextBox ID="TextBox2" runat="server">Irvine</asp:TextBox></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Label ID="Label3" runat="server" Text="State"></asp:Label></td>
                <td style="width: 100px">
                    <asp:TextBox ID="TextBox3" runat="server">CA</asp:TextBox></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px; height: 21px">
                </td>
                <td style="width: 100px; height: 21px">
                    <asp:Label ID="Label9" runat="server" Text="To Address"></asp:Label></td>
                <td style="width: 100px; height: 21px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px; height: 21px">
                    <asp:Label ID="Label4" runat="server" Text="Address:"></asp:Label></td>
                <td style="width: 100px; height: 21px">
                    <asp:TextBox ID="TextBox4" runat="server">833 W. Torrance Blvd.</asp:TextBox></td>
                <td style="width: 100px; height: 21px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Label ID="Label5" runat="server" Text="City"></asp:Label></td>
                <td style="width: 100px">
                    <asp:TextBox ID="TextBox5" runat="server">Torrance</asp:TextBox></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Label ID="Label6" runat="server" Text="State"></asp:Label></td>
                <td style="width: 100px">
                    <asp:TextBox ID="TextBox6" runat="server">CA</asp:TextBox></td>
                <td style="width: 100px">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Label ID="Label10" runat="server"></asp:Label></td>
                <td style="width: 100px">
                    <asp:Label ID="Label7" runat="server" Width="76px"></asp:Label></td>
                <td style="width: 100px">
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

code Behind
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class distcalcul : System.Web.UI.Page
{
    double flatitude = 0;
    double flongitude = 0;
    double tlatitude = 0;
    double tlongitude = 0;
    
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string street=TextBox1.Text;
        string city = TextBox2.Text;
        string state = TextBox3.Text;
       
        fdistcal(street,city, state);
        string tstreet = TextBox4.Text;
       string tcity = TextBox5.Text;
       string tstate = TextBox6.Text;
      tdistcal(tstreet, tcity, tstate);

      distanceCal(flatitude, flongitude, tlatitude, tlongitude, 'M');
      //Response.Write(distanceCal(flatitude, flongitude, tlatitude, tlongitude, 'K') * 0.62);
      double totdist = distanceCal(flatitude, flongitude, tlatitude, tlongitude, 'K') * 0.62;
   //ToString("#0.00"));
      Label7.Text = totdist.ToString("#0.000");
      Label10.Text = "Distance(Miles)";
       
    }

    void fdistcal(string street, string city, string state)
    {
        string geocoderUri = string.Format("http://rpc.geocoder.us/service/rest?address={0},{1},{2}", street, city, state);
        XmlDocument geocoderXmlDoc = new XmlDocument();
        geocoderXmlDoc.Load(geocoderUri);
        XmlNamespaceManager nsMgr = new XmlNamespaceManager(geocoderXmlDoc.NameTable);
        nsMgr.AddNamespace("geo", @"http://www.w3.org/2003/01/geo/wgs84_pos#");
        string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:long", nsMgr).InnerText;
        string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:lat", nsMgr).InnerText;
        double latitude = Double.Parse(sLat);
        double longitude = Double.Parse(sLong);
    //   Response.Write("Latitude: " + latitude + "<br /> Longitude: " + longitude);
        flatitude=latitude;
        flongitude=longitude;
  
    }
    void tdistcal(string street, string city, string state)
    {
        string geocoderUri = string.Format("http://rpc.geocoder.us/service/rest?address={0},{1},{2}", street, city, state);
        XmlDocument geocoderXmlDoc = new XmlDocument();
        geocoderXmlDoc.Load(geocoderUri);
        XmlNamespaceManager nsMgr = new XmlNamespaceManager(geocoderXmlDoc.NameTable);
        nsMgr.AddNamespace("geo", @"http://www.w3.org/2003/01/geo/wgs84_pos#");
        string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:long", nsMgr).InnerText;
        string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:lat", nsMgr).InnerText;
        double latitude = Double.Parse(sLat);
        double longitude = Double.Parse(sLong);
        //Response.Write("Latitude: " + latitude + "<br /> Longitude: " + longitude);
        tlatitude = latitude;
        tlongitude = longitude;
       
 
    }

    private double distanceCal(double Lat1, double Long1, double Lat2, double Long2, char unit)
    {
        double dDistance = Double.MinValue;
        double dLat1InRad = Lat1 * (Math.PI / 180.0);
        double dLong1InRad = Long1 * (Math.PI / 180.0);
        double dLat2InRad = Lat2 * (Math.PI / 180.0);
        double dLong2InRad = Long2 * (Math.PI / 180.0);
        double dLongitude = dLong2InRad - dLong1InRad;
        double dLatitude = dLat2InRad - dLat1InRad;
        // Intermediate result a.
        double a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) *Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);
        // Intermediate result c (great circle distance in Radians).
        double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));
        // Distance.
        // const Double kEarthRadiusMiles = 3956.0;
        const Double kEarthRadiusKms = 6376.5;
        dDistance = kEarthRadiusKms * c;
        return dDistance;
        //return (dist);
    }

    

}
 

No comments: