<< Click to Display Table of Contents >> Google Maps - calcular distância com XML |
![]() ![]() ![]() |
Tela
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Digite endereço de origem "></asp:Label>
<asp:TextBox ID="txtOrigem" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Digite endereço de destino "></asp:Label>
<asp:TextBox ID="txtDestino" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Calcular" OnClick="Button1_Click" /><br />
<br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
cs
using System;
using System.Xml.Linq;
namespace WebApplication5
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//URL do distancematrix - adicionando endereço de origem e destino
string url = string.Format("http://maps.googleapis.com/maps/api/distancematrix/xml?origins={0}&destinations={1}&mode=driving&language=pt-BR&sensor=false",
txtOrigem.Text, txtDestino.Text);
//Carregar o XML via URL
XElement xml = XElement.Load(url);
//Verificar se o status é OK
if (xml.Element("status").Value == "OK")
{
//Formatar a resposta
Label3.Text = string.Format("<strong>Origem</strong>: {0} <br /><strong>Destino:</strong> {1} <br /><strong>Distância</strong>: {2} <br /><strong>Duração</strong>: {3}",
//Pegar endereço de origem
xml.Element("origin_address").Value,
//Pegar endereço de destino
xml.Element("destination_address").Value,
//Pegar duração
xml.Element("row").Element("element").Element("duration").Element("text").Value,
//Pegar distância ente os dois pontos
xml.Element("row").Element("element").Element("distance").Element("text").Value
);
}
else
{
//Se ocorrer algum erro
Label3.Text = String.Concat("Ocorreu o seguinte erro: ", xml.Element("status").Value);
}
}
}
}
URL de exemplo para retornar XML
XML de retorno
<DistanceMatrixResponse>
<status>OK</status>
<origin_address>Porto Alegre - RS, República Federativa do Brasil</origin_address>
<destination_address>São Paulo, República Federativa do Brasil</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>46388</value>
<text>12 horas 53 minutos</text>
</duration>
<distance>
<value>1139925</value>
<text>1.140 km</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>
Status de retorno
Status |
Tipos de Status |
OK |
Retorno válido. |
INVALID_REQUEST |
Requisição inválida. |
MAX_ELEMENTS_EXCEEDED |
Origem ou destino ultrapassaram o numero máximo de caracteres.. |
OVER_QUERY_LIMIT |
Aplicação excedeu o número máximo de requisições em um determinado período. |
REQUEST_DENIED |
Sua aplicação bloqueou o uso do Distance Matrix. |
UNKNOWN_ERROR |
Erro no servidor. Tente novamente que pode funcionar. |
Fonte
http://cbsa.com.br/post/calcular-distancia-entre-dois-enderecos---google-maps-api.aspx