百度地图查询 查找经纬度

 
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 + '&region=' + 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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值