Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutocompleteFragment results return a place with null attributes

I'm trying to make an application with a Navigation Drawer and a Fragment of a Map. I'm trying to have an autocomplete fragment but when i search a place i receive only the name and id attributes. Like this:

I/PLACE: Place{address=null, attributions=[], id=ChIJVXealLU_xkcRja_At0z9AGY, latLng=null, name=Amsterdam, openingHours=null, phoneNumber=null, photoMetadatas=null, plusCode=null, priceLevel=null, rating=null, types=null, userRatingsTotal=null, viewport=null, websiteUri=null}

Here is my MapsFragment

public class MapsFragment extends Fragment implements OnMapReadyCallback, LocationListener {

MapView mapView;
GoogleMap nMap;

AutocompleteSupportFragment autocompleteFragment;
PlacesClient placesClient;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.activity_maps, container, false);

    ...
    mapView = (MapView) rootView.findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    autocompleteFragment.onActivityResult(requestCode, resultCode, data);
}

...



@Override
public void onMapReady(GoogleMap googleMap) {
    Log.i("DEBUG", "onMapReady");

    nMap = googleMap;

    Context mContext = getActivity();
    if (mContext != null){
    Places.initialize(mContext, "*********MY API KEY*********");
    }else{
        Log.i("PLACE", "ERROR");
    }

    placesClient = Places.createClient(getActivity());

    autocompleteFragment = (AutocompleteSupportFragment)
            getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);


    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(@NonNull Place place) {
            Log.i("PLACE", place.toString());
            // Define a Place ID.
            //nMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));
            //nMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 12.0f));
        }

        @Override
        public void onError(@NonNull Status status) {
            Log.i("PLACE", status.toString());
        }
    });

    autocompleteFragment.setUserVisibleHint(true);
    autocompleteFragment.setHint("Select your city");


    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        nMap.setMyLocationEnabled(true);
    } else {
        // Show rationale and request permission.
        Log.d(TAG, "error permission denied");
    }
}

This i my map.xml

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

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>
...

I put my API KEY in the code (look map Fragment), in googles_maps_api.xml and also in gradle.properties

PLACES_API_KEY="MY API KEY"

Here is my dependencies for google maps

dependencies {
    ...
    implementation 'com.google.android.libraries.places:places:1.0.0'
}

I follow every steps for activate my API KEY

I also tried to secure my KEY with android restriction but nothing change

My billing system is enable.

like image 825
Matteo Antolini Avatar asked Feb 15 '19 10:02

Matteo Antolini


2 Answers

you didnt allow in setPlaceFields what do you want.for example i want latlang so i have added LAT_LNG. see below.

 autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.LAT_LNG));
like image 184
Shahjad Ans Sunny Avatar answered Nov 17 '22 01:11

Shahjad Ans Sunny


If you want to use addressComponents object, you have to Add Place.Field.ADDRESS_COMPONENTS to the fields list. otherwise this object will be null.

like image 37
Sabrina Avatar answered Sep 25 '22 07:09

Sabrina