Friday, November 5, 2010

Yahoo map/geocoding API to get latitude and longitude c# sample

Following is the sample code:-

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.Data.SqlClient;

public partial class yalatilongi : System.Web.UI.Page
{
    private GeoAddressAPI geoAPI = new GeoAddressAPI();

    protected void Page_Load(object sender, EventArgs e)
    {
        string strConnection = "server=xxxxx;database=xxxxx;uid=sa;pwd=xxxxxxx;Connect Timeout=60";
        SqlConnection myConn = new SqlConnection(strConnection);
        string strstate = "CA";
        string query = "select aladd1,alcty1,aladds,aladdz,aban8 from jde_rk.dbo.latlongad where lati=0 and aladds='"+strstate+"'";
        SqlCommand myCommand = new SqlCommand(query, myConn);
        myCommand.Connection.Open();
        SqlDataReader myReader = myCommand.ExecuteReader();
        while (myReader.Read())
        {
            Response.Write(myReader.GetString(0));
            Response.Write("<br>");
            Response.Write(myReader.GetString(1));
            Response.Write("<br>");
            Response.Write(myReader.GetString(2));
            Response.Write("<br>");
            Response.Write(myReader.GetString(3));
            Response.Write("<br>");
            Response.Write(myReader.GetString(4));
            Response.Write("<br>");

            GeoAddress address = geoAPI.GeoEncodeAddress(myReader.GetString(0), myReader.GetString(1), myReader.GetString(2), myReader.GetString(3));
            Response.Write(address.Latitude);
            Response.Write("<br>");
            Response.Write(address.Longitude);
            Response.Write("<br>");
            Response.Write(myReader.GetString(4));
           // Response.End();

            editLeadMaster(myReader.GetString(4), address.Latitude, address.Longitude);

        }
        myReader.Close();
        myConn.Close();
    }



    void editLeadMaster(string aban8, string Lati, string Longi)
    {
        SqlConnection myConnection = new SqlConnection(ConnectionString);
       
        string strSQL = "UPDATE jde_rk.dbo.latlongad SET longi=@longi,lati=@lati WHERE aban8 ='" + aban8 + "'";
        SqlCommand cmd = new SqlCommand(strSQL, myConnection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@longi", Longi.ToString());
        cmd.Parameters.AddWithValue("@lati", Lati.ToString());
       
        myConnection.Open();
        cmd.ExecuteNonQuery();
        myConnection.Close();

      

    }

   






    private string ConnectionString
    {
        get
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
            return connectionString;
        }
    }



}






for distance calculation use following:-


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: