首页 > 解决方案 > Android PlacePicker 不返回所选地址

问题描述

我在我的 Android 应用程序中使用 Google PlacePicker。我可以用它来选择地址。但是,一旦选择了地址,它就不会返回调用者活动。相反,我必须按下后退按钮!

这就是我初始化 PlacePicker 的方式:

/**
 * Add New Address
 */
mNewButton.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent)
    {
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
        try
        {
            startActivityForResult(builder.build(AddressPickerActivity.this), ADDRESS_CODE);
        }
        catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e)
        {
            int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(AddressPickerActivity.this);
            GoogleApiAvailability.getInstance().getErrorDialog(AddressPickerActivity.this, status, 100).show();
        }
        return false;
    }
});

这就是我听取结果的方式:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == ADDRESS_CODE)
    {
        if (resultCode == RESULT_OK)
        {
            Log.d(TAG, "Google PlacePicker finished");
            // Do Stuff then finish current activity too
            AddressPickerActivity.this.setResult(RESULT_OK);
            AddressPickerActivity.this.finish();
        }
    }
}

Google PlacePicker finished问题是:除非我按下地点选择器上的后退按钮,否则我不会收到日志消息“ ”!之后一切正常,并且我想要发生的操作正常工作(意味着地址已正确选择)

这里有一个类似的问题,评论表明调用者活动可能android:noHistory="true"在清单中。但我查了一下,不是。这是我清单中的相应片段:

<activity
    android:name=".AddressPickerActivity"
    android:theme="@style/AppThemeNoActionBar" />

这可能是什么原因造成的?

编辑1:

我还尝试明确添加android:noHistory="false"到清单中。没变。

标签: androidgoogle-mapsgoogle-places-api

解决方案


您可以使用OnClickListener或尝试MotionEvent.ACTION_UP为 startActivityForResult 添加 if 条件,因为 PlacePicker 会打开两次。

mNewButton.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(motionEvent.getAction() == MotionEvent.ACTION_UP){
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
            try {
                startActivityForResult(builder.build(AddressPickerActivity.this), ADDRESS_CODE);
            } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
                int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(AddressPickerActivity.this);
                GoogleApiAvailability.getInstance().getErrorDialog(AddressPickerActivity.this, status, 100).show();
            }
        }
        return false;
    }
});

推荐阅读