1

我正在尝试获取位置名称或地址。我已通过Google Fused Location API成功获取经纬度。如何从给定的经度和纬度查找位置名称?

现在我想通过使用纬度和经度来获取位置地址(例如:城市名称,道路号或具体地址)。

为此,我使用的是Google Geocoder,它工作正常。但是在一些设备中,位置地址返回空值。

我在网上搜索了这个问题的解决方案,发现一些设备制造商在他们的设备中没有包含这个功能。这就是为什么这些设备无法通过反向地理编码方法找到该地址的原因。 Here is the link of that information

那么有没有其他方法可以找到没有Geoconding字符串的地址名?

这里是我的地理编码

public static void getAddress(Context context, double LATITUDE, double LONGITUDE) { 


    try { 
     Geocoder geocoder = new Geocoder(context, Locale.getDefault()); 
     List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); 
     if (addresses != null && addresses.size() > 0) { 



      String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex() 
      String city = addresses.get(0).getLocality(); 
      String state = addresses.get(0).getAdminArea(); 
      String country = addresses.get(0).getCountryName(); 
      String postalCode = addresses.get(0).getPostalCode(); 
      String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL 

      Log.d(TAG, "getAddress: address" + address); 
      Log.d(TAG, "getAddress: city" + city); 
      Log.d(TAG, "getAddress: state" + state); 
      Log.d(TAG, "getAddress: postalCode" + postalCode); 
      Log.d(TAG, "getAddress: knownName" + knownName); 

     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return; 
} 

回答

0

使用此其工作对我来说

public class LocationAddress { 
    private static final String TAG = "LocationAddress"; 

    private static String area = null; 

    public static void getAddressFromLocation(final double latitude, final double longitude, 
               final Context context, final Handler handler) { 
     Thread thread = new Thread() { 
      @Override 
      public void run() { 
       Geocoder geocoder = new Geocoder(context, Locale.getDefault()); 
       String result = null; 
       try { 
        List<Address> addressList = geocoder.getFromLocation(
          latitude, longitude, 1); 
        if (addressList != null && addressList.size() > 0) { 
         Address address = addressList.get(0); 


         StringBuilder sb = new StringBuilder(); 
         for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { 
          sb.append(address.getAddressLine(i)).append("\n"); 
         } 

         area = address.getSubLocality(); 

         sb.append(address.getLocality()).append("\n"); 
         sb.append(address.getPostalCode()).append("\n"); 
         sb.append(address.getCountryName()); 
         result = sb.toString(); 
        } 
       } catch (IOException e) { 
        Log.e(TAG, "Unable connect to Geocoder", e); 
       } finally { 
        Message message = Message.obtain(); 
        message.setTarget(handler); 
        if (result != null) { 
         message.what = 1; 
         Bundle bundle = new Bundle(); 
         /*result = "Latitude: " + latitude + " Longitude: " + longitude + 
           "\n\nAddress:\n" + result;*/ 
         bundle.putString("address", result); 
         bundle.putString("area",area); 
         message.setData(bundle); 
        } else { 
         message.what = 1; 
         Bundle bundle = new Bundle(); 
         /* result = "Latitude: " + latitude + " Longitude: " + longitude + 
           "\n Unable to get address for this lat-long.";*/ 
         bundle.putString("address", result); 
         bundle.putString("area",area); 
         message.setData(bundle); 
        } 
        message.sendToTarget(); 
       } 
      } 
     }; 
     thread.start(); 
    } 
} 

要在Activity使用调用此

LocationAddress locationAddress = new LocationAddress(); 
       locationAddress.getAddressFromLocation(latitude,longitude, 
         getApplicationContext(), new GeocoderHandler()); 

GeocoderHandler获取地址码

private class GeocoderHandler extends Handler { 
     @Override 
     public void handleMessage(Message message) { 
      String locationAddress = null; 
      String area = null; 
      switch (message.what) { 
       case 1: 
        Bundle bundle = message.getData(); 
        locationAddress = bundle.getString("address"); 
        area = bundle.getString("area"); 
        break; 
       default: 
        locationAddress = null; 
      } 

      if(locationAddress != null) 
      locationAddress=locationAddress.replaceAll("[\r\n]+", " "); 

      Log.d("===========>>>",area); 
     Log.d("===========>>>>",locationAddress); 
     } 
    } 
+0

您的GeocoderHandler类在哪里?它给出了错误 – XpressGeek

+0

我已经添加了你可以检查 – Anil

+0

它的工作与否 – Anil

相关问题