选择位置后,为什么PlaceAutoCompleteFragment不启动onPlaceSelected回调?

时间:2019-01-07 16:03:20

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

当我在虚拟应用程序的AutoCompleteFragment中选择一个位置时,除了选择一个位置并在logcat中打印其名称外,回调OnPlaceSelected不会被调用并且什么也没有发生。

我试图将google的代码放在这里,但仍然面临相同的问题。我试图将API版本从28降低到25,但这并没有改变。

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final String TAG = "GoogleMaps";
private GoogleMap mMap;
PlaceAutocompleteFragment placeAutoComplete;

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

    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i(TAG, "Place: " + place.getName());
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i(TAG, "An error occurred: " + status);
        }
    });

    // 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));


}

}

预期结果应该是我选择的位置必须在logcat中打印出来。请帮忙。

0 个答案:

没有答案