Autor Beitrag
nru
Hält's aus hier
Beiträge: 9
Erhaltene Danke: 1



BeitragVerfasst: Do 21.10.10 20:00 
Hallo,

in einem TWebbrowser soll ein wenig JavaScript ausgeführt werden (GoogleMaps). Anhand von div. Beispielen gelingt das auch ansatzweise - jedoch nicht zufriedenstellend. Der Aufruf des JavaScriptes im OnDocumentComplete führt dann zu einem Script-Error ('map' ist Null oder kein Objekt), da offensichtlich der OnLoad-Event des HMTL Body-Tags noch nicht vollständig (oder gar nicht?) erfolgt ist. Alle in initialize() testweise eingefügten Alerts() werden auch alle angezeigt - jedoch alle nach dem Error. Somit ist "map" zum Zeitpunkt, da die ScriptFunktion GotoLatLng ausgeführt werden soll noch nicht bekannt.

Was läuft da falsch?
Danke jetzt schon für Eure Aufmerksamkeit

Gruss
nru


ausblenden volle Höhe JS
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=yes" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> 
<script type="text/javascript"> 

  var map;  
  var bonnlat = 50.76222;
  var bonnlon = 7.08011;

  function initialize() { 
  alert( 'ichbindrin1' );
    var latlng = new google.maps.LatLng( bonnlat, bonnlon);  
    var myOptions = { 
      zoom: 14,                 // 1-ganze Welt ... 18-Mülltonne 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP  //Set the default type map
    }; 
  alert( 'ichbindrin2' );
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
  alert( 'ichbindrin3' );
  }

  function GotoLatLng(Lat, Lang) {  //Set the map in the coordinates and put a marker
    var latlng = new google.maps.LatLng(Lat,Lang);
    map.setCenter(latlng);
    var marker = new google.maps.Marker({
        position: latlng, 
        map: map,
        title:Lat+","+Lang
    });
  }
</script> 
</head> 
<body onload="initialize()"> 
  <div id="map_canvas" style="width:100%; height:100%"></div> 
</body> 
</html>



ausblenden volle Höhe TWebBrowserCompo
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
procedure WaitForBrowser(WB: TWebbrowser);
begin
  while (WB.Busy) and not (Application.Terminated) do begin
    Application.ProcessMessages;
    Sleep(100);
  end;
end;

procedure TFrmWebBrowser.LoadJS( xFile: String );
var
  aStream : TMemoryStream;
begin
  Browser.Navigate('about:blank'); //Set the location to an empty page
  WaitForBrowser( Browser );
  if Assigned(Browser.Document) then begin
    aStream := TMemoryStream.Create;  //create a TStream to load the Page from the string
    try
      if not FileExists( xFile ) then Raise Exception.Create( 'Resourcedatei nicht vorhanden!' );
      aStream.LoadFromFile(xFile);
      aStream.Seek(0, soFromBeginning);
      (Browser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));//Load the page from the stream
    finally
      aStream.Free;
    end;
    HTMLWindow2 := (Browser.Document as IHTMLDocument2).parentWindow; //Set the instance of the parentWindow to call the javascripts functions
  end;
end;

procedure TFrmWebBrowser.GM_ShowPosition( lat, lon: string );
var
  c: String;
begin

  if HTMLWindow2 = nil then exit;

  if (lat<>''and (lon<>''then begin
    c := Format('GotoLatLng(%s,%s)',[lat,lon] );
    HTMLWindow2.execScript(c,'JavaScript'); // Hier kommts zum ScriptError
  end;

end;

ausblenden Main
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
procedure TGPSMain.pbShowPos(Sender: TObject);
begin

  with TFrmWebBrowser.Create( self ) do begin

    Browser.OnDocumentComplete := ShowStartPoint;
    Caption := ZQuery.fieldbyname('title').AsString;
    Show();

    // JS-Scripte in WebBrowser laden
    LoadJS('GMTest.html');

  end;
end;
procedure TGPSMain.ShowStartPoint(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin

// Event wieder deaktivieren
  TWebBrowser(Sender).OnDocumentComplete := nil;

  if TWebBrowser(Sender).ReadyState <> READYSTATE_COMPLETE then exit;

  with TFrmWebBrowser(TWebBrowser(Sender).Owner) do
    GM_ShowPosition( ZQuery.fieldbyName('startlat').asString,
                     ZQuery.fieldbyName('startlon').asString );

end;
nru Threadstarter
Hält's aus hier
Beiträge: 9
Erhaltene Danke: 1



BeitragVerfasst: Fr 22.10.10 08:36 
Hab einen Lösungsansatz gefunden.
Die JS-Funktion initialize() nicht im onLoad des Body-Tags, sondern im Body selber aufrufen.
Das funktioniert, Initialisierung erfolgt. ;)

ausblenden JS
1:
2:
3:
4:
5:
6:
7:
<body> 
  <div id="map_canvas" style="width:100%; height:100%"></div> 
  <script type="text/javascript">
    initialize();
  </script>
</body> 
</html>