无法在Android中从反向地理编码打印地址



我参考了Stack溢出中的许多问题,并实现了上述过程。但是我无法获得地址。如果我错过了什么,请告诉我?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);

您可以使用反向地理编码和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("nn");
                }
                addressText.setText(result.toString());
            }
        }
        catch(IOException ex){
            addressText.setText(ex.getMessage().toString());
        }
    }

谷歌API:查看此API,它从纬度和经度中检索地址

http://maps.googleapis.com/maps/api/geocode/json?latlng=17.734884,83.299507&传感器=真实

要了解更多信息,请阅读此

  1. getMaxAddressLineIndex()返回一个从零开始的索引,因此for循环条件应为0<=maxIndex而不是0<最大索引
  2. 通过分配display=",因此将仅以最后一个地址行结束。这是故意的吗

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

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

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

最新更新