java通过经纬度或取城市名_通过经纬度获取城市名/地址(不需要三方包)

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 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();

}

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值