Skip to content

Instantly share code, notes, and snippets.

@William-ST
Created March 29, 2016 21:03
Show Gist options
  • Save William-ST/b2948a8a6cfced1218cbcd3146a393cf to your computer and use it in GitHub Desktop.
Save William-ST/b2948a8a6cfced1218cbcd3146a393cf to your computer and use it in GitHub Desktop.
Buscar direcciones a través de google places service
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.places.AutocompleteFilter;
import com.google.android.gms.location.places.AutocompletePrediction;
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created by William on 29/03/2016.
*/
public class GooglePlaceServicesUtil {
private final String TAG = GooglePlaceServicesUtil.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
private Context context;
/* GooglePlaces */
private LatLng southWest = null, nortHeast = null;
List<Integer> filterTypes = new ArrayList<Integer>();
private InterfacePlacesResult interfacePlacesResult;
public GooglePlaceServicesUtil(Context context, FragmentActivity fragmentActivity) {
this.context = context;
mGoogleApiClient = new GoogleApiClient
.Builder(context)
.enableAutoManage(fragmentActivity, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed");
}
})
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.e(TAG, "onConnected");
}
@Override
public void onConnectionSuspended(int i) {
Log.e(TAG, "onConnectionSuspended");
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed");
}
})
.build();
}
public void configSearchPlaces(LatLng southWest, LatLng nortHeast) {
this.southWest = southWest;
this.nortHeast = nortHeast;
filterTypes.add(Place.TYPE_STREET_ADDRESS);
}
public void configSearchPlaces(LatLng southWest, LatLng nortHeast, List<Integer> filterTypes) {
//Filter: https://developers.google.com/places/supported_types#table3
this.southWest = southWest;
this.nortHeast = nortHeast;
this.filterTypes = filterTypes;
}
public void searchPlaces(String text) {
if (southWest == null || nortHeast == null) {
Log.e(TAG, "Undefined configSearchPlaces");
return;
}
final List<ResultSearch> resultList = new ArrayList<>();
LatLngBounds bounds = new LatLngBounds(southWest, nortHeast);
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, text, bounds, AutocompleteFilter.create(filterTypes))
.setResultCallback(
new ResultCallback<AutocompletePredictionBuffer>() {
@Override
public void onResult(AutocompletePredictionBuffer buffer) {
if (buffer == null)
return;
if (buffer.getStatus().isSuccess()) {
for (AutocompletePrediction prediction : buffer) {
resultList.add(new ResultSearch(prediction.getPlaceId(), prediction.getDescription()));
Log.e(TAG, "prediction: " + prediction.getDescription());
}
interfacePlacesResult.getPlaces(resultList);
} else {
Log.e(TAG, buffer.getStatus().getStatusMessage());
}
buffer.release();
}
}, 60, TimeUnit.SECONDS);
}
public interface InterfacePlacesResult {
void getPlaces(List<ResultSearch> resultSearches);
}
public void setInterfacePlacesResult(InterfacePlacesResult interfacePlacesResult) {
this.interfacePlacesResult = interfacePlacesResult;
}
public static class ResultSearch {
private String idPlace;
private String place;
public ResultSearch(String idPlace, String place) {
this.idPlace = idPlace;
this.place = place;
}
public String getIdPlace() {
return idPlace;
}
public void setIdPlace(String idPlace) {
this.idPlace = idPlace;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
}
}
@William-ST
Copy link
Author

Use:
GooglePlaceServicesUtil googlePlaceServicesUtil = new GooglePlaceServicesUtil(this, this);
googlePlaceServicesUtil.configSearchPlaces(new LatLng(-18.820305, -83.907096), new LatLng(-0.380145, -68.182550));
googlePlaceServicesUtil.setInterfacePlacesResult(new GooglePlaceServicesUtil.InterfacePlacesResult() {
@OverRide
public void getPlaces(List<GooglePlaceServicesUtil.ResultSearch> resultSearches) {
Log.e(TAG, "result counts: "+resultSearches.size());
for (GooglePlaceServicesUtil.ResultSearch resultSearch : resultSearches){
Log.e(TAG, resultSearch.getIdPlace()+" : "+resultSearch.getPlace());
}
}
});
googlePlaceServicesUtil.searchPlaces("av aramburu");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment