{***************************************************************************}
{ TMS TAdvGoogleLookupProvider                                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2012                                               }
{            Email : info@tmssoftware.com                                   }
{            Web : http://www.tmssoftware.com                               }
{                                                                           }
{ The source code is given as is. The author is not responsible             }
{ for any possible damage done due to the use of this code.                 }
{ The component can be freely used in any application. The complete         }
{ source code remains property of the author and may not be distributed,    }
{ published, given or sold in any form as such. No parts of the source      }
{ code can be included in any other component or application without        }
{ written authorization of the author.                                      }
{***************************************************************************}

unit CloudGoogleLookup;

interface

uses
  Classes, CloudBase;

type
  TAdvGoogleLookupProvider = class(TCloudLookupProvider)
  public
    procedure Lookup(AText: string; Values: TStrings); override;
  end;

  TAdvGoogleLocationLookupProvider = class(TCloudLookupProvider)
  private
    FLanguage: string;
  public
    procedure Lookup(AText: string; Values: TStrings); override;
    constructor Create(AOwner: TComponent); override;
  published
    property Language: string read FLanguage write FLanguage;
  end;

implementation

uses
  XMLDoc, XMLIntf, SysUtils, Windows, DBXJSON;

{ TCloudGoogleLookupProvider }

procedure TAdvGoogleLookupProvider.Lookup(AText: string; Values: TStrings);
var
  resdat: ansistring;
  url,v: string;
  xmldoc: TXMLDocument;
  xmlnode: IXMLNode;
  xmlnodesub: IXMLNode;
  i: integer;
begin
  url := 'http://google.com/complete/search?output=toolbar&q=' + URLEncode(AText);
  resdat := HttpsGet(url);

  if resdat <> '' then
  begin
    xmldoc := TXMLDocument.Create(Self);
    try
      xmldoc.LoadFromXML(resdat);

      for i := 0 to XMLDoc.DocumentElement.ChildNodes.Count - 1 do
      begin
        xmlnode := XMLDoc.DocumentElement.ChildNodes.Get(i);
        xmlnodesub := xmlnode.ChildNodes.Get(0);
        v := xmlnodesub.Attributes['data'];
        Values.Add(v);
      end;
    finally
      xmldoc.Free;
    end;
  end;
end;


{ TAdvGoogleLocationLookupProvider }

constructor TAdvGoogleLocationLookupProvider.Create(AOwner: TComponent);
begin
  inherited;
  FLanguage := 'en';
end;

procedure TAdvGoogleLocationLookupProvider.Lookup(AText: string;
  Values: TStrings);
var
  url: string;
  resdat: ansistring;
  jv, jvs: TJSONValue;
  ja: TJSONArray;
  jo: TJSONObject;
  i: integer;
begin
  url := 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' + URLEncode(AText) +'&types=geocode&language='+ FLanguage + '&sensor=true&key=' + URLEncode(App.Key);

  resdat := CleanupJSON(HttpsGet(url));

  if resdat <> '' then
  begin
    jv := TJSONObject.ParseJSONValue(string(resdat));
    if Assigned(jv) then
    begin
      jvs := GetJSONValue(jv as TJSONObject, 'predictions');

      if Assigned(jvs) and (jvs is TJSONArray) then
      begin
        ja := jvs as TJSONArray;
        for i := 0 to ja.Size - 1 do
        begin
          jo := ja.Get(i) as TJSONObject;
          Values.Add(GetJSONProp(jo,'description'));
        end;
      end;
      jv.Free;
    end;
  end;
end;



end.
