sábado, 9 de março de 2013

G-MAPS - RETORNA A DISTÂNCIA ENTRE DOIS CEPS


Daê pessoal! 

Depois de longo e tenebroso inverso, volto para compartilhar um comando que achei muiiito interessante!
Vamos criar uma função que, baseada em duas condicionais (Cep 1 e Cep 2) retornará a distancias (em KM) entre dois pontos.
Agradecimentos aos participantes da turma de Macro e VBA de Nov/2012, Rogerio, Deyse e Naira que lançaram o desafio... Em especial a Jaime Bull responsável pela criação da função, a qual adaptamos as nossas necessidades.

Então vamos a prática! Primeiramente precisamos habilitar a guia desenvolvedor (Mais detalhes no post MACROS), depois clicamos em “Visual Basic”. Abrirá a tela abaixo:



Clicamos em “Inserir“ depois em “Módulo”:


Agora clicaremos duas vezes sobre o “Módulo” adicionado e colaremos as funções abaixo:

‘---------------------------------------------------------------------------------------------------

Function G_DISTANCIA(Origin As String, Destination As String)
' Requires a reference to Microsoft XML, v6.0
' Draws on the stackoverflow answer at bit.ly/parseXML
Dim myRequest As XMLHTTP60
Dim myDomDoc As DOMDocument60
Dim distanceNode As IXMLDOMNode
G_DISTANCIA = 0
' Check and clean inputs
On Error GoTo exitRoute
Origin = Replace(Origin, " ", "%20")
Destination = Replace(Destination, " ", "%20")
' Read the XML data from the Google Maps API
Set myRequest = New XMLHTTP60
& Origin & "&destination=" & Destination & "&sensor=false", False
myRequest.send
' Make the XML readable usign XPath
Set myDomDoc = New DOMDocument60
myDomDoc.LoadXML myRequest.responseText
' Get the distance node value
Set distanceNode = myDomDoc.SelectSingleNode("//leg/distance/value")
If Not distanceNode Is Nothing Then G_DISTANCIA = (distanceNode.Text / 1000) & " KM"
exitRoute:
' Tidy up
Set distanceNode = Nothing
Set myDomDoc = Nothing
Set myRequest = Nothing
End Function
‘---------------------------------------------------------------------------------------------------
Function G_duracao(Origin As String, Destination As String) As Double
' Requires a reference to Microsoft XML, v6.0
' Draws on the stackoverflow answer at bit.ly/parseXML
Dim myRequest As XMLHTTP60
Dim myDomDoc As DOMDocument60
Dim distanceNode As IXMLDOMNode
G_duracao = 0
' Check and clean inputs
On Error GoTo exitRoute
Origin = Replace(Origin, " ", "%20")
Destination = Replace(Destination, " ", "%20")
' Read the XML data from the Google Maps API
Set myRequest = New XMLHTTP60
& Origin & "&destination=" & Destination & "&sensor=false", False
myRequest.send
' Make the XML readable usign XPath
Set myDomDoc = New DOMDocument60
myDomDoc.LoadXML myRequest.responseText
' Get the distance node value
Set distanceNode = myDomDoc.SelectSingleNode("//leg/duration/value")
If Not distanceNode Is Nothing Then G_duracao = distanceNode.Text / 86400
exitRoute:
' Tidy up
Set distanceNode = Nothing
Set myDomDoc = Nothing
Set myRequest = Nothing
End Function
‘---------------------------------------------------------------------------------------------

A tela ficará assim:



Pronto! Agora retorne a planilha e digite as funções:


Para retornar a distancia: “=G_DISTANCIA(A1;A2)”
Para retornar o tempo: “=G_duracao(A1;A2)”

Nas funções acima você poderá utilizar CEP ou o endereço.

Grande abraço!

Prof. Darlan