24 เมษายน 2555

VB.Net Google Maps ตอนหาค่า Latitude และ Longitude

หลังจากที่ได้ทำการเขียนเรื่อง Google Maps ในตอนแรกแล้ว มีผู้อ่านบางท่านสนใจที่จะหาตำแหน่ง Latitude และ Longitude จากที่อยู่ ว่าทำได้หรือไม่ เผอิญมีตัวอย่างที่เว็บต่างประเทศเขาทำไว้น่าสนใจ เลยนำมาทำเป็นในรูปแบบไทย ๆ ให้ผู้สนใจได้ทำการศึกษาและนำไปต่อยอดตามต้องการ
(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 ไปใช้งานได้ตามต้องการ

อะไรเอ่ย หาไม่ยาก ตอบ หายี่ห้อโทรศัพท์
แต่หากจะหาคนส่งภาพในสภา นี่สิหายากที่สุด

11 เมษายน 2555

VB.Net การสร้างรายงานตอนที่ 2

     การสร้างรายงานในตอนที่ 2 นี้จะนำเสนอวิธีการกรองข้อมูลตามช่วงข้อมูลที่กำหนด แล้วนำมาออกรายงาน เราจะได้เรียนรู้วิธีการสร้าง Quey ภายใต้ DataSet และการเรียกใช้ ตลอดจนการอ้างถึงรายงานจากภายนอกด้วยการเขียนโปรแกรม เพื่อนำรายงานมาแสดงในคอนโทรล ReportViewer
     หากยังไม่เคยดูการสร้างรายงาน ตอนที่ 1 กรุณากลับไปศึกษาให้เข้าใจก่อนแล้วกลับมาดูวีดีโอนี้อีกครั้ง




โค้ดคำสั่ง VB.Net

Imports Microsoft.Reporting.WinForms

Public Class Form2
      Private adapter As dsABACUSTableAdapters.ABAssetTableAdapter = New   _
                              dsABACUSTableAdapters.ABAssetTableAdapter()
      Private table As New dsABACUS.ABAssetDataTable()
      Private MyNewDataSource As New ReportDataSource("DataSet1",  _
                                                                 DirectCast(table, DataTable))
      Private Sub btnRun_Click(ByVal sender As System.Object,  _
                                                 ByVal e As System.EventArgs) _
                                                 Handles btnRun.Click
           adapter.FillByType(table, txtFrom.Text.ToString.Trim, txtTo.Text.ToString.Trim)
           Me.ReportViewer1.LocalReport.ReportPath = ".\Reports\rptABAsset.rdlc"
           Me.ReportViewer1.LocalReport.DataSources.Clear()
           Me.ReportViewer1.LocalReport.DataSources.Add(MyNewDataSource)
           Me.ReportViewer1.LocalReport.Refresh()
           Me.ReportViewer1.RefreshReport()
     End Sub
End Class

สวัสดี สงกรานต์ปีนี้ ไม่ต้องไปฟ้องครู "อังคณา" .... น่ะ
"I Believe in You"

Copyright(c) 2007 - 2022 by Kasem Kamolchaipisit.