1

Delphi XE6 I have a program using TWebBrowser. I run an extensive JavaScript to plot data on Google Maps. Because it is in a Delphi program, I am using the TWebBrowser.

Everything works great!!! Now my client recently asked for Location tracking feature.

I have tested the script on the same computer using IE 11, running under WebStorm editor and I see the request "localhost wants to track your physical location." pop-up and then I see my message. I know that IE 11 supports it. Since TWebBrowser is simply to make calls to IE, I would expect it to work in my program.

I messed with Chromium and gave up. I can't even get a successful package build. Plus it has been archived at XE5 level. Unless someone can provide me with a proven CEF3, I would just as well stay away from it. TMS offers their own WebBrowser and I will look at it, if this becomes a show-stopper.

But it would seem that if IE supports it, TWebBrowser should also.

Has anyone gotten this to work with IE 9 +?

{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}


unit uMainForm;

interface

uses
  SysUtils, OleCtrls, SHDocVw, Classes, Controls, ComCtrls, Forms,  MSHTML,  Winapi.ActiveX,
  Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TfrmMain = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);

  private
    HTMLWindow2: IHTMLWindow2;

  public

  end;

var
  frmMain: TfrmMain;

implementation

uses dialogs;
{$R *.dfm}

const
HTMLStr: AnsiString =
'  <html> '+
'  <head> '+
'      <script type="text/javascript" '+
'              src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;libraries=geometry&amp;sensor=false"></script> '+
'      <title>Test GeoLocation</title> '+
'  </head> '+
'  <body style="height:100%;margin:0"> '+
' '+//  <!-- Declare the div, make it take up the full document body -->
'  <div id="map-canvas" style="width: 100%; height: 100%;"></div> '+
' '+
'  <script type="text/javascript"> '+
' '+
'    var map; '+
' '+
'    function initialize() {  '+
'        map = new google.maps.Map(document.getElementById(''map-canvas''), { '+
'            center: new google.maps.LatLng(34.0, -84.0), '+
'            zoom: 17, '+
'            streetViewControl: true, '+
'            mapTypeId: google.maps.MapTypeId.ROADMAP,  '+
'            scaleControl: true  '+
'        });  '+
'           if (navigator.geolocation) { '+
'               var infoWindow = new google.maps.InfoWindow({map: map}); '+
'               navigator.geolocation.getCurrentPosition(function(position) { '+
'                   var pos = { '+
'                       lat: position.coords.latitude, '+
'                       lng: position.coords.longitude '+
'                   }; '+
'                   infoWindow.setPosition(pos); '+
'                   infoWindow.setContent("This is where you are"); '+
'                   map.setCenter(pos); '+
'               }, function() { alert("Time out "); '+
'               }); '+
'           } else { '+
'           alert("No can do "); '+
'           }; '+
'    } '+
'    google.maps.event.addDomListener(window, ''load'', initialize); '+
' '+
'  '+
'  </script> '+
'  </body> '+
' '+
'  </html> ';

procedure TfrmMain.FormCreate(Sender: TObject);
var
  aStream: TMemoryStream;
begin
  WebBrowser1.Navigate('about:blank');
  if Assigned(WebBrowser1.Document) then
  begin
    aStream := TMemoryStream.Create;
    try
      aStream.WriteBuffer(Pointer(HTMLStr)^, Length(HTMLStr));
      aStream.Seek(0, soFromBeginning);
      (WebBrowser1.Document as IPersistStreamInit)
        .Load(TStreamAdapter.Create(aStream));
    finally
      freeandnil(aStream);
    end;
    HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
  end;
end;



end.

DFM

object frmMain: TfrmMain
  Left = 192
  Top = 114
  Caption = 'Location find'
  ClientHeight = 598
  ClientWidth = 708
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object WebBrowser1: TWebBrowser
    Left = 0
    Top = 0
    Width = 708
    Height = 598
    Align = alClient
    TabOrder = 0
    ExplicitWidth = 805
    ExplicitHeight = 515
    ControlData = {
      4C0000002D490000CE3D00000000000000000000000000000000000000000000
      000000004C000000000000000000000001000000E0D057007335CF11AE690800
      2B2E126208000000000000004C0000000114020000000000C000000000000046
      8000000000000000000000000000000000000000000000000000000000000000
      00000000000000000100000000000000000000000000000000000000}
  end
end
2
  • 1
    Are you using proper compatibility mode (by default TWebBrowser control runs in IE7 mode) ? Btw. you should add error handling into your script.
    – TLama
    Sep 2, 2015 at 7:47
  • I set the FEATURE_BROWSER_EMULATION to I.E. 11 Sep 2, 2015 at 15:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.