﻿{"id":10,"date":"2014-08-16T21:09:56","date_gmt":"2014-08-16T13:09:56","guid":{"rendered":"http:\/\/www.tansoo.cn\/?p=10"},"modified":"2014-09-10T10:32:49","modified_gmt":"2014-09-10T02:32:49","slug":"%e5%88%a9%e7%94%a8%e7%99%be%e5%ba%a6%e5%9c%b0%e5%9b%beapi%e5%be%97%e5%88%b0%e6%8c%87%e5%ae%9a%e5%9d%90%e6%a0%87%e5%9c%b0%e5%8c%ba%e7%9a%84%e5%a4%a9%e6%b0%94%e9%a2%84%e6%8a%a5","status":"publish","type":"post","link":"http:\/\/www.tansoo.cn\/?p=10","title":{"rendered":"\u5229\u7528\u767e\u5ea6\u5730\u56feAPI\u5f97\u5230\u6307\u5b9a\u5750\u6807\u5730\u533a\u7684\u5929\u6c14\u9884\u62a5"},"content":{"rendered":"<p>\u5229\u7528\u767e\u5ea6\u5730\u56fe\u8f66\u8054\u7f51API\uff0c\u6839\u636e\u7ecf\u7eac\u5ea6\/\u57ce\u5e02\u540d\u67e5\u8be2\u5929\u6c14\u7684\u7ed3\u679c\u3002\u6e90\u4ee3\u7801\u4e2d\u8981\u7528\u5230\u7684\u4e00\u4e9b\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u7ed3\u6784\u8bf7\u5728\u672c\u7ad9\u5176\u4ed6\u4ee3\u7801\u4e2d\u67e5\u627e\u3002 <!--more--><\/p>\n<pre class=\"height-mode:2 height:100 height-unit:1 width-mode:2 width:100 width-unit:1 lang:delphi decode:true \" >\/\/ \u5f97\u5230\u5750\u6807\u6240\u5728\u5730\u7684\u5929\u6c14\u9884\u62a5\r\nfunction TfrmMain.GetWeather(coords: TCoordinate): WeatherResults;\r\nvar\r\n  I, J, P, count: Integer;\r\n  Url, PicPath: string;\r\n  node: IXMLNode;\r\n  XMLDoc: TXMLDocument;\r\n  nodeList: IXMLNodeList;\r\n  IdHTTP: TIdHTTP;\r\n  tStream: TFileStream;\r\nbegin\r\n  XMLDoc := TXMLDocument.Create(Self);\r\n  IdHTTP := TIdHTTP.Create;\r\n  try\r\n    coords := GpsToBaiduXY(coords);\r\n    if coords.Valid then\r\n    begin\r\n      Url := UrlEncode(Format(\r\n          'http:\/\/api.map.baidu.com\/telematics\/v3\/weather?location=%f,%f&amp;output=xml&amp;coord_type=wgs84&amp;ak=A4c132a2db250e33cd1e2af37076ac28', [coords.X, coords.Y]));\r\n      try\r\n        XMLDoc.XML.Text := IdHTTP.Get(Url);\r\n        XMLDoc.Active := True;\r\n\r\n        node := XMLDoc.DocumentElement.ChildNodes[3]; \/\/ &lt;results&gt;\r\n\r\n        Result.error := XMLDoc.DocumentElement.ChildValues['error'];\r\n        Result.status := XMLDoc.DocumentElement.ChildValues['status'];\r\n        Result.date := XMLDoc.DocumentElement.ChildValues['date'];\r\n        Result.currentCity := node.ChildValues['currentCity'];\r\n        Result.pm25 := node.ChildValues['pm25'];\r\n\r\n        nodeList := node.ChildNodes[1].ChildNodes; \/\/ &lt;weather_data&gt;\r\n\r\n        I := 0;\r\n        for J := 1 to 4 do\r\n        begin\r\n          Result.Data[J].date := string(nodeList[I].NodeValue);\r\n          Inc(I);\r\n          Result.Data[J].dayPictureUrl := string(nodeList[I].NodeValue);\r\n          Inc(I);\r\n          Result.Data[J].nightPictureUrl := string(nodeList[I].NodeValue);\r\n          Inc(I);\r\n          Result.Data[J].weather := string(nodeList[I].NodeValue);\r\n          Inc(I);\r\n          Result.Data[J].wind := string(nodeList[I].NodeValue);\r\n          Inc(I);\r\n          Result.Data[J].temperature := string(nodeList[I].NodeValue);\r\n          Inc(I);\r\n\r\n          P := Pos('\/images\/', Result.Data[J].dayPictureUrl);\r\n          PicPath := ExePath + copy(Result.Data[J].dayPictureUrl, P + 1,\r\n            Length(Result.Data[J].dayPictureUrl) - P);\r\n          PicPath := ReplaceChar(PicPath, '\/', '\\');\r\n          if not FileExists(PicPath) then\r\n          begin\r\n            tStream := TFileStream.Create(PicPath, fmCreate);\r\n            try\r\n              IdHTTP.Get(Result.Data[J].dayPictureUrl, tStream); \/\/ \u5f00\u59cb\u4e0b\u8f7d\r\n            finally\r\n              tStream.Free;\r\n            end;\r\n          end;\r\n\r\n          P := Pos('\/images\/', Result.Data[J].nightPictureUrl);\r\n          PicPath := ExePath + copy(Result.Data[J].nightPictureUrl, P + 1,\r\n            Length(Result.Data[J].nightPictureUrl) - P);\r\n          PicPath := ReplaceChar(PicPath, '\/', '\\');\r\n          if not FileExists(PicPath) then\r\n          begin\r\n            tStream := TFileStream.Create(PicPath, fmCreate);\r\n            try\r\n              IdHTTP.Get(Result.Data[J].nightPictureUrl, tStream); \/\/ \u5f00\u59cb\u4e0b\u8f7d\r\n            finally\r\n              tStream.Free;\r\n            end;\r\n          end;\r\n        end;\r\n      except\r\n        abort;\r\n      end;\r\n    end;\r\n  finally\r\n    XMLDoc.Free;\r\n    IdHTTP.Free;\r\n  end;\r\nend;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528\u767e\u5ea6\u5730\u56fe\u8f66\u8054\u7f51API\uff0c\u6839\u636e\u7ecf\u7eac\u5ea6\/\u57ce\u5e02\u540d\u67e5\u8be2\u5929\u6c14\u7684\u7ed3\u679c\u3002\u6e90\u4ee3\u7801\u4e2d\u8981\u7528\u5230\u7684\u4e00\u4e9b\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u7ed3\u6784\u8bf7\u5728\u672c\u7ad9\u5176\u4ed6\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[6,5,9],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-program","tag-6","tag-5","tag-api"],"_links":{"self":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":0,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}