Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Loading HTML turn-key

Post Reply
SpringerRider
Posts: 8
Joined: Tue Sep 13, 2022 7:26 pm

Loading HTML turn-key

Post by SpringerRider »

I have started using WebView4Delphi and it is saving my bacon. With IE going away, this is my only hope.
I do have one problem and it is probably my own ignorance.

Code: Select all

constructor TfrmStreetMap.Create(AOwner: TComponent; aLat, aLng, aUtc, aShortName: string);
begin
  inherited Create(AOwner);
  Lat  := aLat;
  Lng  := aLng;
  Utc  :=  aUtc;
end;

procedure TfrmStreetMap.FormCreate(Sender: TObject);
begin
  FDownloadIDGen          := 0;
  FDownloadOperation      := nil;
  WVBrowser1.DefaultURL   :='https://www.bing.com';
//  CreateMap(Sender);
end;

procedure TfrmStreetMap.FormActivate(Sender: TObject);
begin
  CreateMap(Sender);
end;

procedure TfrmStreetMap.CreateMap(Sender: TObject);
begin
   WVBrowser1.Navigate('about:blank');
   LoadFromFileAsString('D:\DelphiProjects\D10StreetMapTicket\streetMap\StreetMap.htm');
end;

procedure TfrmStreetMap.LoadFromFileAsString(const aFileName : string);
var
  TempLines : TStringList;
begin
  TempLines := nil;
  try
    try
      if (length(aFileName) > 0) and FileExists(aFileName) then
        begin
          TempLines := TStringList.Create;
          TempLines.LoadFromFile(aFileName);
          WVBrowser1.NavigateToString(TempLines.Text);
        end;
    except
      {$IFDEF DEBUG}
      on e : exception do
        OutputDebugString(PWideChar('TfrmStreetMap.LoadFromFileAsString error: ' + e.message + chr(0)));
      {$ENDIF}
    end;
  finally
    if assigned(TempLines) then
      FreeAndNil(TempLines);
  end;
end;

I am trying to load my StreetMap.htm when the program starts. Calling create on any startup event does not seem to work.

Once the app is running and displaying the Bing, I can use a TEST button to call the CreateMap method and it loads perfectly. But I do not want user interaction. What even can successfully call CreateMap?\\

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  <html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=yes" />
    <script type="text/javascript"  src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxxxxxxxxxxx&amp;callback=initMap"></script>
    <script type="text/javascript">

  var map;
  var trafficLayer;
  var markersArray = [];

  function initialize() {

    var latlng = new google.maps.LatLng(44.714776,-82.019213);
    var myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    trafficLayer = new google.maps.TrafficLayer();
    map.set("streetViewControl", false);
  }

  function GotoLatLng(Lat, Lng, LabelM) {
   var latlng = new google.maps.LatLng(Lat,Lng);
   map.setCenter(latlng);
   PutMarker(Lat, Lng, LabelM);
  }

 function ClearMarkers() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
  }
}

  function PutMarker(Lat, Lng, Msg) {
   var latlng = new google.maps.LatLng(Lat,Lng);
   var marker = new google.maps.Marker({
      position: latlng,
      map: map,
      title: Msg+" ("+Lat+","+Lng+")"
  });
 markersArray.push(marker);
  }

  function ShowAllMarkers() {
  var bounds = new google.maps.LatLngBounds();
    for (var i = 0; i < markersArray.length; i++) {
      bounds.extend(markersArray[i].getPosition());
  }

  map.fitBounds(bounds);
  }


  function TrafficOn()   { trafficLayer.setMap(map); }

  function TrafficOff()  { trafficLayer.setMap(null); }

  function StreetViewOn() { map.set("streetViewControl", true); }

  function StreetViewOff() { map.set("streetViewControl", false); }

</script>
</head>
<body onload="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
Thanks for all you are doing.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Loading HTML turn-key

Post by salvadordf »

Hi,

There are several ways to load a custom HTML file.

The MiniBrowser demo shows two methods to load a file :
  • LoadFromFileAsString
  • LoadFromFileAsFileURI
Use the TWVBrowser.OnAfterCreated event to call LoadFromFileAsString or LoadFromFileAsFileURI. Don't set any value to the WVBrowser1.DefaultURL property and remove the call to WVBrowser1.Navigate('about:blank').

The VirtualHostBrowser demo shows how to load any file from a custom directory on the hard drive. Read the code comments for more information about this demo.
SpringerRider
Posts: 8
Joined: Tue Sep 13, 2022 7:26 pm

Re: Loading HTML turn-key

Post by SpringerRider »

I feel like an idiot. That was so simple.
Contribution going to you guys. You are saving my project.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Loading HTML turn-key

Post by salvadordf »

SpringerRider wrote: Sat Sep 17, 2022 5:36 pm I feel like an idiot. That was so simple.
Contribution going to you guys. You are saving my project.
Thank you very much! :D
Post Reply