DELPHI調用百度定位API(根據IP擷取城市及GPS資訊等)

來源:互聯網
上載者:User

標籤:

 

緣由:因智能助理在使用者說出“天氣如何”時,需要自動擷取城市,所以這裡需要根據使用者IP自動擷取城市,所以有了這篇文章 QQ508882988

//根據百度API,根據來訪IP自動擷取出該IP所在的位置及更多資訊,參數IP為空白時,會使用當前訪問者的IP地址作為定位參數
//sServerAK為使用者密鑰string必選,在lbs雲官網註冊的access key,作為訪問的依據,定期從http://lbsyun.baidu.com/apiconsole/key擷取
//參考http://lbsyun.baidu.com/index.php?title=webapi/ip-api#.E6.9C.8D.E5.8A.A1.E5.9C.B0.E5.9D.80
//function GetLocInfByIP_BaiDu(sIP, sServerAK: string): string;

uses SuperObject; 

function GetLocInfByIP_BaiDu(sIP, sServerAK: string): string;
  var sUrl, ak, sIPS, sR: string;
  vJson, vj: ISuperObject;
  response: TStringStream;
  FHttp: TIdHTTP;
begin
  sR := ‘‘;
  ak := ‘9fe55cc6597c6b331754b19a048accd4‘;
  if sIP <> ‘‘ then begin
    sIPS := ‘&ip=‘+ sIP;
  end else begin
    sIPS := ‘‘;
  end;

  sUrl := ‘http://api.map.baidu.com/location/ip?ak=‘+sServerAK + sIPS;
  response := TStringStream.create(‘‘, tencoding.utf8);
  FHttp := TIdHttp.Create(nil);
  sR := FHttp.Post(sUrl, response);
  FHttp.Free;
  vJson := SO((sr));

  //我這裡只用到城市,所以如下取出,各位客官可自行根據情況擷取
  result := SO(SO(vJson.S[‘content‘]).S[‘address_detail‘]).S[‘city‘];
end;

 

返回的結果json串形如下:

  {          address: "CN|北京|北京|None|CHINANET|1|None",   #地址          content:       #詳細內容          { address: "北京市", #簡要地址 address_detail: #詳細地址資訊 { city: "北京市", #城市 city_code: 131, #百度城市代碼 district: "", #區縣 province: "北京市", #省份 street: "", #街道 street_number: "" #門址 }, point: #百度經緯度座標值 { x: "116.39564504", y: "39.92998578" } }, status: 0 #返回狀態代碼 }

DELPHI調用百度定位API(根據IP擷取城市及GPS資訊等)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.