(Source : http://www.vbdotnetheaven.com/uploadfile/scottlysle/geocode-an-address-using-visual-basic-and-google-maps/ )
1. หลังจากที่เราสร้างโปรเจกต์เรียบร้อย ที่หน้าจอ Form1 ให้ทำการเพิ่มคอนโทรลเข้าไปดังภาพ
2. ให้คลิกเมาส์ 2 ครั้ง (double Click) ที่ปุ่ม Get GeoCode (Button1) แล้วเปลี่ยนคำสั่งในฟอร์มทั้งหมดให้เป็น ดังนี้
Imports System.IO
Imports System.Net
Imports System.Web
Imports System.Collections.Generic
Imports System.ComponentModel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim ll As New LatLon()
ll = GetLatLon(txtAddress.Text)
txtLatLon.Text = ll.Latitude.ToString() & ", " & ll.Longitude.ToString()
Catch ex As Exception
MessageBox.Show(ex.Message, "An error has occurred")
End Try
End Sub
Public Function GetLatLon(ByVal addr As String) As LatLon
Dim url As String = "http://maps.google.com/maps/geo?output=csv&" & _
"key=[YOUR KEYGOES 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
3. ไปที่เมนู Project เลือกรายการ Add Class...จากนั้น ป้อนชื่อคลาส -> LatLon.vb แล้วคลิก Add
จากนั้น พิมพ์คำสั่งภายใต้คลาด LatLon ดังนี้
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
หลังจากนั้นให้กดปุ่ม F5 เพื่อทำการทดสอบโปรแกรม จะปรากฏหน้าต่างให้เราป้อนที่อยู่ที่ต้องการ ดังภาพ
เราก็สามารถนำค่าของ Latitude และ Longitude ไปใช้งานได้ตามต้องการ
อะไรเอ่ย หาไม่ยาก ตอบ หายี่ห้อโทรศัพท์
แต่หากจะหาคนส่งภาพในสภา นี่สิหายากที่สุด


1 ความคิดเห็น:
แสดงความคิดเห็น