AutocompleteFragment结果返回具有空属性的位置

时间:2019-02-15 10:48:33

标签: android google-maps google-maps-api-3

我正在尝试使用导航抽屉和地图片段制作应用程序。我正在尝试使用自动填充片段,但是当我搜索地点时,我只会收到 name id 属性。 像这样:

openwhisk.yml

这是我的MapsFragment

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}

这是我的map.xml

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");
    }
}

我在 googles_maps_api.xml gradle.properties

中将我的API KEY放入了代码(查看地图片段)中
...
<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>
...

这是我对Google地图的依赖

PLACES_API_KEY="MY API KEY"

我跟随每个steps激活我的API密钥

我也尝试通过android限制来保护自己的KEY,但没有任何变化

我的计费系统已启用。

3 个答案:

答案 0 :(得分:9)

  

您没有在setPlaceFields中允许您想要什么。例如,我想要latlang,所以我添加了LAT_LNG。见下文。

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

答案 1 :(得分:1)

如果要使用addressComponents对象,则必须将 Place.Field.ADDRESS_COMPONENTS 添加到字段列表中。否则,该对象将为null。

答案 2 :(得分:0)

使用相同:

List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.ADDRESS,Place.Field.LAT_LNG);

但是仍然给定了lat n lng的空值,剩余值得到i。