Web Browser Control: Google Map

Started by José Roca, August 20, 2011, 10:45:02 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
The following example demonstrates how to display a Google Map in an instance of the Web Browser control embeded in a PowerBASIC application.


' ########################################################################################
' Microsoft Windows
' File: CW_WB_GoogleMap.bas
' Contents: WebBrowser example - Google map.
' Compilers: PBWIN 10.02+, PBCC 6.02+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1

' // Include files for external files
%USEWEBBROWSER = 1            ' // Use the WebBrowser control
#INCLUDE ONCE "CWindow.inc"   ' // CWindow class

' // Identifier
%IDC_WEBBROWSER = 1001

' ########################################################################################
' Main
' ########################################################################################
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Set process DPI aware
'   SetProcessDPIAware

   ' // Create an instance of the class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   pWindow.CreateWindow(%NULL, "Web Browser Template: Google Map", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 700, 500
   ' // Center the window
   pWindow.CenterWindow
   ' Web Browser zoom
'   IF pWindow.IsProcessDPIAware THEN pWindow.WBZoom = 100 * pWindow.rxRatio

   ' // Build the html script
   LOCAL s AS WSTRING
   LOCAL cx, cy AS DOUBLE
   LOCAL zoom AS LONG
   cx = 39.47#
   cy = 0.28#
   zoom = 7

   s  = "<!DOCTYPE html>"
   s += "<html>"
   s += "<head>"
   s += "<meta name='viewport' content='initial-scale=1.0, user-scalable=no' />"
   s += "<style type='text/css'>" & $CRLF
   s += "html { height: 100% }" & $CRLF
   s += "  body { height: 100%; margin: 0px; padding: 0px }" & $CRLF
   s += "  #map_canvas { height: 100% }" & $CRLF
   s += "</style>" & $CRLF
   s += "<script type='text/javascript'" & $CRLF
   s += "    src='http://maps.google.com/maps/api/js?sensor=false'>" & $CRLF
   s += "</script>" & $CRLF
   s += "<script type='text/javascript'>" & $CRLF
   s += "  function initialize() {" & $CRLF
   s += "    var latlng = new google.maps.LatLng(" & FORMAT$(cx) & "," & FORMAT$(cy) & ");" & $CRLF
   s += "    var myOptions = {" & $CRLF
   s += "      zoom: " & FORMAT$(zoom) & "," & $CRLF
   s += "      center: latlng," & $CRLF
   s += "      mapTypeId: google.maps.MapTypeId.ROADMAP" & $CRLF
   s += "    };" & $CRLF
   s += "    var map = new google.maps.Map(document.getElementById('map_canvas')," & $CRLF
   s += "        myOptions);" & $CRLF
   s += "  }" & $CRLF
   s += "</script>" & $CRLF
   s += "</head>" & $CRLF
   s += "<body scroll='no' onload='initialize()'>" & $CRLF
   s += "  <div id='map_canvas' style='width:100%; height:100%'></div>" & $CRLF
   s += "</body>" & $CRLF
   s += "</html>" & $CRLF

   ' // Save the script as a temporary file
   LOCAL bstrTempFileName AS WSTRING
   bstrTempFileName = AfxSaveTempFile(s, "", "TMP", "html", 1)

   ' // Create the WebBrowser control with the embedded map
   pWindow.AddWebBrowserControl(pWindow.hwnd, %IDC_WEBBROWSER, bstrTempFileName, NOTHING, 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ########################################################################################

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   STATIC hInstance AS DWORD        ' // Instance handle
   STATIC lpc AS CREATESTRUCT PTR   ' // Pointer to the creation parameters
   STATIC pWindow AS IWindow        ' // Reference to the IWindow interface

   SELECT CASE uMsg

      CASE %WM_CREATE
         ' // Pointer to the creation parameters
         lpc = lParam
         ' // Instance handle
         hInstance = @lpc.hInstance
         ' // Get a reference to the IWindow interface from the CREATESTRUCT structure
         pWindow = CWindow_GetObjectFromCreateStruct(lParam)
         EXIT FUNCTION

      CASE %WM_SYSCOMMAND
         ' // Capture this message and send a WM_CLOSE message
         ' // Note: Needed with some OCXs that, otherwise, remain in memory
         IF (wParam AND &HFFF0) = %SC_CLOSE THEN
            SendMessage hwnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
         END SELECT

      CASE %WM_SIZE
         IF wParam <> %SIZE_MINIMIZED THEN
            ' // Resize the control
            pWindow.MoveWindow GetDlgItem(hwnd, %IDC_WEBBROWSER), 0, 0, pWindow.ClientWidth, pWindow.ClientHeight, %TRUE
         END IF

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================


Douglas Martin

How can I get the contents of the browser control?  I have an app that uses a browser control and google maps api to produce a map with a turn by turn driving directions.  I want to get the directions and print them on a printer or send them in a text message.

I have tried.

SUB PTGMAPIT_BTNPRINT_Events( MyID&, CMsg&, CVal&, Cancel&)
Local txt$, hDlg AS LONG
     SELECT CASE CMsg&
          CASE %EZ_Click
            hDlg = EZ_Handle("PTGMAPIT",0)
            CONTROL GET TEXT hDlg, %IDC_WEBBROWSER TO txt$
            EZ_Dprint txt$
'            EZ_GetText "PTGMAPIT", %PTGMAPIT_BTNPRINT
          CASE ELSE
     END SELECT
END SUB

I just get "SHELL.EXPLORER" in the txt variable.
Doug

José Roca

No idea. Probably you will need to use some javascript code.