Hi priyajsr,
Refer the below sample code. Here i have used Newtonsoft.Json library.
Using the DLL from here
http://james.newtonking.com/json/help/index.html
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double latSource = Convert.ToDouble(txtLat1.Text.Trim());
double longSource = Convert.ToDouble(txtLong1.Text.Trim());
double latDestination = Convert.ToDouble(txtLat2.Text.Trim());
double longDestination = Convert.ToDouble(txtLong2.Text.Trim());
string sourceAddress = getAddress(latSource, longSource);
string destinationAddress = getAddress(latDestination, longDestination);
txtDistance.Text = getDistance(sourceAddress, destinationAddress);
}
protected string GetJsonData(string url)
{
string sContents = string.Empty;
string me = string.Empty;
try
{
if (url.ToLower().IndexOf("https:") > -1)
{
System.Net.WebClient client = new System.Net.WebClient();
byte[] response = client.DownloadData(url);
sContents = System.Text.Encoding.ASCII.GetString(response);
}
else
{
System.IO.StreamReader sr = new System.IO.StreamReader(url);
sContents = sr.ReadToEnd();
sr.Close();
}
}
catch
{
sContents = "unable to connect to server ";
}
return sContents;
}
public string getAddress(double latitude, double longitude)
{
string address = "";
string content = GetJsonData("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&sensor=true");
JObject obj = JObject.Parse(content);
try
{
address = obj.SelectToken("results[0].address_components[3].long_name").ToString();
return address;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return address;
}
public string getDistance(string source, string destination)
{
int distance = 0;
string content = GetJsonData("https://maps.googleapis.com/maps/api/directions/json?origin=" + source + "&destination=" + destination + "&sensor=false");
JObject obj = JObject.Parse(content);
try
{
distance = (int)obj.SelectToken("routes[0].legs[0].distance.value");
return (distance / 1000).ToString() + " K.M.";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return (distance / 1000).ToString() + " K.M.";
}
}
VB.Net
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs)
Dim latSource As Double = Convert.ToDouble(txtLat1.Text.Trim())
Dim longSource As Double = Convert.ToDouble(txtLong1.Text.Trim())
Dim latDestination As Double = Convert.ToDouble(txtLat2.Text.Trim())
Dim longDestination As Double = Convert.ToDouble(txtLong2.Text.Trim())
Dim sourceAddress As String = getAddress(latSource, longSource)
Dim destinationAddress As String = getAddress(latDestination, longDestination)
txtDistance.Text = getDistance(sourceAddress, destinationAddress)
End Sub
Protected Function GetJsonData(url As String) As String
Dim sContents As String = String.Empty
Dim [me] As String = String.Empty
Try
If url.ToLower().IndexOf("https:") > -1 Then
Dim client As New System.Net.WebClient()
Dim response As Byte() = client.DownloadData(url)
sContents = System.Text.Encoding.ASCII.GetString(response)
Else
Dim sr As New System.IO.StreamReader(url)
sContents = sr.ReadToEnd()
sr.Close()
End If
Catch
sContents = "unable to connect to server "
End Try
Return sContents
End Function
Public Function getAddress(latitude As Double, longitude As Double) As String
Dim address As String = ""
Dim content As String = GetJsonData("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&sensor=true")
Dim obj As JObject = JObject.Parse(content)
Try
address = obj.SelectToken("results[0].address_components[3].long_name").ToString()
Return address
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return address
End Function
Public Function getDistance(source As String, destination As String) As String
Dim distance As Integer = 0
Dim content As String = GetJsonData((Convert.ToString((Convert.ToString("https://maps.googleapis.com/maps/api/directions/json?origin=") & source) + "&destination=") & destination) + "&sensor=false")
Dim obj As JObject = JObject.Parse(content)
Try
distance = CInt(obj.SelectToken("routes[0].legs[0].distance.value"))
Return (distance / 1000).ToString() + " K.M."
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return (distance / 1000).ToString() + " K.M."
End Function
End Class
Screenshot
On Get Distance button click distance will display in the textbox.