0
1

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.

ExcelVBAからGoogleAPIを呼び出す(GoogleMap編)

Posted at

GoogleMapをExcelVBA(XMLで受信)を使って表示します。

主な流れ

・「http://www.geocoding.jp/」から経度、緯度を検索(XMLで受信)
 http://www.geocoding.jp/api?v=1.1&q=(住所を設定)

・GoogleMapにプロパティを渡して、WebBrowserオブジェクトに表示する。
 http://maps.google.com/maps/api/staticmap?size=600x600&zoom=(拡大率)&sensor=false&markers=(上記geocodingから取得した経度、緯度情報)&maptype=(表示形式)

※表示形式について
 roadmap…道路地図ビュー:
 satellite…航空写真:
 hybrid…通常のビューと航空写真ビューの複合ビュー
 terrain…物理地図

サンプル

googlemap.jpg

ソースコード
Private Sub okButton_Click()
  SpinButton1.Enabled = True
  no = 10
  
  Dim xmldoc As MSXML2.XMLHTTP
  Dim myUri As String
  Set xmldoc = CreateObject("MSXML2.XMLHTTP")

  If addressTextBox.Text = "" Then
    MsgBox "正確な住所を入力してください。"
    Exit Sub
  End If
  
  '住所から座標を取得
  myUri = "http://www.geocoding.jp/api?v=1.1&q=" & addressTextBox.Text
  xmldoc.Open "POST", myUri, False
  xmldoc.send (Null)
  Dim doc As MSXML2.DOMDocument
  Set doc = CreateObject("MSXML2.DOMDocument")
  doc.LoadXML (xmldoc.responseText)
  Dim myNodes As MSXML2.IXMLDOMNodeList
  Set myNodes = doc.SelectNodes("result/coordinate")
  Dim i As Integer
  For i = 0 To myNodes.Length - 1
    myLatitude = myNodes(i).ChildNodes(0).Text
    myLongitude = myNodes(i).ChildNodes(1).Text
  Next
  Call Exe_Map
  
End Sub

'GoogleMapを表示
Private Sub Exe_Map()
  
  myAddressCoodinatePosition = myLatitude & "," & myLongitude
  Dim googleUri As String
  Dim myGoogleMapsUri As String
  Dim googleXmldoc As MSXML2.XMLHTTP
  Set googleXmldoc = CreateObject("MSXML2.XMLHTTP")
  
  'GoogleMapを表示
  googleUri = "http://maps.google.com/maps/api/staticmap?size=600x600&zoom=" & no & "&sensor=false&markers=" & myAddressCoodinatePosition & "&maptype=" & myMapType
  WebBrowser.Navigate googleUri

End Sub

'地図の縮小
Private Sub SpinButton1_SpinDown()
    If no <= 10 Then
        no = 10
        Exit Sub
    Else
        no = no - 1
        Call Exe_Map
    End If

End Sub

'地図の拡大
Private Sub SpinButton1_SpinUp()
    no = no + 1
    Call Exe_Map
End Sub

'道路地図ビュー(roadmap)
Private Sub OptionButton1_Click()
  okButton.Enabled = True
  myMapType = OptionButton1.Caption '値=roadmap
  Call Exe_Map
End Sub

'航空写真(satellite)
Private Sub OptionButton2_Click()
  okButton.Enabled = True
  myMapType = OptionButton2.Caption '値=satellite
  Call Exe_Map
End Sub

'通常のビューと航空写真ビューの複合ビュー(hybrid)
Private Sub OptionButton3_Click()
  okButton.Enabled = True
  myMapType = OptionButton3.Caption '値=hybrid
  Call Exe_Map
End Sub

'物理地図(terrain)
Private Sub OptionButton4_Click()
  okButton.Enabled = True
  myMapType = OptionButton4.Caption '=terrain
  Call Exe_Map
End Sub

実行結果
googlemap_exe.jpg

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?