Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

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
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away