6

The autocomplete widget service in the Google Places API for Android is not working as expected. I have included the fragment in my xml page and also added the listener activity to my onCreate().
Then I start executing the autocomplete fragment, I click the first letter of the word I want to search and suddenly instead of showing suggestions the onError() gets executed and it closes.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment 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:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.akhil.maplocation.MapsActivity" />
    <fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />
</RelativeLayout>

The mainactivity for this is:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    LatLng fromadd;
     @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.
                    fromadd = place.getLatLng();
                    double lat = fromadd.latitude;
                    double lng = fromadd.longitude;        
                    gotoLocation(lat, lng, 15);
                }

                @Override
                public void onError(Status status) {
                    // TODO: Handle the error.
                    Toast.makeText(getBaseContext(),"failure",Toast.LENGTH_LONG).show();
                }
            });
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                fromadd = latLng;
                markerOptions.title(latLng.latitude + ":" + latLng.longitude + " " + latLng.toString());
                mMap.clear();
                mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                mMap.addMarker(markerOptions);
            }
        });


        mapFragment.getMapAsync(this);
}

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {                
            return;
        }
        mMap.setMyLocationEnabled(true);

}

Sreehari
  • 5,411
  • 2
  • 21
  • 56
Akhil
  • 481
  • 1
  • 7
  • 12
  • what is the error ? log the error from the placeautocmplete and post the log here . – Sagar Nayak Mar 15 '16 at 07:15
  • Status{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null} – Akhil Mar 15 '16 at 13:18
  • this might help you - http://stackoverflow.com/questions/30434238/place-picker-automatically-close-after-launch if this wont work inform me. i will post a full answer. – Sagar Nayak Mar 15 '16 at 13:38
  • Nope.. That didn't work. For me it doesn't close immediately for me. It closes after I type the first character. – Akhil Mar 15 '16 at 14:04

2 Answers2

7

You probably miss an API key, which is required for use of Google Places API. Check out this link. When you do all things specified in the link, also add this code to your AndroidManifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_KEY" />
L. Kolar
  • 525
  • 2
  • 7
  • I do have the API KEY code for the map. Should I create a new API KEY or can I use the same key? – Akhil Mar 15 '16 at 13:19
  • I got this when i logged it: Status{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null} – Akhil Mar 15 '16 at 13:24
  • 1
    I think that API key should be ok. But make sure that you enable your application in developers console. There you have to supply your application package name and application's SHA-1. Check out link in answer for information about enabling application in developers console and [this link](http://stackoverflow.com/questions/15727912/sha-1-fingerprint-of-keystore-certificate) for information how to obtain SHA-1. – L. Kolar Mar 15 '16 at 13:41
0

mMap need to be initialied before use of mMap.setOnMapClickListener(.... ; the gotoLocation(lat,lng,15) has to look as below :

private void gotoLocation(double lat, double lng, int i) {
                // TODO Auto-generated method stub   

                 Marker myMarker = map.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.home))
                .position(p1)
                .snippet("Lat:" + lat + "Lng:" + lng)
                .title("HOME"));

                 map = ((MapFragment) getFragmentManager()
                        .findFragmentById(R.id.map2)).getMap();
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), i));                  
            }    
ganchito55
  • 3,350
  • 4
  • 27
  • 41
NTsola
  • 41
  • 5