从地址找到纬度和经度时,如何给出地址?

时间:2016-01-14 09:35:56

标签: android google-maps

我按照这个问题(How can I find the latitude and longitude from address?)尝试获取地址的纬度和经度。我称这样的方法为" GeoPoint point = getLocationFromAddress(" colombo,sri lanka");" 。但给出空值。如何正确调用方法??

public GeoPoint getLocationFromAddress(String strAddress){

Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;

try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
   return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();

p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
                  (int) (location.getLongitude() * 1E6));

return p1;
}
}

3 个答案:

答案 0 :(得分:1)

尝试以下代码

    Geocoder gcd = new Geocoder(ReserveTimer.this, Locale.getDefault());
    List<Address> addresses = gcd.getFromLocation(Double.parseDouble(lati), Double.parseDouble(longi), 1);  

                                if (addresses.size() > 0) {
                                    System.out.println(addresses.get(0).getLocality());
                                    System.out.println(addresses.get(0).getCountryName());
 System.out.println(addresses.get(0).getAddressLine(0));


System.out.println(addresses.get(0).getAdminArea());
System.out.println(addresses.get(0).getCountryName());
System.out.println(addresses.get(0).getPostalCode());

                                }

答案 1 :(得分:1)

在我看来,你应该使用google place autocomplete / place picker api's。 选择地点,然后您可以获得纬度和经度。 看一下这个: https://developers.google.com/places/android-api/autocomplete

答案 2 :(得分:1)

您可以使用以下方法获取包含纬度和纵坐标的Location对象,并将该位置记录到LogCat控制台:

public void setLocation(Location loc) {
  //Get Human readable address from latitud and longitud coordinates 
  if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
    try {
      Geocoder geocoder = new Geocoder(this, Locale.getDefault());
      List<Address> list = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
      if (!list.isEmpty()) {
    Address address = list.get(0);
    Log.d(CLASS_TAG, "Mi direcci—n es: \n" + address.getAddressLine(0));
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}