Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TWebBrowser

211 views
Skip to first unread message

Mark Moss

unread,
Jul 22, 2008, 11:19:46 PM7/22/08
to
Help

I have the following code which should work, but does not

If I run it straight from Internet Explorer it works fine, but if I
run it thru TWebBrowser I get the following error.

Line 17
Char 44
'GMap2' is undefined
Code 0
URL about:blank


Any help that you guys can give would be greatly appreciated.

Thanks

Test08.html
{------------------------------------------------------------------------------------------------------------}


<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script
src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAACpomf__h59zOpwlPj8I1TxRvlRs7GQV6IzvBJzRL3vQC1yP8vhT5o5kvyXKxB8bl02zs_dkZYyndHg"
type="text/javascript">
</script>
<script type="text/javascript">
// Create a directions object and register a map and DIV to hold the
// resulting computed directions
var map;
var directionsPanel;
var directions;
function initialize() {
if ( GBrowserIsCompatible() ) {
map = new
GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(
38.932400, -104.785600 ), 14 );
directionsPanel =
document.getElementById("route");
directions = new
GDirections(map, directionsPanel);
directions.load("from
38.932400, -104.785600 to 38.930400, -104.786600" );
}
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width: 350px; height: 350px; float:left;
border: 1px solid black;"></div>
<div id="route" style="width: 350px; height: 350px; float:left;
border; 1px solid black;"></div>
<br/>
</body>
</html>';

Project File

Project1.dpr
{------------------------------------------------------------------------------------------------------------}


program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

Delphi Test Program

Unit1.pas
{------------------------------------------------------------------------------------------------------------}


unit KioskMapDirections;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, AdvGlowButton, ExtCtrls, Shader, SR4UProc,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type
TfrmKioskMapDirections = class(TForm)
Shader1: TShader;
pnlMainMenu: TPanel;
agbMainMenu: TAdvGlowButton;
pnlSubMenu: TPanel;
agbSubMenu: TAdvGlowButton;
pnlDetailMenu: TPanel;
agbDetailMenu: TAdvGlowButton;
pnlDescription: TPanel;
agbDescription: TAdvGlowButton;
WebBrowser: TWebBrowser;
IdHTTP1: TIdHTTP;
procedure agbMainMenuClick(Sender: TObject);
procedure agbSubMenuClick(Sender: TObject);
procedure agbDetailMenuClick(Sender: TObject);
procedure agbDescriptionClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmKioskMapDirections: TfrmKioskMapDirections;

implementation

uses
MSHTML, ACTIVEX;

{$R *.dfm}

const
rootDoc: String =
'<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'#13#10 +
'<html xmlns="http://www.w3.org/1999/xhtml">'#13#10 +
' <head>'#13#10 +
' <meta http-equiv="content-type" content="text/html;
charset=utf-8"/>'#13#10 +
' <title>Google Maps JavaScript API Example</title>'#13#10 +
' <script'#13#10 +
'
src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAACpomf__h59zOpwlPj8I1TxRvlRs7GQV6IzvBJzRL3vQC1yP8vhT5o5kvyXKxB8bl02zs_dkZYyndHg"
type="text/javascript">'#13#10 +
' </script>'#13#10 +
' <script type="text/javascript">'#13#10 +
' // Create a directions object and register a map and DIV to hold
the'#13#10 +
' // resulting computed directions'#13#10 +
' var map;'#13#10 +
' var directionsPanel;'#13#10 +
' var directions;'#13#10 +
' function initialize() {'#13#10 +
' if ( GBrowserIsCompatible() ) {'#13#10 +
' map = new
GMap2(document.getElementById("map_canvas"));'#13#10 +
' map.setCenter(new
GLatLng( 38.932400, -104.785600 ), 14 );'#13#10 +
' directionsPanel =
document.getElementById("route");'#13#10 +
' directions = new
GDirections(map, directionsPanel);'#13#10 +
' directions.load("from
38.932400, -104.785600 to 38.930400, -104.786600" );'#13#10 +
' }'#13#10 +
' }'#13#10 +
' </script>'#13#10 +
' </head>'#13#10 +
' <body onload="initialize()">'#13#10 +
' <div id="map_canvas" style="width: 350px; height: 350px;
float:left; border: 1px solid black;"></div>'#13#10 +
' <div id="route" style="width: 350px; height: 350px;
float:left; border; 1px solid black;"></div>'#13#10 +
' <br/>'#13#10 +
' </body>'#13#10 +
'</html>';

{==============================================================================}

procedure TfrmKioskMapDirections.agbMainMenuClick(Sender: TObject);
begin

GlobalReturnCode := 90;

Close;

end;

{==============================================================================}

procedure TfrmKioskMapDirections.agbSubMenuClick(Sender: TObject);
begin

GlobalReturnCode := 80;

Close;

end;

{==============================================================================}

procedure TfrmKioskMapDirections.agbDetailMenuClick(Sender: TObject);
begin

GlobalReturnCode := 70;

Close;

end;

{==============================================================================}

procedure TfrmKioskMapDirections.agbDescriptionClick(Sender: TObject);
begin

GlobalReturnCode := 60;

Close;

end;

{==============================================================================}

procedure TfrmKioskMapDirections.FormShow(Sender: TObject);
var
v : Variant;
HTMLDocument : IHTMLDocument2;

begin

Webbrowser.Navigate('about:blank');

while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
Application.ProcessMessages;

HTMLDocument := WebBrowser.Document as IHTMLDocument2;

if (assigned(HtmlDocument)) then
begin
v := VarArrayCreate([0, 0], varVariant);
v[0] := rootDoc; // Here's your HTML string
HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
HTMLDocument.Close;
end;


