1

我从堆栈溢出中解决了许多问题并实施了上述过程。但我无法获得地址。如果我错过了什么,请告诉我..?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);
4

4 回答 4

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

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

于 2012-08-21T14:18:14.143 回答
1
  1. getMaxAddressLineIndex()返回一个从零开始的索引,因此您的 for 循环条件应该是0 <= maxIndex而不是0 < maxIndex
  2. 通过分配display = "";在每次迭代中覆盖以前的地址行;因此将仅以最后一个地址行结束。这是故意的吗?
于 2012-08-21T14:40:29.547 回答
0

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

于 2012-08-21T14:25:29.890 回答
0

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

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));
                }
于 2019-06-17T11:25:03.513 回答