【问题标题】:How to convert GPS coordinates to locality如何将 GPS 坐标转换为位置
【发布时间】:2012-08-19 14:45:15
【问题描述】:

我正在编写一个 Android 应用程序,以将动态可用的纬度和对数坐标转换为人类可读的位置。

例如,12.2、4.5 位于英国伦敦市中心。关于粒度,我希望能够至少定位城市->城镇。或者如果没有,至少是城市,

有人可以就此问题提供哪些解决方案提供建议。

【问题讨论】:

  • 你有两个正确答案。将对您有所帮助的答案标记为正确。

标签: android


【解决方案1】:

试试这个:

//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());
 List<Address> addresses = 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(); 
}

【讨论】:

    【解决方案2】:

    将点位置(纬度、经度)转换为可读地址或地名的过程称为Reverse GeoCoding。 [来自维基百科]

    你必须使用GeoCoder类和使用方法getFromLocation。此方法返回List&lt;Address&gt;,您可以通过迭代列表中的每个Address 对象来访问它。

    例子:

    1. http://www.edumobile.org/android/android-development/gecoding-example/
    2. Android: Reverse geocoding - getFromLocation

    【讨论】:

      【解决方案3】:

      我认为这会产生更好的结果:

       private String convertLocationToAddress(Location location) {
          String addressText;
          String errorMessage = "";
      
          Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
      
          List<Address> addresses = 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);
              ArrayList<String> addressFragments = 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;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-06
        • 1970-01-01
        相关资源
        最近更新 更多