|
Ever wanted to link from an open ArcMap project directly to a Google Maps or Google Maps Street View (available in the Wasatch Front urban areas) in a browser? The code below can be paired with an ArcMap custom tool to open your default browser to a Google map at the location at which your ArcMap dataframe was clicked. The default link is to open a north-facing Street View window in the google map if available. Here's how Part I. Paste code into VBA Editor 1) From the ArcMap menu, navigate to Tools --> Macros --> Visual Basic Editor to open the VBA editor 2) In the VBA Editor's Project Window, expand the Project --> Arcmap Objects folder and double click on ThisDocument 3) Paste the code (below) in the window that opens and close the VBA editor. 4) Save your project Part II. Create and configure custom ArcMap Tool
1) From the ArcMap menu, navigate to Tools -->Customize 2) Click the Commands tab. 3) Click the drop-down arrow on the Save in combo box, then change the selected item so it is the current name of your ArcMap project (ex. Untitled.mxd) 4) Click [UIControls] in the Categories list. then Click the New UIControl button. 5) Click the UIToolControl radio button and then click Create. 6) The name of the new tool appears in the Commands list. Single click the newly created UIControl, then click it again to activate in-place editing, then type a new name for the control so it reads Project.GoogleMap_Link. 7) Click and drag the newly created UIControl and drop it on an ArcMap toolbar or menu. 8) On the toolbar or menu, right-click the command to set its image, caption, and other properties. 9) Save Project and start using the tool. Note: Your ArcMap dataframe cannot have an undefined spatial reference system.
Option Explicit
Const SW_SHOWMAXIMIZED = 3 Const SW_SHOWMINIMIZED = 2 Const SW_SHOWDEFAULT = 10 Const SW_SHOWMINNOACTIVE = 7 Const SW_SHOWNORMAL = 1
Private Declare Function ShellExecute Lib "shell32.dll" _ Alias "ShellExecuteA" (ByVal hWnd As Long, _ ByVal lpOperation As String, ByVal lpFile As String, _ ByVal lpParameters As String, ByVal lpDirectory As String, _ ByVal nShowCmd As Long) As Long
Private Function OpenLocation(URL As String, WinState As Long) As Long
'PURPOSE: Opens default browser to display URL
'RETURNS: module handle to executed application or 'Error Code ( < 32) if there is an error
'can also be used to open any document associated with 'an application on the system (e.g., passing the name 'of a file with a .doc extension will open that file in Word)
Dim lHWnd As Long Dim lAns As Long
lAns = ShellExecute(lHWnd, "open", URL, vbNullString, _ vbNullString, WinState) OpenLocation = lAns
'ALTERNATIVE: if not interested in module handle or error 'code change return value to boolean; then the above line 'becomes:
'OpenLocation = (lAns > 32)
End Function
Private Sub GoogleMap_Link_MouseDown(ByVal button As Long, ByVal shift _ As Long, ByVal x As Long, ByVal y As Long) Dim pMxDoc As IMxDocument Dim pApp As IMxApplication Dim pMap As IMap Dim pPoint As IPoint Dim pSpatialReferenceFactory As ISpatialReferenceFactory Dim pSpatialReference As ISpatialReference Set pMxDoc = ThisDocument Set pMap = pMxDoc.FocusMap Set pApp = Application
Set pSpatialReferenceFactory = New SpatialReferenceEnvironment Set pSpatialReference = pSpatialReferenceFactory. _ CreateGeographicCoordinateSystem(esriSRGeoCS_WGS1984) ' convert mouse click to map units Set pPoint = pApp.Display.DisplayTransformation.ToMapPoint(x, y) Set pPoint.SpatialReference = pMap.SpatialReference If pPoint.SpatialReference.Name <> pSpatialReference.Name Then pPoint.Project pSpatialReference End If Dim URLstr As String Dim returnLong As Long 'more info on google map URL query string request setting parameters: 'http://mapki.com/index.php?title=Google_Map_Parameters 'STREET VIEW USE THIS FUNCTION URLstr = "http://www.google.com/maps?ie=UTF8&layer=c&cbll=" & _ pPoint.y & "," & pPoint.x & "&cbp=1,0,,0,5&ll=" & pPoint.y _ & "," & pPoint.x & "&z=16" 'REGULAR MAP USE THIS FUNCTION ... NO STREETVIEW WINDOW 'URLstr = "http://www.google.com/maps?ie=UTF8&ll=" & pPoint.y _ & "," & pPoint.x & "&spn=0.015045,0.016844&z=16" 'Use one of the constants as the window state parameter returnLong = OpenLocation(URLstr, SW_SHOWNORMAL)
End Sub |