2

I want to implement placeautocompletefragment in my Android app. I am trying to use the instructions at this url but it is showing:

can not resolve symbol for placeautocompletefragment

Example of what I want to show

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) 

getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

6
  • 1) can u post your code here. <br/> 2)has the placeautocompletefragment been imported to ur activity Commented Feb 24, 2016 at 7:17
  • have you check my bellow answer... Commented Feb 24, 2016 at 8:38
  • check my answer....let me knw if you have any problem... Commented Feb 24, 2016 at 8:39
  • placeautocompletefragment is not going to imported.there is any library to add for this fragment. Commented Feb 25, 2016 at 5:11
  • placeautocomplete is also not importing. Commented Feb 25, 2016 at 5:32

2 Answers 2

1

Just Follow bellow Procedure but before that make sure you have added google library and api key

1.Add bellow function on you button click

private void openAutocompleteActivity() { try { // The autocomplete activity requires Google Play Services to be available. The intent // builder checks this and throws an exception if it is not the case. Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY) .build(mActivity); startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE); } catch (GooglePlayServicesRepairableException e) { // Indicates that Google Play Services is either not installed or not up to date. Prompt // the user to correct the issue. GoogleApiAvailability.getInstance().getErrorDialog(mActivity, e.getConnectionStatusCode(), 0 /* requestCode */).show(); } catch (GooglePlayServicesNotAvailableException e) { // Indicates that Google Play Services is not available and the problem is not easily // resolvable. String message = "Google Play Services is not available: " + GoogleApiAvailability.getInstance().getErrorString(e.errorCode); Log.e("Tag", message); Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show(); } } 

2.then add this function

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_AUTOCOMPLETE) { if (resultCode == RESULT_OK) { // Get the user's selected place from the Intent. Place place = PlaceAutocomplete.getPlace(mActivity, data); Log.i("TAG", "Place Selected: " + place.getName()); LatLng latLng = place.getLatLng(); latitude = latLng.latitude; longitude = latLng.longitude; Log.v(ApplicationsConstants.LoginDetails.LATITUDE, "" + latitude); Log.v(ApplicationsConstants.LoginDetails.LONGITUDE, "" + longitude); tvCity.setText(place.getName()); Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.CITY, "" + place.getName()); Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.LATITUDE, "" + latitude); Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.LONGITUDE, "" + longitude); } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { Status status = PlaceAutocomplete.getStatus(mActivity, data); Log.e("TAG", "Error: Status = " + status.toString()); } else if (resultCode == RESULT_CANCELED) { // Indicates that the activity closed before a selection was made. For example if // the user pressed the back button. } } } 
Sign up to request clarification or add additional context in comments.

Comments

1

Just in case you haven't figured it out yet. Then remember: You need to setup Google Play Services first before using it. Setup instructions are here.

A point to note is that you don't have to include the entire google play services library like this:

compile 'com.google.android.gms:play-services:9.2.1' 

You can include only the section that you need in your case it is Places. So it will be:

compile 'com.google.android.gms:play-services-places:9.2.1' 

A point to note when looking up resources on google is that earlier Places was bundled with Location services. So the resources on internet would be using them like this:

compile 'com.google.android.gms:play-services-location:X.X.X' 

However, THIS IS NOT THE CASE ANYMORE. Now Places and Location are separate.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.