DEV Community

Max Kleiner
Max Kleiner

Posted on

Google Directions Script

The St. Thomas Church (German: Thomaskirche) is a Lutheran church in Leipzig, Germany, located at the western part of the inner city ring road in Leipzig’s central district. Martin Luther preached in the church in 1539. It is associated with several well-known composers, especially Johann Sebastian Bach, who was its Thomaskantor (music director) from 1723 until his death in 1750. The church holds his remains.

function TAddressGeoCodeOSM8(AURL, location, aApikey: string): tlatlong;
var Httpreq: THttpRequestC; httpres: string;
    Body: TMultipartFormBody;
    jsn: TMcJsonItem;
begin
  httpreq:= THttpRequestC.create(self);
  httpreq.headers.add('Accept: application/json; charset=utf-8');
  httpreq.useragent:= USERAGENT4;
  httpreq.SecurityOptions:= [soSsl3, soPct, soIgnoreCertCNInvalid];
  try
    if httpreq.get(Format(AURL,[location])) then begin
       //httpres:= (httpreq.Response.ContentAsString)
       httpres:= (httpreq.Response.ContentAsUTF8String)
       //writeln('conttype '+httpreq.Response.ContentType);
       writ('debug back '+formatJson(httpres));
       jsn:= TMcJsonItem.Create;
       jsn.AsJSON:= httpres;
       result.lat:= jsn.at(0,'lat').asnumber;
       result.long:= jsn.at(0,'lon').asnumber;
       result.descript:= Format('Coords: lat %2.5f  lng %2.5f %s place_id: %d',
                                [result.lat,result.long,jsn.at(0,'display_name').asstring,
                                                        jsn.at(0,'place_id').asinteger]); 
    end else Writeln('APIError '+inttostr(Httpreq.Response.StatusCode2));
    //StrReplace(httpres, '[{', '{');
  finally 
    writeln('Status3: '+gethttpcod(httpreq.Response.statuscode2))
    httpreq.Free;  
    sleep(200)
    jsn.Free;
  end; 
end;
Enter fullscreen mode Exit fullscreen mode

OSM _to: Coords: lat 51.33933 lng 12.37260 Thomaskirche, 18, Thomaskirchhof, Zentrum, Mitte, Leipzig, Sachsen, 04109, Deutschland place_id: 118838745
get geocoords: lat: 51.3393 - lon: 12.3726
mX5🐞 executed: 26/09/2024 09:52:14 Runtime: 0:0:4.38 Memload: 62% use

TDirection is working as a URL Builder:

procedure TDirectionsBtnGoGoogleClick(Sender: TObject);
Var
  LocFrom,LocTo:RNavigateLongLat;
  Long,Lat:Double;
begin
  LocFrom:=RNavigateLongLat.create;
  Locto:=RNavigateLongLat.create;
  Long:=RealFrmDegreeText(EdtLong.Text);
  Lat:= RealFrmDegreeText(EdtLat.Text);
  LocFrom.CreateDec(Long,Lat);
  Long:=RealFrmDegreeText(EdtLong2.Text);
  Lat:= RealFrmDegreeText(EdtLat2.Text);
  LocTo.CreateDec(Long,Lat);
  // { 0 Start 1 End 3 Center }
  EdtGoogleLink.Text:=LocFrom.GoogleLinkDirectionsTo(LocTo, 0);
  LBlCrowFlies.Caption:='Distance as Crow Flys ='+FormatFloat('0.0km',LocTo.MetresFrom(LocFrom)/1000);
  if CBxGoNow.Checked then
     LocFrom.GoGoogleDirectionsTo(LocTo, 0);
end;
Enter fullscreen mode Exit fullscreen mode

Image description

Google Maps helps you discover new places, find directions, and get real-time updates on traffic, public transportation, and more. You can also control your data with privacy settings, use the Google Assistant, and see stories of how people use Google Maps.

Image description

http://www.softwareschule.ch/examples/directions6.txt

Top comments (0)