public void buttonPlacesClick(View v) {
try {
URL url = new URL(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyD_VBmdcP6RoEfUj-gQ2wBHpm7OxH5Qgng&location=22.997558,120.206832&radius=500&sensor=false&types=food&language=zh-TW");
InputStream is = url.openStream();
StringBuilder sb = new StringBuilder();
Scanner sc = new Scanner(is);
while (sc.hasNextLine()) {
sb.append(sc.nextLine());
}
sc.close();
is.close();
// System.out.println(sb.toString());
JSONObject root = new JSONObject(sb.toString());
JSONArray resultsArray = root.getJSONArray("results");
StringBuilder out = new StringBuilder();
for (int i = 0; i < resultsArray.length(); i++) {
JSONObject biz1 = resultsArray.getJSONObject(i);
System.out.println(biz1.getString("name"));
System.out.println(biz1.getString("vicinity"));
out.append(biz1.getString("name")+"\r\n");
// 取得經緯度
JSONObject geometryObject = biz1.getJSONObject("geometry");
JSONObject locationObject = geometryObject
.getJSONObject("location");
System.out.println("lat:" + locationObject.getDouble("lat")
+ " , lng:" + locationObject.getDouble("lng"));
}
TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(out.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
沒有留言:
張貼留言