Get geocoding information from Google Maps API V3 in asp.net

We spent quite a while searching round for a bit of code...

share on facebook share on twitter share on google share on digg share on linked in share on stumble upon

We spent quite a while searching round for a bit of code where we can input a location and get the returning Long/Lat straight out of Google via its API service. Although there is plenty of code available we wanted to keep it simple and have in ASP.NET (VB). 

It is made up of just a couple of functions. 
See the code below for instructions:

Imports System.Xml
Public Function GetGeoCoords(ByVal inString As String, 
ByVal inType As Integer) As String Dim Chunks As String() Dim outString As String = "" If inType = 0 Then Chunks = Regex.Split(inString, "&") For Each s As String In Chunks If InStr(s, "ll") > 0 Then outString = s Next outString = Replace(Replace(outString, "sll=", ""),
"ll=", "") Else Dim lat As String = "0" Dim lng As String = "0" Try Dim xmlString As String = GetHTML(
"http://maps.googleapis.com/maps/api/geocode/xml?address="
& inString & ",&sensor=true", 1) Dim xmldoc As XmlDocument = New XmlDocument() xmldoc.LoadXml(xmlString) lat = xmldoc.SelectSingleNode(
"//GeocodeResponse/result/geometry/location/lat").InnerText lng = xmldoc.SelectSingleNode(
"//GeocodeResponse/result/geometry/location/lng").InnerText Catch ex As Exception End Try outString = lng + "," + lat End If Return outString End Function Public Function GetHTML(ByVal sURL As String, ByVal e As
Integer) As String Dim oHttpWebRequest As System.Net.HttpWebRequest Dim oStream As System.IO.Stream Dim sChunk As String oHttpWebRequest = (System.Net.HttpWebRequest.Create(sURL)) Dim oHttpWebResponse As System.Net.WebResponse =
oHttpWebRequest.GetResponse() oStream = oHttpWebResponse.GetResponseStream sChunk = New System.IO.StreamReader(oStream).ReadToEnd() oStream.Close() oHttpWebResponse.Close() If e = 0 Then Return Server.HtmlEncode(sChunk) Else Return Server.HtmlDecode(sChunk) End If End Function

Usage Instructions: 
Simply pass into the function something like this: 

litResults.Text = GetGeoCoords(“london”, 1) 

and you should get the long and lat for that location. 

Enjoy!

Get geocoding information from Google Maps API V3 in asp.net

Posted in Coding by on 05 January 2014

Related Posts

What Are CSS Shapes?
WHAT ARE CSS SHAPES?
Erm...What is CAPTCHA?
ERM...WHAT IS CAPTCHA?
How To Create A Link To A Phone Number In HTML
HOW TO CREATE A LINK TO A PHONE NUMBER IN HTML
Special HTML Characters
SPECIAL HTML CHARACTERS
Best Practice - Caching Files Using ASP Dot Net
BEST PRACTICE - CACHING FILES USING ASP DOT NET
Adding A Canonical Tag To An ASP.NET page
ADDING A CANONICAL TAG TO AN ASP.NET PAGE

Fresh From Twitter...

Join the conversation on Twitter @pebbleltd
for all the latest news on web design, ecommerce and search engine marketing with one of the leading web design agencies in the uk

  • Pebble (Peterborough)

    26 Tesla Court
    Innovation Way
    Peterborough
    PE2 6FL

    Tel: 01733 902070

    View On Map

  • Pebble (Kings Langley)

    The Old Chapel
    69 Primrose Hill
    Kings Langley
    WD4 8HX

    Tel: 01442 505878

    View On Map

Pebble Ltd is registered in England & Wales: 06257777 and registered for VAT No: 993 3500 06

Pebble Ltd is Data Protection registered ZA122783

Get geocoding information from Google Maps API V3 in asp.net Get geocoding information from Google Maps API V3 in asp.net We spent quite a while searching round for a bit of code...

We spent quite a while searching round for a bit of code where we can input a location and get the returning Long/Lat straight out of Google via its API service. Although there is plenty of code available we wanted to keep it simple and have in ASP.NET (VB). 

It is made up of just a couple of functions. 
See the code below for instructions:

Imports System.Xml
Public Function GetGeoCoords(ByVal inString As String, 
ByVal inType As Integer) As String Dim Chunks As String() Dim outString As String = "" If inType = 0 Then Chunks = Regex.Split(inString, "&") For Each s As String In Chunks If InStr(s, "ll") > 0 Then outString = s Next outString = Replace(Replace(outString, "sll=", ""),
"ll=", "") Else Dim lat As String = "0" Dim lng As String = "0" Try Dim xmlString As String = GetHTML(
"http://maps.googleapis.com/maps/api/geocode/xml?address="
& inString & ",&sensor=true", 1) Dim xmldoc As XmlDocument = New XmlDocument() xmldoc.LoadXml(xmlString) lat = xmldoc.SelectSingleNode(
"//GeocodeResponse/result/geometry/location/lat").InnerText lng = xmldoc.SelectSingleNode(
"//GeocodeResponse/result/geometry/location/lng").InnerText Catch ex As Exception End Try outString = lng + "," + lat End If Return outString End Function Public Function GetHTML(ByVal sURL As String, ByVal e As
Integer) As String Dim oHttpWebRequest As System.Net.HttpWebRequest Dim oStream As System.IO.Stream Dim sChunk As String oHttpWebRequest = (System.Net.HttpWebRequest.Create(sURL)) Dim oHttpWebResponse As System.Net.WebResponse =
oHttpWebRequest.GetResponse() oStream = oHttpWebResponse.GetResponseStream sChunk = New System.IO.StreamReader(oStream).ReadToEnd() oStream.Close() oHttpWebResponse.Close() If e = 0 Then Return Server.HtmlEncode(sChunk) Else Return Server.HtmlDecode(sChunk) End If End Function

Usage Instructions: 
Simply pass into the function something like this: 

litResults.Text = GetGeoCoords(“london”, 1) 

and you should get the long and lat for that location. 

Enjoy!

Contact Pebble

  • Pebble Ltd (Peterborough)
    26 Tesla Court
    Innovation Way
    Peterborough
    PE2 6FL
    Tel: 01733 902070
  • Pebble Ltd (Kings Langley)
    The Old Chapel
    69 Primrose Hill
    Kings Langley
    WD4 8HX
    Tel: 01442 505878