2017-03-09 63 views
-1

我想在選擇位置時獲取完整地址。這是我的場景的一個例子。如何從Google PlaceAutoComplete片段獲取完整地址?

enter image description here

當我選擇鬱多,我只得到鬱多,如果我申請place.getName()。如果我申請place.getAddress(),我只能得到孟加拉國的Uttara。我如何才能獲得孟加拉達卡分部的達卡Uttara?請幫傢伙.....

這裏是我的代碼片段:

PlaceAutocompleteFragment places= (PlaceAutocompleteFragment) 
       getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment_pick); 
     LinearLayout ll = (LinearLayout) places.getView().findViewById(R.id.place_autocomplete_fragment_pick); 
     ll.setPadding(-7,-7,-7,-7); 
     etPickPlace = (EditText)places.getView().findViewById(R.id.place_autocomplete_search_input); 
     etPickPlace.setTextColor(getResources().getColor(R.color.colorPrimaryDark)); 
     etPickPlace.setTextSize(18.0f); 
     AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
       .setCountry("BD") 
       .build(); 

     places.setFilter(typeFilter); 
     places.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
      @Override 
      public void onPlaceSelected(Place place) { 
       //Here I want to get the full pick up address 
       pickAddress = place.getAddress().toString(); 
       // Toast.makeText(getApplicationContext(),place.getName(),Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onError(Status status) { 
       Toast.makeText(getApplicationContext(),status.toString(), Toast.LENGTH_SHORT).show(); 

      } 

     }); 

回答

3

您可以place.getAddess()只有這一點。如果你想要完整的地址並且更加正確,你需要實現GeoCode API。

下面的類是反向地理編碼獲取傳遞的緯度和經度座標的地址的關鍵元素。我們訪問這裏的輸入代碼是爲反向地理編碼地理編碼谷歌API和得到這樣的街道,城市,針/郵編地址的每一行等

LocationAddress.java

public class LocationAddress { 

private static final String TAG = "LocationAddress"; 
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"); 
        } 
        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); 
        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); 
        message.setData(bundle); 
       } 
       message.sendToTarget(); 
      } 
     } 
    }; 
    thread.start(); 
} 
} 

希望這將有助於實現你的要求。

供您參考:Click Here

編碼愉快!

+0

謝謝你的回答。但我想從地址自動填充片段中找到地址。這裏是 - https://developers.google.com/places/android-api/autocomplete – tahsinRupam

+2

請根據您的預期結果在同一頁面上查看「以編程方式獲取地點預測」。希望你會找到解決方案。 –

+0

我以前見過這些程序,但現在我看到了這個全新的視野!謝謝你,我一定會嘗試不同的組合,直到我得到期望的結果。 – tahsinRupam