2017-08-17 67 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

嗨,我已经更新了答案,请现在检查。 –