2017-08-17 45 views
2

當我從Google Places自動填充進行搜索時,它會提供來自世界各地的建議/預測。不過,我想根據用戶的位置獲取建議。我試圖在模擬器中手動更改我的位置,但它仍然不起作用。我想我的代碼中可能缺少一些東西,但我不知道是什麼。請幫忙。Google Places API不會根據地點返回建議

代碼:

我的依賴

compile 'com.google.android.gms:play-services-places:10.2.1' 
compile 'com.google.android.gms:play-services-location:10.2.1' 
compile 'com.google.android.gms:play-services:10.2.1' 

FindLocation活動

public class FindLocation extends AppCompatActivity 
     implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 

    private int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1; 
    private GoogleApiClient mGoogleApiClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_find_location); 

     mGoogleApiClient = new GoogleApiClient 
       .Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(Places.GEO_DATA_API) 
       .addApi(Places.PLACE_DETECTION_API) 
       .enableAutoManage(this, this) 
       .build(); 

     try { 
      Intent intent = 
        new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY) 
          .build(this); 
      startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); 
     } catch (GooglePlayServicesRepairableException e) { 
      // TODO: Handle the error. 
     } catch (GooglePlayServicesNotAvailableException e) { 
      // TODO: Handle the error. 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       Place place = PlaceAutocomplete.getPlace(this, data); 
       //Log.i(TAG, "Place: " + place.getName()); 
      } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 
       Status status = PlaceAutocomplete.getStatus(this, data); 
       // TODO: Handle the error. 
       //Log.i(TAG, status.getStatusMessage()); 

      } else if (resultCode == RESULT_CANCELED) { 
       // The user canceled the operation. 
      } 
     } 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 

     //refreshPlacesData(); 

    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

    } 
} 

我的動態的XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.android.project_water.FindLocation"> 

</android.support.constraint.ConstraintLayout> 

回答

0

我已經找到了解決方案,並感謝馬赫什Gawhane和Patrick R代表我的幫助。但爲了充分回答我的問題,我必須首先根據經度和緯度獲取用戶的當前位置。我遵循this教程,以Lat和Lng的方式獲取用戶的位置(取決於您的應用程序是什麼,您可以根據需要自定義代碼)。

然後,我不得不將這個Lat和Lng座標輸入到LatLngBounds對象中,其中,我遵循this教程來做到這一點。一旦我擁有LatLngBounds對象,我將setBoundsBias()方法稱爲Places自動完成,請參閱指南here

現在,自動填充地點會根據用戶位置周圍的某個「有界」區域給出「偏好」建議!通過'偏見'我的意思是相對於不在這個「有界」範圍內的地方。

1

設置LatLongBounds到PlaceAutoComplete用戶當前位置的顯示結果一樣

以下行給出的當前位置的界限

LatLngBounds bounds = mGoogleMap.getProjection().getVisibleRegion().latLngBounds; 
    Log.i("curScreen", ""+bounds); 

,或者你可以設置靜態latlongbounds像

LatLngBounds latLngBounds = new LatLngBounds(
       new LatLng(19.8036125,75.1932621), 
       new LatLng(19.9365798,75.4171406)); 

然後設置過濾器來將自動完成

final AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
      .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS) 
      .setTypeFilter(3) 
      .build(); 

並通過LatLongBoundsAutocompleteFilterPlaceAutocomplete意圖

try 
    { 
     Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setBoundsBias(bounds).setFilter(typeFilter).build(MapActivity.this); 
     startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); 

    } 
+0

試圖在空對象引用 – ben

2

更新代碼:

LatLngBounds bounds = new LatLngBounds(new LatLng(23.8036125,72.1932621), new LatLng(23.9365798,72.4171406)); 
     final AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
       .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS) 
       .build(); 
     try { 

      Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY). 
        setBoundsBias(bounds). 
        setFilter(typeFilter).build(LocationPickerActivityDEMO.this); 
      startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); 

     } catch (GooglePlayServicesRepairableException e) { 
      // TODO: Handle the error. 
     } catch (GooglePlayServicesNotAvailableException e) { 
      // TODO: Handle the error. 
     } 

希望它可以幫助

+0

上調用虛擬方法'com.google.android.gms.maps.Projection com.google.android.gms.maps.GoogleMap.getProjection()'Google Places API指南建議如果我希望片段顯示爲片段,則只能添加片段。但是,我正在使用意圖啓動自動完成作爲一項活動。 – ben

+0

通過從Google控制檯啓用,你的意思是檢索API密鑰?如果是這樣,我已經做到了。 – ben

+0

嗨,我已經更新了答案,請現在檢查。 –

相關問題