PlaceAutocompleteFragment - 在onPlaceSelected方法中标识调用对象

时间:2017-02-17 08:08:32

标签: java android google-places-api google-maps-android-api-2

我创建了PlaceAutocompleteFragment类的2个对象,并在两者上设置了OnPlaceSelectedListener。

placePickup = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_source);
placePickup.setOnPlaceSelectedListener(this);

placeDrop = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_target);
placeDrop.setOnPlaceSelectedListener(this);

现在,我想找到一种方法来识别哪个对象调用onPlaceSelected()方法。

@Override
public void onPlaceSelected(Place place) {
    latLng = place.getLatLng();

    if (invoking object is placePickup){
        do this;
    }
    if (invoking object is placeDrop){
        do this;
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

你可以维护一个布尔变量 当用户点击pickUp时,将该变量设为true。

boolean isPickUp=false;

onCLlick of pickUp,你加载PlaceAutoComplete片段使其成为真; 而onclick of drop使它变得虚假;

if (isPickUp){
    do pickUp related work;
}
else{
    do drop related work here;
}

答案 1 :(得分:0)

使用如下所示的内联电话。

placePickup = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_source);
placePickup.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        Log.e(TAG,"Pick Up");

    }

    @Override
    public void onError(Status status) {

    }
});

placeDrop = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_target);
placeDrop.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        Log.e(TAG,"Drop");             
    }

    @Override
    public void onError(Status status) {

    }
});