我想计算谷歌地图中2点之间的距离。我有纬度&两点的经度,但想要它们之间的距离。
答案 0 :(得分:1)
您可以使用Haversine公式。
在下一页上查看Distance To()函数:
http://blog.codebrain.co.uk/post/2009/05/08/C-Distance-Between-Two-Postcodes.aspx
答案 1 :(得分:1)
为此您应该使用以下代码。 为了设计
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 />
用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();
}
}
答案 2 :(得分:0)
一个简单的谷歌搜索显示this 在那里你可以做计算,它也解释了使用的公式 我首先看看Haversine folmula。