0

I am trying to get a place by passing the place_id to the Places.GeoDataApi but it is not returning anything and I am sure that the place_id is correct.

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

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Places.GEO_DATA_API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();

    String placeId = "ChIJja4QaeH0GjkRKK5cBjTfKQo";

    Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
            .setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(@NonNull PlaceBuffer places) {
                    if(places.getStatus().isSuccess() && places.getCount()>0){
                        final Place myPlace = places.get(0);
                        Log.i(String.valueOf(MainActivity.this),
                                "Place found: " + myPlace.getName() + "\n Address: " + myPlace.getAddress());
                    } else {
                        Log.i(String.valueOf(MainActivity.this),"Place not found!");
                    }
                    places.release();
                }
            });
}

Edit: Added manifest.xml file

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

</application>

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
Gautam Hans
  • 135
  • 1
  • 12

1 Answers1

1

Code is very similar that one i implemented..except for one thing.

you forgot to call mGoogleApiClient.connect();

in your code:

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Places.GEO_DATA_API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this).build();

mGoogleApiClient.connect(); //add this

//now call the placeID codes Result callback

Edit

slight update to the condition:

 if(places.getStatus().isSuccess()){
    final Place myPlace = places.get(0);
    Log.i(String.valueOf(MainActivity.this),
            "Place found: " + myPlace.getName() + "\n Address: " + myPlace.getAddress());
} else {
    Log.i(String.valueOf(MainActivity.this),"Place not found!");
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Thank you. It worked! :) But I am getting Place Not Found in logs even though the place_id is correct. – Gautam Hans Apr 10 '17 at 09:22
  • yes, I am specifying the placeId like String placeId = "ChIJja4QaeH0GjkRKK5cBjTfKQo"; even then the place is not found. I've tried another ID too "ChIJfapwfcT0GjkR6aj2WIaRdIk" it should return "Virk Hospital and Maternity Home" but it's going to the else part. – Gautam Hans Apr 10 '17 at 09:32
  • I tried what you recommended but still same results. Don't know what's happening. – Gautam Hans Apr 10 '17 at 09:43
  • check if there is something in the logcat.like error or access denied ..and clean build the project.. – rafsanahmad007 Apr 10 '17 at 09:45
  • I built the project clean. Same problem. This is what I get in logs `I/com.example.gautam.placeiddemo.MainActivity@6763349: Place not found!` – Gautam Hans Apr 10 '17 at 09:48
  • I found what was causing this Status{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null} – Gautam Hans Apr 10 '17 at 09:54
  • 1
    Enable the Google Places API ...see this: http://stackoverflow.com/questions/31449649/android-google-places-api-getautocompletepredictions-returns-status-places-api – rafsanahmad007 Apr 10 '17 at 09:56