Android PlacePicker 不返回选择的地址

标签 android google-maps google-places-api

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

这是我初始化 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”,除非我按下地点选择器上的后退按钮!之后一切照常进行,我想要执行的操作也能正常工作(这意味着地址已被正确选择)

有个类似的问题here评论表明调用者 Activity 可能在 list 中有 android:noHistory="true"。但是我查了一下,不是。这是我的 list 中的相应 fragment :

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

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

编辑1:

我还尝试将 android:noHistory="false" 显式添加到 list 中。没有变化。

最佳答案

您可以使用 OnClickListener 或尝试为 startActivityForResult 添加 if 条件 MotionEvent.ACTION_UP 因为碰巧 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;
    }
});

关于Android PlacePicker 不返回选择的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51145927/

相关文章:

javascript - 我可以在Android应用程序中使用javascript吗?

android - 获取错误 : *****. 未找到 apk

android - MPAndroidChart - 折线图 - 缩小没有 X 值的图形部分

java - 从字符串中过滤出某些文本

javascript - Google map - 获取当前 map 位置的地址

swift - 如何使用 GMSPlacePickerViewController 类?

android - 确保 "Google Maps Android API v2"已启用。尝试实现 Google map 时出现此错误

google-maps - 意图包含一个地点,但实际上没有

javascript - Google Places API Web 服务服务器端解决方案

javascript - 如何限制 Google places api 对某些城市名​​称和所有州名称的自动完成?