Access 2013 VBA获取AA里程计算器谷歌地图图片技术求助
Capturing the Static Google Map from AA Mileage Calculator into Access Form
Hey there! Let's get that static map from the AA site into your Access form. I'll break this down into simple, actionable steps that fit right into your existing VBA code.
First, Add the Required API Declaration
You'll need this to download the map image from its URL to a local file (Access image controls work best with local files). Stick this at the top of your module (before any subs/functions):
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _ Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, _ ByVal szURL As String, ByVal szFileName As String, _ ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As LongPtr Private Function DownloadFile(url As String, localPath As String) As Boolean ' Returns True if download succeeds Dim result As LongPtr result = URLDownloadToFile(0, url, localPath, 0, 0) DownloadFile = (result = 0) End Function
Now, Insert the Map Capture Code
Place this right where you commented 'Google image code to go here after map page loads?:
' Wait for the map container to load (adjust timeout if needed) Dim mapImg As HTMLImg Dim waitTime As Integer waitTime = 0 Do DoEvents ' Try to find the map image - adjust the selector based on what you see in DOM On Error Resume Next Set mapImg = doc.querySelector(".gmnoprint img") ' First try gmnoprint div's img If mapImg Is Nothing Then Set mapImg = doc.querySelector("#mapContainer img") ' Fallback to mapContainer End If On Error GoTo Errorhandler waitTime = waitTime + 1 If waitTime > 30 Then ' Wait up to 30 seconds MsgBox "Map image timed out loading." GoTo Cleanup End If Loop While mapImg Is Nothing Or mapImg.src = "" ' Download the image to a temporary file (we'll use Windows temp folder) Dim tempPath As String tempPath = Environ("TEMP") & "\AA_Route_Map_" & Format(Now(), "YYYYMMDDHHMMSS") & ".png" If DownloadFile(mapImg.src, tempPath) Then ' Bind the image to your Access form's image control (replace "imgRouteMap" with your control name) Me.imgRouteMap.Picture = tempPath MsgBox "Map successfully added to form!" Else MsgBox "Failed to download map image." End If Cleanup: Set mapImg = Nothing
Key Notes for Your Setup:
- Adjust the Selectors: If
.gmnoprint imgor#mapContainer imgdon't find the image, use your DOM Explorer to locate the exact img tag. Look for an img with asrcstarting withhttps://maps.googleapis.com/maps/api/staticmap— that's the static map image. - Image Control Name: Replace
imgRouteMapwith the actual name of the image control on your Access form. - Timeout: The 30-second wait should be enough, but if your internet is slow, you can increase
waitTime > 30to a higher number. - Temporary Files: The code saves the map to your Windows temp folder. If you want to keep images permanently, change
tempPathto a folder in your database directory.
Updated Full Snippet Context
Here's how it fits into your existing code (the new part is marked):
Dim IE As New InternetExplorer Dim doc As HTMLDocument Dim RDate As String On Error GoTo Errorhandler RDate = Me.TxtRouteDate '................................................................ Select Case Me.TxtNoSum Case Is = IsNull(Me.TxtPC1) Or IsNull(Me.TxtPC2) MsgBox "Please Enter at least 2 Postcodes" Case Is <= 2 MsgBox "Please Enter at least 2 Postcodes" Case Is = 3 IE.Visible = True IE.Navigate "https://www.theaa.com/driving/mileage-calculator.jsp" IE.Top = 20 IE.Left = 50 IE.Height = 1300 IE.Width = CInt(Int((1000 * Rnd()) + 1)) While IE.Busy DoEvents Wend Set doc = IE.Document With doc 'Clears previous searches doc.all("mcDeleteRoutesLink").Click 'Inserts info into web site text boxes. doc.getElementById("RouteDate").Value = RDate doc.getElementById("RouteFrom").Value = Me.TxtPC1 doc.getElementById("RouteTo").Value = Me.TxtPC2 doc.getElementById("fuelPricePerLitre").Value = Me.TxtPPL End With While IE.Busy DoEvents Wend 'Clicks "Get Route" Button Dim btn As Object Set btn = doc.getElementById("getRouteWrapper").Children(0) If Not btn Is Nothing Then btn.Click While IE.Busy DoEvents Wend ' Inserts distance recorded by AA mileage calculator into text control on my form Set htmlDist = doc.all("routeDistanceTotal") If Not htmlDist Is Nothing Then Me.TxtDistance = htmlDist.innerText End If ' ------------------- NEW MAP CAPTURE CODE STARTS HERE ------------------- ' Wait for the map container to load (adjust timeout if needed) Dim mapImg As HTMLImg Dim waitTime As Integer waitTime = 0 Do DoEvents ' Try to find the map image - adjust the selector based on what you see in DOM On Error Resume Next Set mapImg = doc.querySelector(".gmnoprint img") ' First try gmnoprint div's img If mapImg Is Nothing Then Set mapImg = doc.querySelector("#mapContainer img") ' Fallback to mapContainer End If On Error GoTo Errorhandler waitTime = waitTime + 1 If waitTime > 30 Then ' Wait up to 30 seconds MsgBox "Map image timed out loading." GoTo Cleanup End If Loop While mapImg Is Nothing Or mapImg.src = "" ' Download the image to a temporary file (we'll use Windows temp folder) Dim tempPath As String tempPath = Environ("TEMP") & "\AA_Route_Map_" & Format(Now(), "YYYYMMDDHHMMSS") & ".png" If DownloadFile(mapImg.src, tempPath) Then ' Bind the image to your Access form's image control (replace "imgRouteMap" with your control name) Me.imgRouteMap.Picture = tempPath MsgBox "Map successfully added to form!" Else MsgBox "Failed to download map image." End If Cleanup: Set mapImg = Nothing ' ------------------- NEW MAP CAPTURE CODE ENDS HERE ------------------- End If ' (Next Case)
Troubleshooting Tips:
- If
querySelectordoesn't work, trygetElementsByClassName("gmnoprint")(0).getElementsByTagName("img")(0)— older IE versions sometimes prefer these methods. - Verify the image's
srcis accessible: Copy the URL from DOM Explorer and paste it into a browser to confirm it loads. - Adjust your Access form's image control to "Zoom" or "Stretch" if needed to fit the map properly.
内容的提问来源于stack exchange,提问作者GMan63
相关产品推荐
相关产品推荐

