Você está na página 1de 3

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.

Eve
ntArgs) Handles btnSearch.Click
Try
Dim fmqRequest = New GeocodingRequest() 'Request geographical info
fmqRequest.Address = txtSearch.Text 'Address that needs to be converted t
o geographic coordinates
fmqRequest.Sensor = "false" 'No built in location sensor
Dim fmqResponse = GeocodingService.GetResponse(fmqRequest) 'Get response
from request
If fmqResponse.Status = ServiceResponse.Ok Then 'Everything OK, add to Li
stview
lvAddressDetails.Items.Add(fmqResponse.Results.Single().FormattedAddres
s.ToString) 'Main address
lvAddressDetails.Items(0).SubItems.Add(fmqResponse.Results.Single().Geo
metry.Location.Latitude) 'Latitude
lvAddressDetails.Items(0).SubItems.Add(fmqResponse.Results.Single().Geo
metry.Location.Longitude) 'Longitude
End If
Catch ex As System.InvalidOperationException
MessageBox.Show("Please enter Town / Suburb / City with Street Address")
'Improper address
End Try
End Sub
------------------------------------------------------------------------------Code: LatLon.vb
The LatLon class is used merely to contain a latitude/longitude pair returned fr
om the geocoding request. It contains a pair of doubles: Latitude and Longitud
e. The constructor is overloaded to allow you to set the latitude and longitude
upon instantiation of the class if you choose to do so.
Public Class LatLon
Public Property Latitude As Double
Public Property Longitude As Double
Public Sub New()
End Sub
Public Sub New(ByVal lat As Double, ByVal lon As Double)
Me.Latitude = lat
Me.Longitude = lon
End Sub
End Class
Code: Form1.vb
The code behind the demo application consists of a single method used to make th
e geocoding request to Google maps. There is a single button click event handle
r that makes the request and displays the results in a text box. Refer to the d
ocumentation provided by Google for more information on the use of this and othe
r methods supplied through the Google Maps API. The methods are annotated in th
e following:

Imports System.IO
Imports System.Net
Imports System.Web
Imports System.Collections.Generic
Imports System.ComponentModel
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handl
es
MyBase.Load
End Sub
''' <summary>
''' Call the GetLatLon method to geocode a physical address
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btnGeocode_Click(sender As System.Object, e As System.EventArgs)
Handles
btnGeocode.Click
Try
Dim ll As New LatLon()
ll = GetLatLon(txtAddress.Text)
txtLatLon.Text = ll.Latitude.ToString() & ", " & ll.Longitude.ToStri
ng()
Catch ex As Exception
MessageBox.Show(ex.Message, "An error has occurred")
End Try
End Sub
''' <summary>
''' This function makes a call using the Google Maps API to geocode a physic
al
''' address and report the
''' latitude and longitude of the address
'''
''' You will need to obtain a Google Maps API key and plug it into the url s
tring the
''' space indicated in order for this
''' code to execute properly
'''
''' The code could be useful to you if you need to geocode some addresses in
order to
''' display them on a map, for example if
''' your site had a store locator, this code could be used to find the lat/l
on of
''' each store. I would recommend obtaining the
''' addresses and keeping them in a database table rather than querying Goog
le maps
''' for the lat/lon of the address each time it
''' is needed. Google does have limitations upon the number of free queries
it will
''' support, at that, it might be useful to just
''' write an application to loop through all of the addresses, geocode each
address,
''' and then write those captured lat/lon values
''' into your table.
'''
''' Whilst useful, there is nothing particulary interesting about this code;
I think
''' it is pretty well covered in the Google Maps API documentation
''' </summary>

''' <param name="addr"></param>


''' <returns></returns>
''' <remarks></remarks>
Public Function GetLatLon(ByVal addr As String) As LatLon
Dim url As String = "http://maps.google.com/maps/geo?output=csv&key=[YOU
R KEY
GOES HERE]&q=" & addr
Dim request As System.Net.WebRequest = WebRequest.Create(url)
Dim response As HttpWebResponse = request.GetResponse()
If response.StatusCode = HttpStatusCode.OK Then
Dim ms As New System.IO.MemoryStream()
Dim responseStream As System.IO.Stream = response.GetResponseStream(
)
Dim buffer(2048) As Byte
Dim count As Integer = responseStream.Read(buffer, 0, buffer.Length)
While count > 0
ms.Write(buffer, 0, count)
count = responseStream.Read(buffer, 0, buffer.Length)
End While
responseStream.Close()
ms.Close()
Dim responseBytes() As Byte = ms.ToArray()
Dim encoding As New System.Text.ASCIIEncoding()
Dim coords As String = encoding.GetString(responseBytes)
Dim parts() As String = coords.Split(",")
Return New LatLon(Convert.ToDouble(parts(2)), Convert.ToDouble(parts
(3)))
End If
Return Nothing
End Function
End Class

Você também pode gostar