Android如何实现坐标转换,如何将GPS坐标转换为地点

尝试这个:

//listenner location changed private class MyLocListener implements LocationListener { public void onLocationChanged(Location location) { if (location != null) { Log.d("LOCATION CHANGED", location.getLatitude() + ""); Log.d("LOCATION CHANGED", location.getLongitude() + ""); } } } //Get address base on location try{ Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault()); Listaddresses = geo.getFromLocation(latitude, longitude, 1); if (addresses.isEmpty()) { yourtextfieldname.setText("Waiting for Location"); } else { if (addresses.size() > 0) { Log.d(TAG,addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName()); } } } catch (Exception e) { e.printStackTrace(); }

将点位置(纬度,经度)转换为可读地址或地名的过程称为反向地理编码 。 [来自维基百科]

您必须使用GeoCoder类并使用方法getFromLocation 。 此方法返回List

,您可以通过迭代列表中的每个Address对象来访问它。

例子:

我认为这会带来更好的结果:

private String convertLocationToAddress(Location location) { String addressText; String errorMessage = ""; Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault()); Listaddresses = null; try { addresses = geocoder.getFromLocation( location.getLatitude(), location.getLongitude(), 1 ); } catch (IOException ioException) { // Network or other I/O issues errorMessage = getString(R.string.network_service_error); Log.e(TAG, errorMessage, ioException); } catch (IllegalArgumentException illegalArgumentException) { // Invalid long / lat errorMessage = getString(R.string.invalid_long_lat); Log.e(TAG, errorMessage + ". " + "Latitude = " + location.getLatitude() + ", Longitude = " + location.getLongitude(), illegalArgumentException); } // No address was found if (addresses == null || addresses.size() == 0) { if (errorMessage.isEmpty()) { errorMessage = getString(R.string.no_address_found); Log.e(TAG, errorMessage); } addressText = errorMessage; } else { Address address = addresses.get(0); ArrayListaddressFragments = new ArrayList<>(); // Fetch the address lines, join them, and return to thread for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) { addressFragments.add(address.getAddressLine(i)); } Log.i(TAG, getString(R.string.address_found)); addressText = TextUtils.join(System.getProperty("line.separator"), addressFragments); } return addressText; }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值