daily pastebin goal
16%
SHARE
TWEET

Distance and time between two locations

ShadowTzu Nov 19th, 2014 (edited) 168 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Net
  2.  
  3. Public Class Form1
  4.     Private Web As Net.WebClient
  5.  
  6.     Private Structure struct_Info
  7.         Public Origine As String
  8.         Public destination As String
  9.         Public distance As Single
  10.         Public duration As Integer
  11.     End Structure
  12.  
  13.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  14.         Web = New WebClient
  15.         Web.Encoding = System.Text.Encoding.UTF8
  16.         Dim Info As struct_Info
  17.  
  18.         Info = Distance("ol3 5rq", "ol8 1xu")
  19.  
  20.         MsgBox("Origine: " & Info.Origine & vbCrLf & _
  21.             "Destination: " & Info.destination & vbCrLf & _
  22.             "Distance: " & Info.distance & " m" & vbCrLf & _
  23.             "Duration: " & Info.duration & " s")
  24.     End Sub
  25.  
  26.     Private Function Distance(PostCode1 As String, PostCode2 As String) As struct_Info
  27.         'mode=driving
  28.         'mode=bicycling
  29.         'mode=transit
  30.         'mode=walking
  31.         Dim mode As String = "driving"
  32.         Dim url As String = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=" & PostCode1 & "&destinations=" & PostCode2 & "&mode=" & mode & "&language=en-EN&sensor=false"
  33.         Dim data As String = Web.DownloadString(url)
  34.         data = data.Replace("(", "") : data = data.Replace(")", "") : data = data.Replace("[", "") : data = data.Replace("]", "") : data = data.Replace("{", "") : data = data.Replace("}", "") : data = data.Replace(":", "")
  35.         Dim splitted() As String = Split(data, """")
  36.         Dim result As struct_Info
  37.         With result
  38.             .Origine = splitted(7)
  39.             .destination = splitted(3)
  40.             .distance = CSng(Val(splitted(20)))
  41.             .duration = CInt(Val(splitted(28)))
  42.         End With
  43.  
  44.         Return result
  45.     End Function
  46. End Class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top