android - Google places打开Map1秒后关闭

wbgh16ku  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(128)

我创建了一个活动的意图,通过点击一个按钮,应该打开谷歌的地方,但它再次关闭真的很快,说没有选择的位置,并返回到主要活动,然后什么也没有发生,如果我再次点击。
我的api应该没问题,我已经检查了连接到api密钥的SHA1指纹是否正确。
结果代码为2
它在此之前的Activity中工作过,但我需要它在我单击按钮时打开,而现在当我尝试将此新Activity作为Intent打开时,它不会工作。

public class MapActivity extends AppCompatActivity {

int PLACE_PICKER_REQUEST = 1;
int status;

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

    status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (status != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
            GooglePlayServicesUtil.getErrorDialog(status, this,
                    100).show();
        }
    }
    if (status == ConnectionResult.SUCCESS) {
        int PLACE_PICKER_REQUEST = 199;
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
        Context context = this;
        try {
            startActivityForResult(builder.build(context), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    System.out.println("Result code: " + resultCode);
    System.out.println("Request code: " + requestCode);
    if (requestCode == 100) {
        status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    }
    if (requestCode == 199) {

        //process Intent......
        if (data != null) {
            Place place = PlacePicker.getPlace(data, this);
            String toastMsg = String.format("Place: %s", place.getName());
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        } else {
            String toastMsg = ("No location selected.");
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        }

    }
}

}
这是从创建新Intent到Map的Intent

public void onClick(View view) {
        Intent i = new Intent(this, MapActivity.class);
       startActivity(i);
   }
xcitsw88

xcitsw881#

我认为这是因为您使用了旧的getPlace方法
尝试交换参数,将其从:

Place place = PlacePicker.getPlace(data, this);

Place place = PlacePicker.getPlace(getContext(), data);

更新#2

启用Google在开发者控制台中放置API并将这些行添加到AndroidManifest

<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="ADD_YOUR_API_KEY_HERE" />

更新#3

经过一些搜索后,似乎有其他人遇到了相同的问题。请查看以下链接:
https://github.com/zhangtaii/react-native-google-place-picker/issues/21
https://stackoverflow.com/a/32751164/
https://github.com/googlesamples/android-play-places/issues/13

相关问题