1:getLocation()方法筛选出最优获取经纬度的方法
2:MapThread线程通过将getLocation()获取的经纬度上传而获取城市名
public class PositionActivity extends BaseActivity implements IInit, IResponseHandler, View.OnClickListener { private TextView mLocationTV, mCategoryTV;//位置.种类 private double latitude, longitude;//经纬度 private String mapUriStr = "http://maps.google.cn/maps/api/geocode/json?latlng={0},{1}&sensor=true&language=zh-CN"; private HttpResponse httpResponse = null; private HttpEntity httpEntity = null; private MapThread mapThread; private Handler handler; private String result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_share_goods_edit); init(); } @Override public void init() { mLocationTV = (TextView) findViewById(R.id.tv_goods_location);, getLocation();//获取经纬度 mapThread = new MapThread(); mapThread.start(); } //获取经纬度 public void getLocation() { LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); List<String> lp = lm.getAllProviders();// 返回所有已知的位置提供者的名称列表,包括未获准访问或调用活动目前已停用的。 for (String item : lp) { // Log.i("---->可用位置服务:", item); } Criteria criteria = new Criteria(); criteria.setCostAllowed(false);//设置位置服务免费 criteria.setAccuracy(Criteria.ACCURACY_COARSE); //设置水平位置精度 String providerName = lm.getBestProvider(criteria, true); //getBestProvider 只有允许访问调用活动的位置供应商将被返回 if (providerName != null) { Location location = lm.getLastKnownLocation(providerName); latitude = location.getLatitude();//获取维度信息 longitude = location.getLongitude();//获取经度信息 } else { ToastUtil.show(this, "1.请检查网络连接 \n2.请打开我的位置"); } } class MapThread extends Thread { @Override public void run() { super.run(); String uriStr = MessageFormat.format(mapUriStr, latitude, longitude); HttpGet httpGet = new HttpGet(uriStr);//生成一个请求对象 HttpClient httpClient = new DefaultHttpClient(); //生成一个Http客户端对象 try { httpResponse = httpClient.execute(httpGet); //使用Http客户端发送请求对象 httpEntity = httpResponse.getEntity(); //获取响应内容 BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent())); result = ""; String line = ""; while ((line = reader.readLine()) != null) { result += line; } Log.v("地址:",result ); } catch (Exception e) { e.printStackTrace(); } } } }
原文地址:http://blog.csdn.net/pengkv/article/details/42246435