We Recommend

Beginning VB.NET Beginning VB.NET
Visual Basic .NET is the latest version of the most widely used programming language in the world, popular with professional developers and complete beginners alike. This book will teach you Visual Basic .NET from first principles. You'll quickly and easily learn how to write Visual Basic .NET code and create attractive windows and forms for the users of your applications.


Posted By

blackf0rk on 05/12/09


Tagged

google maps longitude latitude coordinates geocode


Versions (?)


Who likes this?

4 people have marked this snippet as a favorite

fuzzylogic
Scooter
rifat
haoji


Get Latitude and Longitude from Address (using Google Maps)


Published in: VB.NET 


Use inType=0 and feed in a specific Google Maps URL to parse out the GeoCoords from the URL e.g. http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=53154&sll=37.0625,-95.677068&sspn=52.505328,80.507812&ie=UTF8&ll=42.858224,-88.000832&spn=0.047943,0.078621&t=h&z=14 Function returns a string of geocoords (e.g. "-87.9010610,42.8864960")

Use inType=1 and feed in a zip code, address, or business name Function returns a string of geocoords (e.g. "-87.9010610,42.8864960") If an invalid address, zip code or location was entered, the function will return "0,0"

  1. Public Function GetGeoCoords(ByVal inString As String, ByVal inType As Integer) As String
  2. ' Explanation of function:
  3. ' Use inType=0 and feed in a specific Google Maps URL to parse out the GeoCoords from the URL
  4. ' e.g. http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=53154&sll=37.0625,-95.677068&sspn=52.505328,80.507812&ie=UTF8&ll=42.858224,-88.000832&spn=0.047943,0.078621&t=h&z=14
  5. ' Function returns a string of geocoords (e.g. "-87.9010610,42.8864960")
  6. '
  7. ' Use inType=1 and feed in a zip code, address, or business name
  8. ' Function returns a string of geocoords (e.g. "-87.9010610,42.8864960")
  9. ' If an invalid address, zip code or location was entered, the function will return "0,0"
  10.  
  11. Dim Chunks As String()
  12. Dim outString As String = ""
  13.  
  14. If inType = 0 Then
  15. Chunks = Regex.Split(inString, "&")
  16. For Each s As String In Chunks
  17. If InStr(s, "ll") > 0 Then outString = s
  18. Next
  19. outString = Replace(Replace(outString, "sll=", ""), "ll=", "")
  20. Else
  21. Dim xmlString As String = GetHTML("http://maps.google.com/maps/geo?output=xml&key=abcdefg&q=" & inString, 1)
  22. Chunks = Regex.Split(xmlString, "coordinates>", RegexOptions.Multiline)
  23. If Chunks.Count > 1 Then
  24. outString = Replace(Chunks(1), ",0</", "")
  25. Else
  26. outString = "0,0"
  27. End If
  28.  
  29. End If
  30. Return outString
  31. End Function
  32.  
  33. Public Function GetHTML(ByVal sURL As String, ByVal e As Integer) As String
  34. Dim oHttpWebRequest As System.Net.HttpWebRequest
  35. Dim oStream As System.IO.Stream
  36. Dim sChunk As String
  37. oHttpWebRequest = (System.Net.HttpWebRequest.Create(sURL))
  38. Dim oHttpWebResponse As System.Net.WebResponse = oHttpWebRequest.GetResponse()
  39. oStream = oHttpWebResponse.GetResponseStream
  40. sChunk = New System.IO.StreamReader(oStream).ReadToEnd()
  41. oStream.Close()
  42. oHttpWebResponse.Close()
  43. If e = 0 Then
  44. Return Server.HtmlEncode(sChunk)
  45. Else
  46. Return Server.HtmlDecode(sChunk)
  47. End If
  48. End Function

Report this snippet 

You need to login to post a comment.