ApiException: 9003: PLACES_API_ACCESS_NOT_CONFIGURED

泪湿孤枕 提交于 2019-11-26 09:04:29

Many friends suggested me to post how I have implemented this(AutoComplete) on my Project .So I am suggested you to see this migration guide and implement on your project step by step.

Migration Guide:-https://developers.google.com/places/android-sdk/client-migration

Follows this steps to implement AutoComplete in your project :-

There is two way to implement to AutoComplete .

  1. Using Intent
  2. Using AutocompleteFragment

    In both case follow these steps:-

     1. Add this line in build.gradle file
    
    dependencies {
      implementation 'com.google.android.libraries.places:places:1.0.0'
    }
    
     2. Add other dependencies (This is optional) .
    
    dependencies {
      implementation 'com.google.android.libraries.places:places-compat:1.0.0'
    }
    
    implementation 'com.google.android.gms:play-services-places:16.0.0'
    
     3. // Add an import statement for the client library.
    
    import com.google.android.libraries.places.api.Places;
    
    // Initialize Places.
    
    Places.initialize(getApplicationContext(), apiKey);
    
    // Create a new Places client instance.
    
    PlacesClient placesClient = Places.createClient(this);
    

    ----------Now Choose any of the method you want to implement in your project.-----

    Using Intent

     if (!Places.isInitialized()) {
            Places.initialize(getApplicationContext(), "YOUR_API_KEY");
        }
    
        ...
    
        // Set the fields to specify which types of place data to return.
        List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
    
        // Start the autocomplete intent.
        Intent intent = new Autocomplete.IntentBuilder(
                AutocompleteActivityMode.FULLSCREEN, fields)
                .build(this);
        startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
    
    
    and OnActivityResult method-------
    
    
        /**
         * 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 == AUTOCOMPLETE_REQUEST_CODE) {
                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.
                }
            }
        }
    

    Using Fregment

    Initialize this Autocomplete Fregment on .xml file

    <fragment
      android:id="@+id/autocomplete_fragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:name=
    "com.google.android.libraries.places.widget.AutocompleteSupportFragment"
      />
    

    -------------- and on .class file--------------------------

    Initialize Places, passing the application context and your API key. Initialize the AutocompleteSupportFragment. Call setPlaceFields() to indicate the types of place data that you want to get. Add a PlaceSelectionListener to do something with the result, as well as handle any errors that might occur. The following example shows adding an autocomplete widget to an activity:

     if (!Places.isInitialized()) {
            Places.initialize(getApplicationContext(), "YOUR_API_KEY");
        }
    
    
        // Initialize the AutocompleteSupportFragment.
    
        AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    
    
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
    
    
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            }
    
            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i(TAG, "An error occurred: " + status);
            }
        });
    

And Get LATLONG from selected place like this:-

    LatLng latLng = place.getLatLng();
    String mStringLatitude = String.valueOf(latLng.latitude);
    String mStringLongitude = String.valueOf(latLng.longitude);

Hope this will help you .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!