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

相關問題