【问题标题】:Not able to print address from Reverse Geocoding in Android无法从 Android 中的反向地理编码打印地址
【发布时间】:2012-08-16 21:45:47
【问题描述】:

我从堆栈溢出中解决了许多问题并实施了上述过程。但我无法获得地址。如果我错过了什么,请告诉我..? myLoc = (TextView) findViewById(R.id.id1);

Geocoder geocoder = new Geocoder(getBaseContext(),Locale.getDefault());
try {
    address = geocoder.getFromLocation(latitude, longitude, 1);
                if (address.size() > 0) {
        for (int i = 0; i < address.get(0)
                .getMaxAddressLineIndex(); i++) {
            display = "";
            display += address.get(0).getAddressLine(i)
                    + "\n";
        }
        }

} catch (Exception e2) {
    // TODO: handle exception
}
myLoc.setText("Current Location:"+display);
System.out.println(display);

【问题讨论】:

    标签: android google-maps android-emulator reverse-geocoding


    【解决方案1】:

    您可以使用反向地理编码和 Google api 从纬度和经度获取地址。

    反向地理编码

     double currentLatitude;
        double currentLongitude;
    
    void getAddress(){
            try{
                Geocoder gcd = new Geocoder(this, Locale.getDefault());
                List<Address> addresses = 
                    gcd.getFromLocation(currentLatitude, currentLongitude,100);
                if (addresses.size() > 0) {
                    StringBuilder result = new StringBuilder();
                    for(int i = 0; i < addresses.size(); i++){
                        Address address =  addresses.get(i);
                        int maxIndex = address.getMaxAddressLineIndex();
                        for (int x = 0; x <= maxIndex; x++ ){
                            result.append(address.getAddressLine(x));
                            result.append(",");
                        }               
                        result.append(address.getLocality());
                        result.append(",");
                        result.append(address.getPostalCode());
                        result.append("\n\n");
                    }
                    addressText.setText(result.toString());
                }
            }
            catch(IOException ex){
                addressText.setText(ex.getMessage().toString());
            }
        }
    

    Google API:查看这个从经纬度返回地址的api

    http://maps.googleapis.com/maps/api/geocode/json?latlng=17.734884,83.299507&sensor=true

    To know more read this

    【讨论】:

    • 我正在获取纬度和经度值。但是在添加你的代码之后,我仍然没有得到反向地理编码:(
    【解决方案2】:
    1. getMaxAddressLineIndex() 返回一个从零开始的 index,因此您的 for 循环条件应该是 0 而不是 0
    2. 您通过分配 display = ""; 在每次迭代中覆盖以前的地址行,因此将仅以最后一个地址行结束。这是故意的吗?

    【讨论】:

    • 不敢相信没有人为答案 1 投票。
    【解决方案3】:

    另一个好主意是实现 LocationListener 接口并使用 LocationManager requestLocationUpdates() 方法将您的 Activity 注册为侦听器。然后,您可以覆盖onLocationUpdate(),以便在设备位置更改时收到通知。您向requestLocationUpdates() 方法提供在您接受另一个更新之前必须经过的最短时间,以及在您获得更新之前设备必须移动多远。

    【讨论】:

    • 这将为他提供位置坐标(他似乎已经拥有)而不是地址。
    【解决方案4】:

    您可以这样做以获得完整的地址。如果您想要国家/地区名称 , state 等。那么,我不会喜欢你这种方法。

    public class ParentHomeActivity extends AppCompatActivity {
    
         ...
    
    private Geocoder geocoder;
    private TextView mAddressTxtVu;
    
         ...
    
    
    // I assume that you got latitude and longitude correctly 
    
    mLatitude  =  20.23232
    mLongitude =  32.999
    
    String errorMessage = "";
    
    geocoder = new Geocoder(context, Locale.getDefault());
    
    List<Address> addresses = null;
    
    try {
              addresses = geocoder.getFromLocation(
                       mlattitude,
                       mlongitude,
                       1);
      } catch (IOException e) {
              errorMessage = getString(R.string.service_not_available);
              Log.e(TAG, errorMessage, e);
      } catch (IllegalArgumentException illegalArgumentException) {
                        // Catch invalid latitude or longitude values.
              errorMessage = getString(R.string.invalid_lat_long_used);
              Log.e(TAG, errorMessage + ". " + "Latitude = " + mlattitude +", 
             Longitude = " + mlongitude, illegalArgumentException);
      }
    
      // Handle case where no address was found.
      if (addresses == null || addresses.size() == 0) {
             if (errorMessage.isEmpty()) {
                      errorMessage = getString(R.string.no_address_found);
                      Log.e(TAG, errorMessage);
             }
    
      } else {
             Address address = addresses.get(0);
             ArrayList<String> addressFragments = new ArrayList<String>();
    
             // Fetch the address lines using getAddressLine,
             // join them, and send them to the thread.
             for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
                      addressFragments.add(address.getAddressLine(i));
             }
             // Log.i(TAG, getString(R.string.address_found));
    
    
       mAddressTxtVu.setText(TextUtils.join(System.getProperty("line.separator"),
                                addressFragments));
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多