Basically I am trying to implement a PLACE_PICKER for Android and I am getting this confusing error here:
int PLACE_PICKER_REQUEST = 1;
PlacePicker.I
It looks like the main issue is that you're running with an older version of Google Play Services. So in order for it to work for you, you will need to update the version of Google Play Services that is installed on your device or emulator.
This also suggests that you don't have any code that checks if the version required is present on the device, and this is something that you should definitely add, since users in the field may run into the same issue.
The framework provides an easy way to add a prompt to the user to update Google Play Services if the version currently on the device is older than the one required by your app.
Here is a simple example that I just got working.
First, create a member variable status
:
int status;
Then, in onCreate()
of your Activity, check if Google Play Services are available. If not, then show the dialog that will prompt the user to upgrade:
status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
GooglePlayServicesUtil.getErrorDialog(status, this,
100).show();
}
}
Then, when you want to show the PlacePicker, ensure that status
indicates that Google Play Services are available. You may want to also give some indication to the user (add an else
here) if this condition is not met (also note the extra try/catch).
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();
}
}
One more thing you could also add would be to re-check the status in onActivityResult()
to see if the user has updated Google Play Services.
This is also where you would get the result of the PlacePicker dialog:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100){
status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
}
if (requestCode == 199){
//process Intent......
Place place = PlacePicker.getPlace(data, this);
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
Result: