how to find distance between 2 points on google map?
I want to calculate the distance between 2 points in Google Maps. I have the latitude & longitude of both points but want the distance between t开发者_如何学JAVAhem.
You can use the Haversine formula.
Check out the DistanceTo() function on the following page:
http://blog.codebrain.co.uk/post/2009/05/08/C-Distance-Between-Two-Postcodes.aspx
for this you should have use following codes. for Design
function OrginAutoComplete() {
try {
var input = document.getElementById('TextBox1');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.setTypes('changetype-geocode');
}
catch (err) {
}
}
function DestAutoComplete() {
try {
var input = document.getElementById('TextBox4');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.setTypes('changetype-geocode');
}
catch (err) {
}
}
google.maps.event.addDomListener(window, 'load', OrginAutoComplete);
google.maps.event.addDomListener(window, 'load', DestAutoComplete);
</script>
<asp:Label ID="Label1" runat="server" Text="Pick Up address"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="DroppOff Address"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Details"
onclick="Button1_Click" style="margin-left: 55px" Width="97px" />
<br />
<br />
For coding in c#:
string xmlResult = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + TextBox1.Text + "&destinations=" + TextBox4.Text + "&mode=car &language=us-en&sensor=false");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader resStream = new StreamReader(response.GetResponseStream());
XmlDocument doc = new XmlDocument();
xmlResult = resStream.ReadToEnd();
doc.LoadXml(xmlResult);
string output = "";
try
{
if (doc.DocumentElement.SelectSingleNode("/DistanceMatrixResponse/row/element/status").InnerText.ToString().ToUpper() != "OK")
{
lblResult.Text = "Invalid City Name please try again";
return;
}
XmlNodeList xnList = doc.SelectNodes("/DistanceMatrixResponse");
foreach (XmlNode xn in xnList)
{
if (xn["status"].InnerText.ToString() == "OK")
{
output = "<table align='center' width='600' cellpadding='0' cellspacing='0'>";
output += "<tr><td height='60' colspan='2' align='center'><b>Travel Details</b></td>";
output += "<tr><td height='40' width='30%' align='left'>Orgin Place</td><td align='left'>" + xn["origin_address"].InnerText.ToString() + "</td></tr>";
output += "<tr><td height='40' align='left'>Destination Place</td><td align='left'>" + xn["destination_address"].InnerText.ToString() + "</td></tr>";
output += "<tr><td height='40' align='left'>Distance</td><td align='left'>" + doc.DocumentElement.SelectSingleNode("/DistanceMatrixResponse/row/element/distance/text").InnerText + "</td></tr>";
output += "</table>";
lblResult.Text = output;
}
}
}
catch (Exception ex)
{
lblResult.Text = "Error during processing";
return;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
double cost = 0;
double distance = Convert.ToDouble(TextBox5.Text);
if (distance >= 50)
{
cost = distance * 0.4;
lblResult.Text = cost.ToString();
}
else if (distance >= 1000)
{
cost = distance * 0.35+50;
lblResult.Text = cost.ToString();
}
else if (distance < 50)
{
cost = distance * 0.65;
lblResult.Text = cost.ToString();
}
else if (distance <=10)
{
cost = distance * 0.85;
lblResult.Text = cost.ToString();
}
}
A simple google search revealed this.
There you can do calculations and it also explains the formulas used.
I'd start by looking at the Haversine folmula.
精彩评论