【问题标题】:When find the latitude and longitude from address, how to give the address?从地址中查找经纬度时,如何给出地址?
【发布时间】:2016-01-14 09:35:56
【问题描述】:

我关注这个问题 (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;
}
}

【问题讨论】:

  • 来自 API 文档,“结果是最佳猜测,不保证有意义或正确..”

标签: android google-maps


【解决方案1】:

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

【讨论】:

  • 请注意,此方法使用 Google API 客户端,可能不适用于 Kindle
【解决方案2】:

试试下面的代码

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

                                }

【讨论】:

    【解决方案3】:

    您可以使用以下方法获取包含纬度和经度坐标的 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();
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      相关资源
      最近更新 更多