end;

{==============================================================================}

end.

Delphi Test Form

Unit1.dfm
{------------------------------------------------------------------------------------------------------------}


object Form1: TForm1
Left = 233
Top = 107
Width = 870
Height = 600
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object WebBrowser: TWebBrowser
Left = 0
Top = 0
Width = 862
Height = 573
Align = alClient
TabOrder = 0
ControlData = {
4C00000017590000393B00000000000000000000000000000000000000000000
000000004C000000000000000000000001000000E0D057007335CF11AE690800
2B2E126208000000000000004C0000000114020000000000C000000000000046
8000000000000000000000000000000000000000000000000000000000000000
00000000000000000100000000000000000000000000000000000000}
end
object IdHTTP1: TIdHTTP
AllowCookies = True
ProxyParams.BasicAuthentication = False
ProxyParams.ProxyPort = 0
Request.ContentLength = -1
Request.Accept = 'text/html, */*'
Request.BasicAuthentication = False
Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
HTTPOptions = [hoForceEncodeParams]
Left = 48
Top = 28
end
end


Marc Rohloff [TeamB]

unread,
Jul 23, 2008, 9:54:54 AM7/23/08
to
On Tue, 22 Jul 2008 21:19:46 -0600, Mark Moss wrote:

> I have the following code which should work, but does not
>
> If I run it straight from Internet Explorer it works fine, but if I
> run it thru TWebBrowser I get the following error.

And if you load the test08.html file into TWebBrowser?

When you load an html file from a local file the Google Maps API does
not check if the key is valid. My guess is that the way you are
loading the file it doesn't appear to loaded from a local file and
hence the check of the key is invalid (The key is usually tied to a
domain).

--
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com

Mark Moss

unread,
Jul 23, 2008, 2:34:45 PM7/23/08
to
Marc


Below iis another test program that loads the same HTML but uses the
"WebBrowser1.Navigate( s + 'HTML_Test_8.html' );" function instead of the
load from String method used in the prior program.

This one works using the same key

Any help that you can give would be greatly appreciated.

Mark Moss

HTML_Test_08.html


<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script

src="http://maps.google.com/maps?file=api&v=2.x&key=ABQIAAAACpomf__h59zOpwlPj8I1TxTIepB8lULTRp_rMs1GPPR1NBziUxTq6bXPIME5bqlIiFEXVANmN34zAQ"

{======================================================================================================================}

program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

{======================================================================================================================}

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, ExtCtrls, OleCtrls, SHDocVw, ComCtrls, WebImage,
WebData;

type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
PageControl1: TPageControl;
WebData1: TWebData;

procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
s : string;
begin

s := ExtractFilePath( ParamStr(0) );

WebBrowser1.Navigate( s + 'HTML_Test_8.html' );

Application.ProcessMessages;

end;

end.

{======================================================================================================================}


object Form1: TForm1
Left = 192


Top = 107
Width = 870

Height = 682


Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False

PixelsPerInch = 96
TextHeight = 13

object Panel1: TPanel
Left = 0
Top = 575
Width = 862
Height = 80
Align = alBottom
BevelInner = bvRaised
BevelWidth = 2
TabOrder = 0
object Button1: TButton
Left = 392
Top = 28
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end
object PageControl1: TPageControl


Left = 0
Top = 0
Width = 862

Height = 575
Align = alClient
TabOrder = 1
end
object WebData1: TWebData
Data = <
item
ScanFirst = '<html'
ScanFrom = '<route>'
ScanTo = '</route>'
Tag = 0
end>
ImageType = itAll
Version = '1.1.0.1'
Left = 740
Top = 604
end
end


Marc Rohloff [TeamB]

unread,
Jul 23, 2008, 2:43:31 PM7/23/08
to
On Wed, 23 Jul 2008 12:34:45 -0600, Mark Moss wrote:

> This one works using the same key

This would work with any key or even no key. The Google maps server
ignores the key if you are using a local file.

> Any help that you can give would be greatly appreciated.

I assume that Google is checking the referrer URL sent with the the
request for the javascript file and comparing it to the one you gave
when you created your key. It might be possible to modify the referrer
using one of the Webbrowser events.
The alternative is to always use a file, even if you have to create it
first.

Jason Penny

unread,
Jul 24, 2008, 3:12:57 PM7/24/08
to
I have found that telling the webbrowser that the page is http://localhost
avoids google's API key problems, though I think somewhere in their
documents it says you need to create a key for localhost, but not file://...
URLs

I don't even specify an API key in the url of my test program,
http://www.jasontpenny.com/Delphi/


after
WebBrowser.Navigate('about:blank') ;

you can set the IHTMLDocument2.URL, and
(WebBrowser.Document as IHTMLDocument2).URL := 'http://localhost/';


the whole function that I use:
procedure WBLoadHTML(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
sl: TStringList;
ms: TMemoryStream;
begin
WebBrowser.Navigate('about:blank') ;
// pretend we're at localhost, so google doesn't complain about the
API key
(WebBrowser.Document as IHTMLDocument2).URL := 'http://localhost/';

while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
Application.ProcessMessages;

if Assigned(WebBrowser.Document) then
begin
sl := TStringList.Create;
try
ms := TMemoryStream.Create;
try
sl.Text := HTMLCode;
sl.SaveToStream(ms);
ms.Seek(0, 0);
(WebBrowser.Document as
IPersistStreamInit).Load(TStreamAdapter.Create(ms));
finally
ms.Free;
end;
finally
sl.Free;
end;
end;
end;

"Marc Rohloff [TeamB]" <ma...@nospam.marcrohloff.com> wrote in message
news:dj0geoc7...@dlg.marcrohloff.com...

0 new messages