excel - 将 Google 街景图像嵌入 Excel

标签 excel vba google-maps google-maps-api-3

我正在尝试将 Google 街景嵌入 Excel。我找到了 link它具有以下代码。对我来说根本不起作用,并寻求一些帮助来开始。显然,我需要为街景 URL 的查找设置变量。但我从未通过 VBA 插入图像,寻求一些指导。

Sub GoogleStaticStreetView(oShape As Shape, _
                        sAddress As String, _
                        lHeading As Long, _
                        Optional lHeight As Long = 512, _
                        Optional lWidth As Long = 512)

    'https://developers.google.com/maps/documentation/streetview/

    Dim sURL As String
    Dim sMapsURL As String

    On Error GoTo RETURN_FALSE

    If bRunMode Then On Error Resume Next 'Error if quota exceeded

    If Len(sAddress) > 0 Then
        'URL-Escaped addresses
        sAddress = Replace(sAddress, " ", "+")
    Else
        Exit Sub
    End If

    sURL = _
    "http://maps.googleapis.com/maps/api/streetview?" & _
    "&location=" & sAddress & _
    "&size=" & lWidth & "x" & lHeight & _
    "&heading=" & lHeading & _
    "&sensor=false"

    sMapsURL = "http://maps.google.com/maps?q=" & _
    sAddress & "&t=m&layer=c&panoid=0" & _
    "&cbp=12," & lHeading & ",,0,4.18"

    oShape.Fill.UserPicture sURL
    oShape.AlternativeText = sMapsURL

    Exit Sub

RETURN_FALSE:

End Sub

Sub GoogleStaticMap(oShape As Shape, _
                    sAddress As String, _
                    Optional sMapType As String = "roadmap", _
                    Optional lZoom As Long = 12, _
                    Optional lHeight As Long = 512, _
                    Optional lWidth As Long = 512)

    'https://developers.google.com/maps/documentation/staticmaps/

    Dim sURL As String
    Dim sMapsURL As String
    Dim sMapTypeURL As String

    On Error GoTo RETURN_FALSE

    ' Google Maps Parameters '&t=m' = roadmap, '&t=k' = satellite
    sMapTypeURL = "m"
    If sMapType = "satellite" Then
        sMapTypeURL = "k"
    End If

    If bRunMode Then On Error Resume Next 'Error if quota exceeded

    If Len(sAddress) > 0 Then
        'URL-Escaped addresses
        sAddress = Replace(sAddress, " ", "+")
    Else
        Exit Sub
    End If

    sURL = _
    "http://maps.googleapis.com/maps/api/staticmap?center=" & _
    sAddress & "," & _
    "&maptype=" & sMapType & _
    "&markers=color:green%7Clabel:%7C" & sAddress & _
    "&zoom=" & lZoom & _
    "&size=" & lWidth & "x" & lHeight & _
    "&sensor=false" & _
    "&scale=1"

    sMapsURL = "http://maps.google.com/maps?q=" & _
    sAddress & _
    "&z=" & lZoom & _
    "&t=" & sMapTypeURL

    oShape.Fill.UserPicture sURL
    oShape.AlternativeText = sMapsURL

    Exit Sub

RETURN_FALSE:

End Sub

最佳答案

您可以通过将此行添加到 GoogleStaticStreetView 中的其他 Dims 来使该代码正常工作:

Dim bRunMode As Boolean

然后运行这个模块:
Sub makeThisCodeWork()
    GoogleStaticStreetView Sheets(1).Shapes.AddShape(msoShapeRectangle, 0, 0, 512, 512), "GooglePlex, CA 94043", 100
    Debug.Assert False
    Sheets(1).Shapes.Delete
End Sub

这只是创建一个矩形对象用作容器,然后让代码将图像粘贴进去。

当它到达 debug.assert false 时它将暂停执行,然后它将删除工作表上的所有形状,以便您可以再次清理运行它。您必须使用地址和标题变量才能获得您想要的东西。

我没有尝试运行其他模块,因为那是用于返回 map ,而您只是说 StreetView :)

希望这会有所帮助 - 如果您希望我更详细/解释这里发生的事情,请告诉我。

关于excel - 将 Google 街景图像嵌入 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33573556/

相关文章:

Excel 应用程序事件 : How to automatically reset WithEvents variable after a runtime error?

vba - 无法让 vba 函数返回正确答案

PHP导出带图片的excel

python - 使用xlrd库读取Excel文件时如何检测单元格是否为空?

crystal-reports - 卓越的信心

Excel VBA 传递组合框中值的 ID

excel - 通过按宏按钮将行从一张纸复制并插入到另一张纸上

php - 防止重复的谷歌地图标记

javascript - 在事件监听器中使用闭包时如何在 Google Map 上定义自定义大小

google-maps - 谷歌地图 API v 3 : Heatmap Creation Issue