[ASP.NET][Class] Google Maps 輸入地址取得經緯度 & 取得兩點距離

Standard

需求:Google Maps 輸入地址取得經緯度 & 取得兩點距離
寫成一個陽春的 Class 方便往後使用。

Imports System.Net
Imports System.IO

Namespace tw.patw

    Public Class Maps

        Private _output As String = "csv"
        Private _key As String
        Private _addr As String

        Property key() As String
            Get
                Return _key
            End Get
            Set(ByVal value As String)
                If String.IsNullOrEmpty(value) = False Then
                    _key = value
                Else
                    Throw New Exception("屬性:缺少 Google Maps 金鑰。")
                End If
            End Set
        End Property

        Property addr() As String
            Get
                Return _addr
            End Get
            Set(ByVal value As String)
                If String.IsNullOrEmpty(value) = False Then
                    _addr = value
                Else
                    Throw New Exception("屬性:缺少地址字串。")
                End If
            End Set
        End Property

        ''' <summary>
        ''' 地球半徑
        ''' </summary>
        ''' <remarks></remarks>
        Private Const EARTH_RADIUS As Double = 6378.137

        Private Shared Function rad(ByVal d As Double) As Double
            Return d * Math.PI / 180.0
        End Function

        ''' <summary>
        ''' 取得兩點距離
        ''' </summary>
        ''' <param name="lat1">點1: 經度</param>
        ''' <param name="lng1">點1: 緯度</param>
        ''' <param name="lat2">點2: 經度</param>
        ''' <param name="lng2">點2: 緯度</param>
        ''' <returns>距離 (公里)</returns>
        ''' <remarks></remarks>
        Public Shared Function GetDistance(ByVal lat1 As Double, ByVal lng1 As Double, ByVal lat2 As Double, ByVal lng2 As Double) As Double
            Dim radLat1 As Double = rad(lat1)
            Dim radLat2 As Double = rad(lat2)
            Dim a As Double = radLat1 - radLat2
            Dim b As Double = rad(lng1) - rad(lng2)
            Dim s As Double = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)))
            s = s * EARTH_RADIUS
            s = Math.Round(s * 10000) / 10000
            Return s
        End Function

        ''' <summary>
        ''' 用地址取得經緯度
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function getGeoByAddr() As Array

            If String.IsNullOrEmpty(_key) = True Then
                Throw New Exception("缺少 Google Maps 金鑰。")
            End If

            If String.IsNullOrEmpty(_addr) = True Then
                Throw New Exception("缺少地址字串。")
            End If



            ' 查詢經緯度
            Dim url As String = String.Format("http://maps.google.com/maps/geo?q={0}&output={1}&key={2}", _addr, _output, _key)

            ' 送出要求
            Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)

            ' 取得回應
            Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

            ' 讀取結果
            Dim sr As New StreamReader(response.GetResponseStream())

            Dim tmpArray As String() = sr.ReadToEnd().Split(","c)
            Dim latitude As String = tmpArray(2)
            Dim longitude As String = tmpArray(3)
            Dim geoArray() As String = {latitude, longitude}

            Return geoArray

        End Function

    End Class

End Namespace

用法:(需先 Import 進 Namespace)

傳入地址取得經緯度:

Dim googlemaps = New Maps
googlemaps.key = "GOOGLE MAPS KEY"
googlemaps.addr = "地址"
Response.Write(googlemaps.getGeoByAddr()(0) & "," & googlemaps.getGeoByAddr()(1) & "<br>")

傳入兩點經緯度取得距離:

Dim gmap = New Maps
Response.Write(gmap.GetDistance(40.723063, -74.005129, 25.149876, 121.459631))
'美國紐約 ~ 台北縣淡水

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *