Use ActiveX Control to Embed World Maps in AutoHotkey GUI

By Directly Loading a Map from OpenStreetMap.org into Your AutoHotkey Graphical User Interface (GUI) Pop-up Window, You Can Add Interactive Geographic Locations to All Your Apps

I have some good news and some bad news about using AutoHotkey tools to directly access Web data through the Internet. First the bad news. Since the AutoHotkey tools for downloading and reading Web pages use Internet Explore (built into Windows but no longer supported by Microsoft), Web providers can effectively block access by identifying that user browser. For the good news, you rarely need to use those sites blocking simple little personal apps such as my IPFind.ahk script. So many other sites support location data for IP addresses that I don’t have a problem keeping the script up and running.

For a quick glance at the geographic location of an IP address, insert an OpenStreetMap.org Web window into an AutoHotkey GUI. Hold the mouse cursor over a map and scroll in or out to zoom in or out.

Previously, I had repaired other issues caused by changes in the source Web page and converted the IPFind.ahk script to use a GUI window rather than a MsgBox command. This upgrade facilitated adding links to the app, see “Adding Web Links to the AutoHotkey IPFind.ahk Script,” as well as making the current insertion of interactive maps using the ActiveX GUI control possible. I fixed the IPFind.ahk script problems by switching to another source Web page and added an interactive map from OpenStreetMap.com.

The Latest Blocking of IPFind.ahk

Recently, after reworking the RegEx in the IPFind.ahk script and reinstating its function, I soon discovered that it once again had lost all its function—returning no information at all. My usual approach provided me with what I thought would correct the problem, but to my surprise, nothing worked.

I could load a sample page into my Web browser and read the source code, but any change I made yielded no results. For some reason, the source code I directly downloaded did not match the source code in my regular browser. I only received a bunch of JavaScript without any of the good stuff.

As a test, I loaded the sample URL into the GUI browser I introduced last time “Build a Barebones Web Browser Using the AutoHotkey ActiveX GUI Control.” The Web site ran a “Prove you’re a human” test in the older ActiveX supported Windows browser. (It seems that the URLDownloadToFile command and other AutoHotkey Web download techniques use these same IE tools.) If I employed any of the AutoHotkey Web download methods or the ActiveX control, the Web site only sent me the blocking JavaScript code. If I used Google Chrome or another modern browser, I received the useful HTML code.

I can understand why Webmasters want to block the apps that scrape data from their sites. When bypassing the browser, AutoHotkey eliminates all opportunities for displaying advertising and engaging in other commercial activities. Using Web data without all the annoying extras effectively eliminates the site’s money-making devices. However, that’s not my problem. I make fair use of the data on a limited basis and the Webmasters are free to hinder my ability to take advantage of what they have made available on the Web.

Rather than attempting to defeat the new protections, I took the easy way out and switched to another site—accessing essentially the same information without all the obstacles. The old site doesn’t need to worry about me anymore (plus, they no longer get the free link from my app to their full Web page). I’ve moved on. I’ve also learned that any scriptwriter using the Web for source data must accept the fact that the script will need updates if and when the source page changes.

Add an Interactive Map

Once I figured out the OpenStreetMap.org URL for locating the right portion of the globe, I coded the expression for writing the address that loads a map into a browser:

https://www.openstreetmap.org/?mlat=" . map1 . "&mlon=" . map2 
                          . "#map=5/" . Map1 . "/" . Map2

Note: The above line uses line continuation techniques to wrap lines of code for display purposes.

The variables map1 and map2 contain the URL longitude and latitude, respectively—drawn from the source Web page. The above ULR loads the OpenStreetMap.org page displaying the area surrounding the marked location set at scale number 5.

Next, AutoHotkey loads the URL into an ActiveX GUI control:

Gui Add, ActiveX, ys w200 h200 vWB%A_Index%, Shell.Explorer
WB%A_Index%.Navigate(WhereIs[3])

Note that the variable name (vWB%A_Index%) concatenates the Loop index number to create a unique object name. The array element WhereIs[3] contains the URL formed in and returned from the function GetLocation(findip).

For a couple of reasons that I plan to explore in the future, I have deliberately avoided Google Maps. For now, see the code for the new IPFindMap.ahk script below:

^!i::
  OldClipboard:= ClipboardAll
  Clipboard:= ""
  Send, ^c ;copies selected text
  ClipWait 0
  If ErrorLevel
    {
      MsgBox, No Text Selected!
      Return
    }
CountIP := 1
Next := 1
Loop
{
  FoundPos := RegExMatch(Clipboard, "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b" , ipaddress%CountIP%, Next)
  Next := FoundPos + StrLen(ipaddress%CountIP%)
  If FoundPos = 0
    Break
  CountIP++
}
Gui, +AlwaysOnTop
If IPAddress1
  {
    CountIP--
    Loop, %CountIP%
    {
       	CheckIP := ipaddress%A_Index%
       	WhereIs := GetLocation(CheckIP)

		WhereIs[1] := StrReplace(WhereIs[1],"Postal code","`rPostal code:`t")
		WhereIs[1] := StrReplace(WhereIs[1],"Region","`rRegion/State:")
		WhereIs[1] := StrReplace(WhereIs[1],"Country","`rCountry:`t`t")
		WhereIs[1] := StrReplace(WhereIs[1],"State:","State:`t")
		WhereIs[1] := StrReplace(WhereIs[1],"Continent","`rContinent:`t")
		WhereIs[1] := StrReplace(WhereIs[1],"Metro code","`rMetro code:`t")
		WhereIs[1] := StrReplace(WhereIs[1],"City","`rCity:`t`t")
		WhereIs[1] := StrReplace(WhereIs[1],"Coordinates","`rCordinates:`t")

		Gui, Add , Text, xm section, % CheckIP . WhereIs[1] 
		Gui Add, ActiveX, ys w200 h200 vWB%A_Index%, Shell.Explorer
		WB%A_Index%.Navigate(WhereIs[3])
		Gui, Add , Link, xs yp+120, % "<a href=""https://tools.keycdn.com/geo?host=" . CheckIp . """>IP Data" . "</a>" 
		Gui, Add , Link, yp+15, % WhereIs[2] 
    }
}
Else
  WhereIs := "No IPs Found!"

Clipboard := OldClipboard

Gui, Add, Button,, Close
Gui, Show,,IP Locations

Return

GetLocation(findip)
{
UrlDownloadToFile, % "https://tools.keycdn.com/geo?host=" . findip, iplocate
	IPsearch := "https://tools.keycdn.com/geo?host=" . findip
	whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	whr.Open("GET", IPsearch)
	whr.Send()
	  sleep 100
	version := whr.ResponseText
	RegExMatch(version, "s)Location</p>(.*?)Time</dt>", Location)
    FileRead, version2, iplocate
	RegExMatch(version, "Coordinates</dt><dd class=""col-8 text-monospace"">(.*?) \(lat\) / (.*?)\(long\)", Map)
	Map := Map1 . "/" . Map2
	MapLink := "<a href=""https://www.openstreetmap.org/?mlat=" . map1 . "&mlon=" . map2 . "#map=5/" . Map . """>Map" . "</a>"
	MapGet := "https://www.openstreetmap.org/?mlat=" . map1 . "&mlon=" . map2 . "#map=5/" . Map1 . "/" . Map2
	Location := RegExReplace(Location1,"<.+?>")
	Return [Location,MapLink,MapGet]
}

ButtonClose:
	WinClose
Return

GuiClose:
	Gui, Destroy
Return

You can also find the IPFindMap.ahk script at the ComputorEdge Free AutoHotkey Scripts page.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

3 thoughts on “Use ActiveX Control to Embed World Maps in AutoHotkey GUI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s