1

我正在尝试在 Delphi 7 中执行一些 javascript。我修复了原来的问题,即缺少分号,但不,我只是没有得到任何结果。似乎没有调用 oncalc,只是一个猜测。

unit fMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  OleCtrls, SHDocVw, MSHTML;

type
  TfrmMain = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    HTMLWindow2: IHTMLWindow2;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

uses
   ActiveX;


{$R *.dfm}

const

HTMLStrMap: AnsiString =
'<!DOCTYPE html> '+
'<html> '+
'  <head> '+
'    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> '+
'    <meta charset="utf-8"> '+
'    <title>Google Maps JavaScript API v3 Example: Directions Simple</title> '+
'    <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet"> '+
'    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> '+
'    <script> '+
'      var directionsService = new google.maps.DirectionsService(); '+
'      var map; '+
''+
'      function initialize() { '+
'        directionsDisplay = new google.maps.DirectionsRenderer(); '+
'        var chicago = new google.maps.LatLng(41.850033, -87.6500523); '+
'        var mapOptions = { '+
'          zoom:7, '+
'          mapTypeId: google.maps.MapTypeId.ROADMAP, '+
'          center: chicago '+
'        }; '+
'        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); '+
'        directionsDisplay.setMap(map); '+
'      } '+
''+
'      function calcRoute() { '+
'        var start = document.getElementById("start").value; '+
'        var end = document.getElementById("end").value; '+
'        var request = { '+
'            origin:start, '+
'            destination:end, '+
'            travelMode: google.maps.DirectionsTravelMode.DRIVING '+
'        }; '+
'        directionsService.route(request, function(response, status) { '+
'          if (status == google.maps.DirectionsStatus.OK) { '+
'            directionsDisplay.setDirections(response); '+
'          } '+
'        }); '+
'      } '+
'    </script> '+
'  </head> '+
'  <body onload="initialize()"> '+
'    <div> '+
'    <b>Start: </b> '+
'    <select id="start" onchange="calcRoute();"> '+
'      <option value="chicago, il">Chicago</option> '+
'      <option value="st louis, mo">St Louis</option> '+
'      <option value="joplin, mo">Joplin, MO</option> '+
'      <option value="oklahoma city, ok">Oklahoma City</option> '+
'      <option value="amarillo, tx">Amarillo</option> '+
'      <option value="gallup, nm">Gallup, NM</option> '+
'      <option value="flagstaff, az">Flagstaff, AZ</option> '+
'      <option value="winona, az">Winona</option> '+
'      <option value="kingman, az">Kingman</option> '+
'      <option value="barstow, ca">Barstow</option> '+
'      <option value="san bernardino, ca">San Bernardino</option> '+
'      <option value="los angeles, ca">Los Angeles</option> '+
'    </select> '+
'    <b>End: </b> '+
'    <select id="end" onchange="calcRoute();"> '+
'      <option value="chicago, il">Chicago</option> '+
'      <option value="st louis, mo">St Louis</option> '+
'      <option value="joplin, mo">Joplin, MO</option> '+
'      <option value="oklahoma city, ok">Oklahoma City</option> '+
'      <option value="amarillo, tx">Amarillo</option> '+
'      <option value="gallup, nm">Gallup, NM</option> '+
'      <option value="flagstaff, az">Flagstaff, AZ</option> '+
'      <option value="winona, az">Winona</option> '+
'      <option value="kingman, az">Kingman</option> '+
'      <option value="barstow, ca">Barstow</option> '+
'      <option value="san bernardino, ca">San Bernardino</option> '+
'      <option value="los angeles, ca">Los Angeles</option> '+
'    </select> '+
'    </div> '+
'    <div id="map-canvas" style="top:30px;"></div> '+
'  </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(HTMLStrMap)^, Length(HTMLStrMap));
         aStream.Seek(0, soFromBeginning);
         (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));
      finally
         aStream.Free;
      end;
      HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
    end;
end;

end.
4

1 回答 1

2

该示例的 CSS 是指向 google 站点上示例的相对链接。如果您要复制他们的示例,请将其更改为绝对链接或将 CSS 文件移动到您的域。

于 2013-03-18T15:29:06.953 回答