var
Form1: TForm1;
fBaiduKey: string;
fLastError: String;
implementation
uses IdURI, MBJson;
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
GeocodingQuery('老下陆', '黄石', 50, 1);
end;
function TForm1.GeocodingQuery(const ObjectType, aCity: string;
PageSize, PageNo: integer): boolean;
var
url, tmpstr: string;
stream: TStringStream;
i: integer;
nodeList: IXMLNodeList;
node: IXMLNode;
num: integer;
begin
fBaiduKey := '74c27c8c6c623cfbe0bc80aa0940038f';
url := Tiduri.URLEncode('http://api.map.baidu.com/place/v2/search?&q=' +
ObjectType + '®ion=' + aCity + '&page_num=' + inttostr(PageNo) +
'&page_size=' + inttostr(PageSize) + '&output=xml&ak=' + fBaiduKey);
stream := TStringStream.Create('', TEncoding.UTF8);
IdHTTP1.HTTPOptions := [hoKeepOrigProtocol];
IdHTTP1.Request.Connection := 'Keep-Alive';
try
IdHTTP1.Get(url, stream);
Result := (stream.Size > 0);
except
on e: exception do
begin
Result := false;
fLastError := '[' + e.ClassName + ']-' + e.Message;
end;
end;
IdHTTP1.Disconnect;
if Result then
begin
tmpstr := stream.DataString;
tmpstr := StringReplace(tmpstr, 'utf-8', 'gb2312', [rfReplaceAll]);
self.XMLDoc.Active := false;
self.XMLDoc.Xml.Text := tmpstr;
self.XMLDoc.Active := true;
nodeList := XMLDoc.DocumentElement.ChildNodes[3].ChildNodes;
num := nodeList.Count;
for i := 0 to num - 1 do
begin
node := nodeList[i];
StringGrid1.Cells[0, i] := node.ChildNodes['name'].Text;
StringGrid1.Cells[1, i] := node.ChildNodes[1].ChildNodes['lat'].Text;
StringGrid1.Cells[2, i] := node.ChildNodes[1].ChildNodes['lng'].Text;
end;
end;
FreeAndNil(stream);
end;