{"id":1327,"date":"2012-10-29T23:41:36","date_gmt":"2012-10-29T15:41:36","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=1327"},"modified":"2012-10-30T00:24:18","modified_gmt":"2012-10-29T16:24:18","slug":"javagoogle-geocoding-api","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=1327","title":{"rendered":"[Java]Google Geocoding API"},"content":{"rendered":"<p>\u5728\u5beb\u9019\u4e00\u7bc7\u4e4b\u524d\u61c9\u8a72\u6703\u6709\u4e09\u7bc7\u95dc\u65bcFacebook\u5728Android\u8207iOS\u4e0a\u7684\u61c9\u7528<\/p>\n<p>\u4f46\u6211\u53ea\u5beb\u5230\u4e00\u534a\uff0c\u56e0\u70baFacebook\u7684SDK\u4f7f\u7528\u4e0a\u6709\u5e7e\u500b\u5f88\u91cd\u8981\u7684\u7d93\u9a57<\/p>\n<p>\u9700\u8981\u82b1\u4e00\u9ede\u6642\u9593\u622a\u5716\u8aaa\u660e\uff0c\u4f46\u6211\u6700\u8fd1\u4e00\u76f4\u90fd\u6c92\u6709\u6642\u9593\u53bb\u5b8c\u6210\u4ed6<\/p>\n<p>\u6240\u4ee5\u770b\u5b98\u5011\u5c0dFacebook\u5728Android\u8207iOS\u61c9\u7528\u6709\u8208\u8da3\u7684\u9700\u8981\u518d\u7b49\u6703\uff0c\u6211\u5148\u5beb\u9019\u4e00\u7bc7\u6bd4\u8f03\u7c21\u55ae\u7684\u3002<\/p>\n<p>\u672c\u6587\u958b\u59cb\u524d\uff0c\u5148\u4e86\u89e3\u4e00\u4e0b\u4ec0\u9ebc\u662f<a href=\"https:\/\/developers.google.com\/maps\/documentation\/geocoding\/?hl=zh-tw\" target=\"_blank\">Geocoding<\/a>\uff0c\u5176\u5be6\u8aaa\u7a7f\u7684\u5c31\u662fgoogle\u63d0\u4f9b\u628a\u4f4f\u5740\u8f49\u63db\u6210\u7d93\u7def\u5ea6\u7684API<\/p>\n<p>\u81f3\u65bc\u80fd\u505a\u4ec0\u9ebc\u61c9\u7528\u5c31\u898b\u4ec1\u898b\u667a\u3002<\/p>\n<p><!--more-->\u5176\u4e2d\u8981\u6ce8\u610f\u7684\u6211\u6b78\u7d0d\u6709\u4ee5\u4e0b\u5e7e\u9ede<\/p>\n<p>1. 2500\u7b46\u7684\u9650\u5236\uff1agoogle\u70ba\u4e86\u907f\u514d\u6709\u4eba\u6feb\u7528 Geocoding API \u548c (\u6216) \u5c07\u5176\u7528\u5728\u5176\u4ed6\u76ee\u7684\uff0c\u9650\u5236\u975eGoogle Maps API Premier \u4f7f\u7528\u8005\uff0c\u6bcf\u5929\u53ea\u80fd\u8981\u6c42 2,500 \u500b\u5730\u7406\u4f4d\u7f6e\uff0c\u7d93\u5be6\u6e2c\u7d50\u679c\u7684\u78ba\u57282600\u591a\u7b46\u4e4b\u5f8c\u56de\u50b3\u7684\u7d93\u7def\u5ea6\u5c31\u90fd\u662f(0, 0)\u4e86\uff0c\u4f46\u9019\u53ea\u662f\u9632\u541b\u5b50\u4e0d\u9632\u5c0f\u4eba\u7684\u9ce5\u7b56\u7565\u3002<\/p>\n<p>2.\u8acb\u6c42\u901f\u5ea6\u4e0d\u80fd\u592a\u5feb\uff1a\u5982\u679c\u4f60\u7a0b\u5f0f\u4e00\u57f7\u884c\u5c31\u8b93\u4ed6\u4e00\u76f4\u5411Google\u8981\u6c42\u4f4f\u5740\u8f49\u63db\u6210\u7d93\u7def\u5ea6\uff0c\u4f60\u6703\u767c\u73fe\u6bcf\u9694\u5e7e\u7b46\u8acb\u6c42\u6703\u56de\u50b3\u7d93\u7def\u5ea6\u662f(0, 0)<\/p>\n<p>3.\u4f7f\u7528http GET\u7684\u65b9\u5f0f\u8acb\u6c42\uff1a\u53f0\u7063\u662f\u4e2d\u6587\u8a9e\u7cfb\u7684\u570b\u5bb6\uff0c\u6240\u4ee5\u8acb\u6c42\u524d\u6700\u597d\u6307\u5b9a\u4e00\u4e0b\u70baUTF-8\u7de8\u78bc\u683c\u5f0f<\/p>\n<p>4.\u56de\u50b3\u7684\u7d50\u679c\u53ef\u4ee5\u662fXML\u6216JSON\u7684\u5448\u73fe\u65b9\u5f0f\uff1a\u6211\u53ea\u6e2c\u8a66JSON\u7684\u90e8\u5206\uff0c\u56e0\u6b64\u4f7f\u7528\u7684\u7b2c\u4e09\u65b9\u7684\u5957\u4ef6\u4f86\u89e3\u6790<a href=\"http:\/\/code.google.com\/p\/json-simple\/\" target=\"_blank\">json-simple<\/a><\/p>\n<p>\u4ee5\u4e0b\u662f\u539f\u59cb\u78bc\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.BufferedReader;\r\nimport java.io.BufferedWriter;\r\nimport java.io.DataOutputStream;\r\nimport java.io.FileReader;\r\nimport java.io.FileWriter;\r\nimport java.io.InputStreamReader;\r\nimport java.net.HttpURLConnection;\r\n\r\nimport java.net.InetAddress;\r\nimport java.net.URL;\r\nimport java.net.URLEncoder;\r\n\r\nimport org.json.JSONArray;\r\nimport org.json.JSONObject;\r\n\r\npublic class GeoCoding {\r\n\r\n \/**\r\n * @param args\r\n *\/\r\n public static void main(String&#x5B;] args) {\r\n \/\/ TODO Auto-generated method stub\r\n\r\n try {\r\n \/\/\u8a2d\u5b9a\u8981\u8b80\u53d6\u8f49\u63db\u5730\u5740\u7684\u6587\u5b57\u6a94\u8207\u5beb\u5165\u8f49\u63db\u5f8c\u7d93\u7def\u5ea6\u7684\u6587\u5b57\u6a94\r\n BufferedReader addressIn = new BufferedReader(new FileReader(&quot;address_in.txt&quot;));\r\n BufferedWriter addressOut = new BufferedWriter(new FileWriter(&quot;address_out.txt&quot;));\r\n String strIn;\r\n String strOut;\r\n double AddrXY&#x5B;] = { 0, 0 };\r\n int queryCount = 0;\r\n\r\n while ((strIn = addressIn.readLine()) != null) {\r\n Thread.sleep(500);\u00a0\u00a0 \u00a0\/\/\u9019\u88e1\u5c31\u662f\u6211\u8aaa\u7684\u5982\u679c\u4e0d\u4f11\u606f\u500b\u5e7e\u6beb\u79d2\uff0c\u4e00\u76f4\u50b3\u9001\u8acb\u6c42\u6703\u6709\u4e9b\u7121\u6cd5\u53d6\u5230\u7d93\u7def\u5ea6\r\n queryCount++;\r\n AddrXY = getAdressXY(strIn);\u00a0\u00a0 \u00a0\/\/\u53d6\u5f97\u7d93\u7def\u5ea6\r\n if (queryCount == 2500){\u00a0\u00a0 \u00a0\/\/\u7528\u4f86\u9632\u541b\u5b50\u4e0d\u9632\u5c0f\u4eba\u7684\u65b9\u5f0f\uff0c2500\u7b46\u7684\u6642\u5019\u91cd\u65b0\u9023\u7dda\u53d6\u5f97\u65b0IP\r\n Runtime.getRuntime().exec(&quot;cmd \/k start PPPoE.bat&quot;); \/\/\u57f7\u884c\u6211\u5beb\u7684\u91cd\u65b0\u9023\u7ddabat\u6a94\r\n Thread.sleep(7000); \/\/\u6015\u9084\u6c92\u64a5\u63a5\u5b8c\uff0c\u6240\u4ee5\u505c7\u79d2\u518d\u7e7c\u7e8c(\u5176\u5be6\u4e0d\u9700\u8981\u9019\u9ebc\u4e45)\r\n InetAddress myIP = InetAddress.getLocalHost() ;\r\n System.out.println(myIP.getHostAddress());\r\n queryCount = 0;\r\n }\r\n\r\n strOut = strIn + &quot;;&quot; + AddrXY&#x5B;1] + &quot;;&quot; + AddrXY&#x5B;0];\r\n System.out.println(strOut);\r\n addressOut.write(strOut);\r\n addressOut.newLine();\r\n }\r\n addressIn.close();\r\n addressOut.close();\r\n\r\n } catch (Exception e) {\r\n \/\/ TODO Auto-generated catch block\r\n \/\/e.printStackTrace();\r\n System.out.println(&quot;Main Exception&quot; + e.toString());\r\n }\r\n }\r\n\r\n public static double&#x5B;] getAdressXY(String Address) {\r\n URL url;\r\n double retXY&#x5B;] = { 0, 0 };\r\n\r\n try {\r\n Address = URLEncoder.encode(Address, &quot;utf-8&quot;);\r\n String actionUrl = &quot;http:\/\/maps.googleapis.com\/maps\/api\/geocode\/json?sensor=false&amp;address=&quot;\r\n + Address;\r\n\r\n url = new URL(actionUrl);\r\n HttpURLConnection con = (HttpURLConnection) url.openConnection();\r\n\r\n con.setDoOutput(true);\r\n con.setDoInput(true);\r\n con.setRequestMethod(&quot;GET&quot;);\r\n con.setUseCaches(false);\r\n con.setReadTimeout(3000);\r\n con.setConnectTimeout(3000);\r\n\r\n con.setRequestProperty(&quot;Connection&quot;, &quot;Keep-Alive&quot;);\r\n con.setRequestProperty(&quot;Charset&quot;, &quot;utf-8&quot;);\r\n con.setRequestProperty(&quot;Content-Type&quot;,\r\n &quot;application\/x-www-form-urlencoded&quot;);\r\n\r\n DataOutputStream ds = new DataOutputStream(con.getOutputStream());\r\n\r\n \/* \u53d6\u5f97Response\u5167\u5bb9 *\/\r\n int retCode = con.getResponseCode();\r\n \/\/ System.out.println(&quot;JsonUtil retCode&quot; + retCode);\r\n String inputLine = null;\r\n if (retCode == 200) {\r\n BufferedReader reader = new BufferedReader(new InputStreamReader(\r\n con.getInputStream(), &quot;utf-8&quot;));\r\n\r\n String strResult = &quot;&quot;;\r\n while (((inputLine = reader.readLine()) != null)) {\r\n strResult = strResult + inputLine;\r\n }\r\n \/\/System.out.println(&quot;strResult=&quot;+strResult);\r\n reader.close();\r\n\r\n \/\/\u89e3\u6790JSON\u56de\u50b3\u7d50\u679c\r\n JSONObject obj = new JSONObject(strResult);\r\n\r\n JSONArray jsonArray = obj.getJSONArray(&quot;results&quot;);\r\n for (int i = 0; i &lt; jsonArray.length(); i++) {\r\n JSONObject obj2 = jsonArray.getJSONObject(i);\r\n JSONObject obj3 = obj2.getJSONObject(&quot;geometry&quot;);\r\n JSONObject obj4 = obj3.getJSONObject(&quot;location&quot;);\r\n \/\/retString&#x5B;0] = obj4.getString(&quot;lat&quot;);\r\n \/\/retString&#x5B;1] = obj4.getString(&quot;lng&quot;);\r\n retXY&#x5B;0] = obj4.getDouble(&quot;lat&quot;);\r\n retXY&#x5B;1] = obj4.getDouble(&quot;lng&quot;);\r\n\r\n \/\/System.out.println(&quot;lat(\u7d93\u5ea6)\uff1a&quot;+retXY&#x5B;0]);\r\n \/\/System.out.println(&quot;lng(\u7def\u5ea6)\uff1a&quot;+retXY&#x5B;1]);\r\n }\r\n }\r\n con.disconnect();\r\n } catch (Exception e) {\r\n \/\/ TODO Auto-generated catch block\r\n \/\/e.printStackTrace();\r\n System.out.println(&quot;getAdressXY Exception&quot; + e.toString());\r\n }\r\n\r\n return retXY;\r\n }\r\n\r\n}\r\n<\/pre>\n<p><strong>\u8b80\u5165\u7684address_in.txt\u6587\u5b57\u6a94\u5167\u5bb9\u5982\u4e0b<\/strong><br \/>\n\u53f0\u6771\u7e23\u53f0\u6771\u5e02\u6c38\u5b89\u8857\uff19\uff17\u865f<br \/>\n\u53f0\u6771\u7e23\u53f0\u6771\u5e02\u6f22\u4e2d\u8857\uff11\uff12\uff18\u865f<br \/>\n\u53f0\u6771\u7e23\u9e7f\u91ce\u9109\u9ad8\u53f0\u8def\uff14\uff12\u5df7\uff12\u865f<br \/>\n\u53f0\u6771\u7e23\u53f0\u6771\u5e02\u65b0\u751f\u8def\uff17\uff17\uff12\u865f<br \/>\n\u53f0\u6771\u7e23\u53f0\u6771\u5e02\u50b3\u5ee3\u8def\uff12\uff15\uff17\u865f<\/p>\n<p><strong>\u5beb\u5165\u7684address_out.txt\u6587\u5b57\u9ee8\u5167\u5bb9\u5982\u4e0b<\/strong><br \/>\n\u53f0\u6771\u7e23\u53f0\u6771\u5e02\u6c38\u5b89\u8857\uff19\uff17\u865f;121.1220152;22.7829546<br \/>\n\u53f0\u6771\u7e23\u53f0\u6771\u5e02\u6f22\u4e2d\u8857\uff11\uff12\uff18\u865f;121.1374301;22.7582271<br \/>\n\u53f0\u6771\u7e23\u9e7f\u91ce\u9109\u9ad8\u53f0\u8def\uff14\uff12\u5df7\uff12\u865f;121.1197793;22.9141254<br \/>\n\u53f0\u6771\u7e23\u53f0\u6771\u5e02\u65b0\u751f\u8def\uff17\uff17\uff12\u865f;121.1341475;22.7654438<br \/>\n\u53f0\u6771\u7e23\u53f0\u6771\u5e02\u50b3\u5ee3\u8def\uff12\uff15\uff17\u865f;121.1439304;22.7629646<\/p>\n<p><strong>\u53e6\u5916\u5176\u4e2d\u6709\u4e00\u6bb5\u7528\u4f86\u9632\u541b\u5b50\u4e0d\u9632\u5c0f\u4eba\u7684\u505a\u6cd5<\/strong><br \/>\n\u5c31\u662f\u5728\u8acb\u6c422500\u7b46\u5f8c\uff0c\u91cd\u65b0\u64a5\u63a5ADSL\u53d6\u5f97\u65b0IP<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nif (queryCount == 2500){\u00a0\u00a0 \u00a0\/\/\u7528\u4f86\u9632\u541b\u5b50\u4e0d\u9632\u5c0f\u4eba\u7684\u65b9\u5f0f\uff0c2500\u7b46\u7684\u6642\u5019\u91cd\u65b0\u9023\u7dda\u53d6\u5f97\u65b0IP\r\n Runtime.getRuntime().exec(&quot;cmd \/k start PPPoE.bat&quot;); \/\/\u57f7\u884c\u6211\u5beb\u7684\u91cd\u65b0\u9023\u7ddabat\u6a94\r\n Thread.sleep(7000); \/\/\u6015\u9084\u6c92\u64a5\u63a5\u5b8c\uff0c\u6240\u4ee5\u505c7\u79d2\u518d\u7e7c\u7e8c(\u5176\u5be6\u4e0d\u9700\u8981\u9019\u9ebc\u4e45)\r\n InetAddress myIP = InetAddress.getLocalHost() ;\r\n System.out.println(myIP.getHostAddress());\r\n queryCount = 0;\r\n }\r\n\r\n<\/pre>\n<p><strong><br \/>\nPPPoE.bat\u7684\u5167\u5bb9\u5982\u4e0b\uff1a<\/strong><br \/>\nrasdial \/disconnect<br \/>\nrasdial \u9023\u7dda\u540d\u7a31 12345678@hinet.net password<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5beb\u9019\u4e00\u7bc7\u4e4b\u524d\u61c9\u8a72\u6703\u6709\u4e09\u7bc7\u95dc\u65bcFacebook\u5728Android\u8207iOS\u4e0a\u7684\u61c9\u7528 \u4f46\u6211\u53ea\u5beb\u5230\u4e00\u534a\uff0c\u56e0\u70baFaceb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1327","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1327"}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1327"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1327\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}