Android using PlaceAutoCompleteActivity (Google Pl

2019-07-12 14:25发布

问题:

I have made a very simple example of the issue I am having with the PlaceAutoCompelteActivity. My map will show on the screen, along with 2 text search bars on top. The issue is that once I click in the textview, a keyboard will come up and a cursor will blink in the textview (as expected), however after I type the first letter (if i type quickly I can get 2 letters in), the keyboard will go away and the search box will blank out and the cursor will go away (return to the preclicked on state.) Here is a link to the documentation: https://developers.google.com/places/android-api/autocomplete?hl=en

The code for the simple activity and the xml file are below:

java activity:

package com.blah.android.backseatbuddy;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class RouteActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    //    private EditText beginningDestination;
//    private EditText endingDestination;
    PlaceAutocompleteFragment beginningDestination;
    PlaceAutocompleteFragment endingDestination;

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

        beginningDestination = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.beginning_destination);
        beginningDestination.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i("beginningDestination ", "Place: " + place.getName());
            }

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

        endingDestination = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.ending_destination);
        endingDestination.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i("endingDestination ", "Place: " + place.getName());
            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i("endingDestination ", "An error occurred: " + status);
            }
        });
//        beginningDestination = (EditText) findViewById(R.id.beginning_destination);
//        endingDestination = (EditText) findViewById(R.id.ending_destination);

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


    }


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

and xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/relativeLayoutFragment"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <fragment
        android:id="@+id/beginning_destination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

    <fragment
        android:id="@+id/ending_destination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.greg.android.backseatbuddy.RouteActivity"/>

</LinearLayout>

回答1:

You may need to insert your own API key (if you haven't done so already) that can be acquired through the Google developers console. Details here: https://developers.google.com/places/android-api/signup?hl=en Also, in the Google developers console, add Google Places API to the list of enabled API's in your project.