5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VB.NET GoogleGeocodingで住所から座標情報取得

Last updated at Posted at 2016-10-05

大量の住所から座標情報取るときに使ってます。
詳細はここ

GoogleApi.vb
Imports System.Text
Imports System.Web.Script.Serialization

Public Class GoogleAPI
    Private Const APIKEY As String = "ここにGoogleから取得したAPIキーを入力"

    ''' <summary>
    ''' Main method which performs the call to the REST service
    ''' </summary>
    ''' <remarks></remarks>
    Public Function GetGeoCode(fullAddress As String) As GeoResponse
        Try
            Dim req As System.Net.WebRequest = System.Net.WebRequest.Create($"https://maps.googleapis.com/maps/api/geocode/json?address={fullAddress}&key={APIKEY}")
            req.Method = "GET"
            Dim resp As System.Net.WebResponse = req.GetResponse
            If (resp Is Nothing) Then
                Return Nothing
            End If
            Dim sr As System.IO.StreamReader = New System.IO.StreamReader(resp.GetResponseStream)
            Dim jsonData As String = sr.ReadToEnd.Trim

            Dim ser As JavaScriptSerializer = New JavaScriptSerializer
            Dim res = ser.Deserialize(Of GeoResponse)(jsonData)
            Return res
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Class GeoResponse
        Public status As String = ""
        Public results As GeoResult() = {}
    End Class
    Public Class GeoResult
        Public address_components As AddressComponents()
        Public formatted_address As String = ""
        Public geometry As Geometry
        Public partial_match As String = ""
        Public place_id As String = ""
        Public types As String() = {}
    End Class
    Public Class AddressComponents
        Public long_name As String = ""
        Public short_name As String = ""
        Public types As String() = {}
    End Class
    Public Class Geometry
        Public bounds As NESWLocation
        Public location As GeoLocation
        Public location_type As String = ""
        Public viewport As NESWLocation
    End Class
    Public Class NESWLocation
        Public northeast As GeoLocation
        Public southwest As GeoLocation
    End Class
    Public Class GeoLocation
        Public lat As String = ""
        Public lng As String = ""
    End Class
End Class

こんな感じでつかう

Try
    Dim ggl As New GoogleAPI
    For Each address In Addresses
        'APIから情報を取得する。
        Dim geo As GoogleAPI.GeoResponse = ggl.GetGeoCode(address)
        If "OK".Equals(geo.status) Then
            'ロケがとれたとき
        ElseIf "OVER_QUERY_LIMIT".Equals(geo.status) Then
            'クエリ発行上限にひっかかったとき
            Return
        Else
            'ロケが取れなかったとき
        End If
    Next
Catch
    Throw
End try
5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?