1

This error has been bugging me since more than a Day now. I have Already searched thoroughly, but none of the answer provided a solution for me. I have properly set up the API key with App restrictions providing the package name and SHA1 key. Here is the code manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <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">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity android:name=".PlacePickerActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->


        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">

        </activity>

    </application>

</manifest>

MapsActivity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

PlacePickerActivity

public class PlacePickerActivity extends AppCompatActivity {

    int PLACE_PICKER_REQUEST=1;
    TextView tvPlace;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_picker);
        tvPlace=findViewById(R.id.tv_Place);
    }

    public void goPlacePicker(View view) {
        PlacePicker.IntentBuilder builder=new PlacePicker.IntentBuilder();
        try{
            Log.e("error","1");
            startActivityForResult(builder.build(PlacePickerActivity.this),PLACE_PICKER_REQUEST);

        }catch(GooglePlayServicesRepairableException e)
        {
            Log.e("Repairable",e.toString());

            e.printStackTrace();
        }
        catch(GooglePlayServicesNotAvailableException e){
            Log.e("Not Available",e.toString());
            e.printStackTrace();
        }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == PLACE_PICKER_REQUEST){
            if(resultCode == RESULT_OK){
                Log.e("Activity","2");
                Place place=PlacePicker.getPlace(PlacePickerActivity.this,data);
                Log.e("PlacePicker","3");
                tvPlace.setText(place.getAddress());
            }
        }

    }
}

Thanks in advance for any possible help..

Guneet
  • 209
  • 1
  • 4
  • 12

1 Answers1

7

Notice: The Google Play Services version of the Places SDK for Android (in Google Play Services 16.0.0) is deprecated as of January 29, 2019, and will be turned off on July 29, 2019. A new version of the Places SDK for Android is now available. We recommend updating to the new version as soon as possible. For details, see the migration guide.

You can check the above message in this link under Depreciation notice.

Place APIs are moved to a separate library instead of Google Play Services. So you have to migrate to New Place API library.


You can follow this migration guideline or below code to integrate Places SDK for Android.

1. Add this dependency in app level build.gradle file:
implementation 'com.google.android.libraries.places:places:1.0.0'

Note: minSdkVersion of your application project should be 14 or higher

2. Initialize Places in your Activity.
Places.initialize(getApplicationContext(), YOUR_API_KEY);

3. Call below function when you want to open PlaceAutocomplete Activity

private void startAutocompleteActivity() {
        List<com.google.android.libraries.places.api.model.Place.Field> placeFields = new ArrayList<>(Arrays.asList(com.google.android.libraries.places.api.model.Place.Field.values()));
        List<TypeFilter> typeFilters = new ArrayList<>(Arrays.asList(TypeFilter.values()));
// Create a RectangularBounds object.
  RectangularBounds bounds = RectangularBounds.newInstance(
    new LatLng(-33.880490, 151.184363),
    new LatLng(-33.858754, 151.229596));
        Intent autocompleteIntent =
                new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, placeFields)
                        .setLocationBias(bounds)
                        .setTypeFilter(typeFilters.get(0))
                        .build(this);
        startActivityForResult(autocompleteIntent, 1001);
    }  

4. Write below code of onActivityResult

/**
     * Override the activity's onActivityResult(), check the request code, and
     * do something with the returned place data (in this example it's place name and place ID).
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1001) {
            if (resultCode == RESULT_OK) {
                Place place = Autocomplete.getPlaceFromIntent(data);
                Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                // TODO: Handle the error.
                Status status = Autocomplete.getStatusFromIntent(data);
                Log.i(TAG, status.getStatusMessage());
            } else if (resultCode == RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }


You can check this Demo application for more detail


Now most important thing is that you have to enable billing to use New Place API for Android.

To use the Places SDK for Android, you must include an API key with all API requests and you must enable billing on each of your projects.

Check this link for more info and pricing.

SKU: Basic Data

Fields in the Basic category are included in the base cost of the Places request, and do not result in any additional charge. The Basic Data SKU is triggered when any of these fields are requested: ADDRESS, ID, LAT_LNG, NAME, OPENING_HOURS, PHOTO_METADATAS, PLUS_CODE, TYPES, USER_RATINGS_TOTAL, VIEWPORT.

You can check pricing and other SKUs in the same link given above.

primo
  • 1,340
  • 3
  • 12
  • 40
Viraj Patel
  • 2,113
  • 16
  • 23
  • What are the constants BOUNDS_MOUNTAIN_VIEW and SELECT_REQUEST_PLACE for?? – Guneet Feb 23 '19 at 05:08
  • Check my update answer. BOUNDS_MOUNTAIN_VIEW was set for bias autocomplete results to a specific geographic region.You can remove in your case. SELECT_REQUEST_PLACE was constant field for startActivityForResult so I can get and compare result using same value. Here it is 1001. – Viraj Patel Feb 23 '19 at 07:13
  • For autocomplete it is showing REQUEST_DENIED error .Any ideas how to resolve it? – Guneet Feb 25 '19 at 06:22
  • Might be your API key is wrong. [Check this answer](https://stackoverflow.com/questions/9060563/google-places-api-request-denied). You have to **use Server key (auto created by Google Service)** which will be available in your project's https://console.developers.google.com – Viraj Patel Feb 25 '19 at 06:50
  • You can [refer Troubleshooting section in this link](https://developers.google.com/places/web-service/faq) as well for REQUEST_DENIED issue. – Viraj Patel Feb 25 '19 at 07:00
  • Thanks a lot Viraj .You helped a lot. One last thing could you please help me with the autocomplete widget as I am not able to fully understand the guide. It would be of great help – Guneet Feb 26 '19 at 12:00
  • Hi @Guneet sure. What do you want to understand? – Viraj Patel Feb 26 '19 at 13:00
  • So what I was trying to do is have an AutoCompleteTextView and when the user enters some text of the location, he gets a list of suggested locations. (Can send you the code).Was doing that using adapters and GoogleClient. But got error that PLACES_API_ACCESS not configured. So it would be of great help if you could help me in doing this using the new method – Guneet Feb 26 '19 at 13:10
  • You can share code with me bz I am not getting the idea what is the problem in your code. PLACES_API_ACCESS not configured error is possible in many cases like Place API is not enabled, Wrong Package or SHA1 is entered, wrong API_KEY is used etc. – Viraj Patel Feb 26 '19 at 13:51
  • http://txt.do/15rjl This is activity code and http://txt.do/15rjq , this is the adapter . – Guneet Feb 26 '19 at 14:58
  • I want that when user starts writing in location textfield a list of suggested locations should come up. – Guneet Feb 26 '19 at 15:01
  • I have question, compare to older places-ui, its doesnt provide picker to pick location is there any way to use this feature in latest place library – Arul Dec 08 '19 at 14:24