简体   繁体   中英

Geocoding with GMLib on non visual component

I'm using Delphi 10.1 Berlin with GMLib 1.5.5 I can successfully display a map and populate Markers based on data from SQL dataset. But it can be rather slow to drop markers when the result sets of my queries become large. I think a lot of this is due to the overhead of GeoCoding addresses into LatLng.
I would like to GeoCode those address at the time of entry and store the Coordinates making it easier/quicker to display the markers. I wrote a function in my DataModule to GeoCode the address any time it changes (Before Post). But the routine 'hangs' as I don't believe the Map ever gets fully initialized (TGMMap.DoMap)

Here is my function:

procedure GeoCodeAddress(var coords: TLatLng; Address: String);
Var  mMap : TGMMap;
     mBrowser : TWebBrowser;
     mGeoCoder : TGMGeoCode;
begin
     mBrowser := TWebBrowser.Create(Nil);
     mBrowser.Silent := True;
     mMap := TGMMap.Create(Nil);
     mMap.APIKey := GMapsAPIKey;
     mMap.AfterPageLoaded := AfterPageLoaded;
     mMap.WebBrowser := mBrowser;
     mMap.Active := True;
     //mMap.DoMap;
     mGeoCoder := TGMGeoCode.Create(Nil);
     mGeoCoder.Map := mMap;
     mGeoCoder.Geocode(Address);
     if mGeoCoder.GeoStatus = gsOK Then
       Begin
        Coords.Lat := mGeoCoder.GeoResult[0].Geometry.Location.Lat;
        Coords.Lng := mGeoCoder.GeoResult[0].Geometry.Location.Lng;
       End;
     mGeoCoder.Free;
     mMap.Free;
     mBrowser.Free;
end;

procedure AfterPageLoaded(Sender: TObject; First: Boolean);
begin
  if First then
      TGMMap(Sender).DoMap;
end;

Thanks in advance for any suggestions.

The problem is that when you call mGeoCoder.Geocode method its possible that the HTML file is not yet loaded.

Try to use mGeoCoder component in AfterPageLoaded event

procedure AfterPageLoaded(Sender: TObject; First: Boolean);
begin
  if First then
  begin
    TGMMap(Sender).DoMap;

    // use here
  end;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM