Sample Application for Web Service
Write this code on WebService Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
}
[WebMethod]
public double distanceFrom(double lat1, double lng1, double lat2, double lng2)
{
double dist;
dist= distance(lat1, lng1, lat2, lng2);
return dist;
}
private double distance(double lat1, double lon1, double lat2, double lon2)
{
double theta = lon1 - lon2;
double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
dist = Math.Acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
//For Kilometer
// dist = dist * 1.609344;
//For S Miles
// dist = dist * 0.8684;
dist = Math.Round(dist, 2);
return (dist);
}
private double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad)
{
return (rad / Math.PI * 180.0);
}
}
After This Add Web Reference in your site and write code on your aspx.cs page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference1;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceSoapClient sp=new ServiceSoapClient();
double lat1=Convert.ToDouble(TextBox1.Text);
double lat2 = Convert.ToDouble(TextBox3.Text); ;
double lon1=Convert.ToDouble(TextBox2.Text);
double lon2 = Convert.ToDouble(TextBox4.Text); ;
double str = sp.distanceFrom(lat1,lon1,lat2,lon2);
TextBox5.Text = str.ToString()+' '+"Miles";
}
}
Write this code on WebService Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
}
[WebMethod]
public double distanceFrom(double lat1, double lng1, double lat2, double lng2)
{
double dist;
dist= distance(lat1, lng1, lat2, lng2);
return dist;
}
private double distance(double lat1, double lon1, double lat2, double lon2)
{
double theta = lon1 - lon2;
double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
dist = Math.Acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
//For Kilometer
// dist = dist * 1.609344;
//For S Miles
// dist = dist * 0.8684;
dist = Math.Round(dist, 2);
return (dist);
}
private double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad)
{
return (rad / Math.PI * 180.0);
}
}
After This Add Web Reference in your site and write code on your aspx.cs page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference1;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceSoapClient sp=new ServiceSoapClient();
double lat1=Convert.ToDouble(TextBox1.Text);
double lat2 = Convert.ToDouble(TextBox3.Text); ;
double lon1=Convert.ToDouble(TextBox2.Text);
double lon2 = Convert.ToDouble(TextBox4.Text); ;
double str = sp.distanceFrom(lat1,lon1,lat2,lon2);
TextBox5.Text = str.ToString()+' '+"Miles";
}
}