Şimdi bir çay molası vermiştim aşağıdaki kod çıktı.
Kirli metoddur, temizlenmesi lazım...
İlgilisine...
Teyit için haritayı da bir Webbrowser ile gösterebilirsiniz. WebBrowser belirtirseniz, Registry altında projeniz IE uyumu için FEATURE_BROWSER_EMULATION da işleyen kısmı da ekli...
WebBrowser belirtmek
istemezseniz parametre varsayılan olarak
NIL olduğundan sıkıntı olmaz result olarak sadece mesafe döner.
Bu taslak projedir, özellikle parse kısmını geniş tuttum, başka bilgileri de çekmek üzere (
güzergah, alternatifler vb. parse edilen veride yer alıyor)
uses System.Net.HttpClientComponent, Registry;
function GetDistance( Addr1, Addr2 : String; WebBrowser: TWebBrowser = nil ): string;
procedure IE_Emul;
var
r : Registry.TRegistry;
begin
r := TRegistry.Create;
try
r.RootKey := HKEY_CURRENT_USER;
r.OpenKey('\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION', True);
r.WriteInteger(ExtractFileName ( ParamStr(0) ) , 11001);
r.CloseKey;
finally
r.Free;
end;
end;
const
AddrFormat = 'https://www.google.com/maps/dir/%s/%s';
var
aUrl : String;
aClient : TNetHTTPClient;
sResponse : String;
aNodeBegin,
aNodeEnd : Char;
aSrc, aBlock, aResult, aTest : String;
i, flag : Integer;
stoploop : boolean;
begin
Result := '';
Addr1 := StringReplace( Addr1, ' ', '+', [rfReplaceAll] );
Addr2 := StringReplace( Addr2, ' ', '+', [rfReplaceAll] );
aUrl := Format( AddrFormat, [Addr1, Addr2] );
aClient := TNetHTTPClient.Create(nil);
try
aClient.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'; // 'Dalvik/2.1.0 (Linux; U; Android 8.0.0; SM-N950F Build/R16NW)';
aClient.Accept := 'text/html';
sResponse := aClient.Get( aUrl ).ContentAsString;
// Parse Section
// ------------------------------------------------------------------------ //
aSrc := '[[[[[[';
if pos(aSrc, sResponse ) > 0 then
begin
System.Delete(sResponse, 1, Pos(aSrc, sResponse) -1);
aNodeBegin := '[';
aNodeEnd := ']';
aBlock := sResponse;
System.Delete( aBlock, 1, pos( aSrc, aBlock ) -1 );
aTest := aBlock;
system.Delete(aTest, 1, Pos('"', aTest) );
system.Delete(aTest, 1, Pos('"', aTest) );
aTest := trim(aTest);
i := Pos( aNodeBegin, aBlock );
flag := 0;
stoploop := false;
while ( i < Length(aBlock) )
AND ( not stoploop ) do
begin
if aBlock[ i ] = aNodeBegin then inc ( flag )
else
if aBlock[ i ] = aNodeEnd then dec ( flag );
if flag = 0
then stoploop := true
else inc(i);
end;
aResult := Copy( aBlock, 1, i );
aResult := StringReplace( aResult, '\"', '"', [rfReplaceAll] );
aResult := StringReplace( aResult, '\n', #13#10 , [rfReplaceAll] );
i := pos( 'km"', aResult );
if i > 0 then begin
while aResult[ i ] <> '"' do dec ( i );
System.Delete( aResult, 1, i );
Result := Copy( aResult, 1, Pos('"', aResult) -1 );
if Assigned(WebBrowser) then begin
IE_Emul();
WebBrowser.Silent := True;
WebBrowser.Navigate( aURL );
end;
end;
end;
finally
FreeAndNil(aClient);
end;
end;
Kullanımı için şöyle deneyin... (
Not: Adresler haritadan rastgele seçilmiştir...)
procedure TForm1.Button1Click(Sender: TObject);
const
SampleStart = 'ankara, çankaya, yukarı dikmen, 629, 15';
SampleFinish = 'ankara, çankaya, açın, 12';
var
aResult : String;
begin
aResult := GetDistance( SampleStart, SampleFinish, WebBrowser1 );
Showmessage( 'Hesaplanmış Mesafe : ' + aResult );
end;
"Başlama" veya "Bitiş" adresi için alışveriş merkezi, anıtsal yapı, iskele isimlerini direkt verebildiğiniz gibi, adres verecekseniz formatınız şöyle genelden özele veya özelden genele doğru olsun... örnek:
il, ilçe, mahalle, sokak, bina numarası
Adres yazarken yanına
sokak,
mahallesi,
no gibi belirtici kullanmayın.