Calculating driving distance between two post codes (geocoding)

skip21

Member
Joined
Oct 21, 2012
Messages
6
Programming Experience
1-3
Hi Guys,

I've been looking at various pieces of code and searches for ages now and still cant make head or tails of where to begin with this. I'm need to work out the driving distances between two given postcodes. I dont need the directions themselves, or the map etc just how far some one would have to drive (and ideally how long it might take) to get from one postcode to the next postcode. Ive seen its fairly easy to do a straight line distance calculation, but I was hoping someone could help me out with some clues or a bit of code to help me work this distance out? I know I can should probably be able to use the bing or Google API, but I'm really struggling!

Any help much appreciated!

Skip :)
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,397
Location
Sydney, Australia
Programming Experience
10+
It took me about 20 seconds to find this with a web search using google maps driving distance postcodes as keywords:

How to Find the Distance Between Two Postcodes/Zipcodes and Driving Time in Current Traffic Using the Google Maps API ? Stanislav Kostadinov

It's not VB.NET code but it demonstrates the principles that you need to implement regardless of language. Another web search produced numerous hits for integrating Google Maps into VB.NET apps, many with YouTube videos showing how it's done.

With that information, you should be able to make an attempt at least. If your attempt doesn't work then you post your code, tell us what you expect to happen and what actually happens.
 

skip21

Member
Joined
Oct 21, 2012
Messages
6
Programming Experience
1-3
So, you were right and it was quite easy to get the information. I'm struggling to extract the information that I receive back though. Can you point me in the right direction? I have used this code (any tips welcomed :05.18-flustered: )

VB.NET:
Imports System.Net


Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim postcode1, postcode2, url As String
            postcode1 = "NG196NN"
            postcode2 = "LE113JB"


            url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=NG196NF&destinations=NG198QU&mode=driving&language=en-EN&sensor=false"


            Using client As New WebClient
                Dim Content As String = client.DownloadString(url)
                TextBox1.Text = Content
            End Using


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub


End Class

I dont know how to extract the driving distance from the information thats return...Any help gratefully received!
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,517
Location
Norway
Programming Experience
10+
You're calling the api with a json request, the returned data is in json format: try a search for "vb.net json" for how to handle json data.

You can also call with a xml request, just change "json" to "xml" in url. The returned data will then be in xml format, which .Net has better built-in support for. For example:
Dim doc = XDocument.Load(url)
Dim distance = doc...<distance>.<text>.Value
 
Top Bottom