private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case SHOW_LOCATION:
String position = (String) msg.obj;
postionView.setText(position);
break;
default:
break;
}
}
};
/**
* 显示地理位置经度和纬度信息
* @param location
*/
private void showLocation(final Location location){
/*String locationStr = "维度:" + location.getLatitude() +"\n"
+ "经度:" + location.getLongitude();
postionView.setText(locationStr);*/
new Thread(new Runnable() {
@Override
public void run() {
try{
//组装反向地理编码的接口位置
StringBuilder url = new StringBuilder();
url.append("http://maps.google.cn/maps/api/geocode/json?&sensor=true,language=zh-CN&latlng=");
url.append(location.getLatitude()).append(",");
url.append(location.getLongitude());
url.append("&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url.toString());
httpGet.addHeader("Accept-Language","zh-CN");
HttpResponse response = client.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity entity = response.getEntity();
String res = EntityUtils.toString(entity);
//解析
JSONObject jsonObject = new JSONObject(res);
//获取results节点下的位置信息
JSONArray resultArray = jsonObject.getJSONArray("results");
if(resultArray.length() > 0){
JSONObject obj = resultArray.getJSONObject(0);
//取出格式化后的位置数据
String address = obj.getString("formatted_address");
Message msg = new Message();
msg.what = SHOW_LOCATION;
msg.obj = address;
handler.sendMessage(msg);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}
注:有正确的金纬度就可以搞出对应的地理位置。