Search in sources :

Example 6 with Place

use of com.google.android.libraries.places.api.model.Place in project iNaturalistAndroid by inaturalist.

the class LocationChooserActivity method getPlaceDetails.

private void getPlaceDetails(AutocompletePrediction prediction, int index, CountDownLatch latch) {
    new Thread(() -> {
        if (prediction.getPlaceId() == null) {
            // We only show predictions with place IDs (so we can retrieve exact lat/lng)
            latch.countDown();
            return;
        }
        List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.VIEWPORT);
        FetchPlaceRequest request = FetchPlaceRequest.builder(prediction.getPlaceId(), fields).build();
        mPlacesClient.fetchPlace(request).addOnSuccessListener(fetchPlaceResponse -> {
            // Only when successfully fetching the place details -> add it to the results list
            Place googlePlace = fetchPlaceResponse.getPlace();
            LatLngBounds viewport = googlePlace.getViewport();
            Double radius = null;
            Double latitude = googlePlace.getLatLng().latitude;
            Double longitude = googlePlace.getLatLng().longitude;
            if (viewport != null) {
                LatLng center = viewport.getCenter();
                LatLng northeast = viewport.northeast;
                // Radius is the largest distance from geom center to one of the bounds corners
                radius = Math.max(distanceInMeters(latitude, longitude, center.latitude, center.longitude), distanceInMeters(latitude, longitude, northeast.latitude, northeast.longitude));
            } else {
                radius = 10.0;
            }
            if (index < mPlaces.size()) {
                mPlaces.get(index).accuracy = radius;
                mPlaces.get(index).latitude = latitude;
                mPlaces.get(index).longitude = longitude;
            }
            latch.countDown();
        }).addOnFailureListener(e -> {
            latch.countDown();
        });
    }).start();
}
Also used : Address(android.location.Address) TypeFilter(com.google.android.libraries.places.api.model.TypeFilter) CameraUpdateFactory(com.google.android.gms.maps.CameraUpdateFactory) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) Arrays(java.util.Arrays) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) Uri(android.net.Uri) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) LocationListener(android.location.LocationListener) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) VisibleRegion(com.google.android.gms.maps.model.VisibleRegion) ActionBar(androidx.appcompat.app.ActionBar) Manifest(android.Manifest) Locale(java.util.Locale) Handler(android.os.Handler) View(android.view.View) Animation(android.view.animation.Animation) ContextCompat(androidx.core.content.ContextCompat) Log(android.util.Log) ConnectivityManager(android.net.ConnectivityManager) LatLng(com.google.android.gms.maps.model.LatLng) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) IntentFilter(android.content.IntentFilter) NetworkInfo(android.net.NetworkInfo) Place(com.google.android.libraries.places.api.model.Place) BroadcastReceiver(android.content.BroadcastReceiver) Geocoder(android.location.Geocoder) Logger(org.tinylog.Logger) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TextView(android.widget.TextView) ListView(android.widget.ListView) Location(android.location.Location) Marker(com.google.android.gms.maps.model.Marker) LocationManager(android.location.LocationManager) TextWatcher(android.text.TextWatcher) Context(android.content.Context) KeyEvent(android.view.KeyEvent) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Bridge(com.livefront.bridge.Bridge) HashMap(java.util.HashMap) Intent(android.content.Intent) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Editable(android.text.Editable) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) InputMethodManager(android.view.inputmethod.InputMethodManager) PermissionChecker(androidx.core.content.PermissionChecker) AnimationUtils(android.view.animation.AnimationUtils) SuppressLint(android.annotation.SuppressLint) MenuInflater(android.view.MenuInflater) MotionEvent(android.view.MotionEvent) Toast(android.widget.Toast) Menu(android.view.Menu) State(com.evernote.android.state.State) SupportMapFragment(com.google.android.gms.maps.SupportMapFragment) DialogInterface(android.content.DialogInterface) MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) Places(com.google.android.libraries.places.api.Places) IOException(java.io.IOException) Point(android.graphics.Point) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) Spinner(android.widget.Spinner) TimeUnit(java.util.concurrent.TimeUnit) Color(android.graphics.Color) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) Configuration(android.content.res.Configuration) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) GoogleMap(com.google.android.gms.maps.GoogleMap) EditText(android.widget.EditText) OnClickListener(android.view.View.OnClickListener) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) List(java.util.List) ArrayList(java.util.ArrayList) LatLng(com.google.android.gms.maps.model.LatLng) Place(com.google.android.libraries.places.api.model.Place)

Example 7 with Place

use of com.google.android.libraries.places.api.model.Place in project open-event-orga-app by fossasia.

the class UpdateEventFragment method setupPlacesAutocomplete.

private void setupPlacesAutocomplete() {
    ApplicationInfo ai = null;
    try {
        ai = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    Bundle bundle = ai.metaData;
    String placesApiKey = bundle.getString("com.google.android.geo.API_KEY");
    Places.initialize(getActivity().getApplicationContext(), placesApiKey);
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS));
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {

        @Override
        public void onPlaceSelected(Place place) {
            Timber.d(place.getAddress());
            Event event = binding.getEvent();
            event.latitude = place.getLatLng().latitude;
            event.longitude = place.getLatLng().longitude;
            event.locationName = place.getAddress();
            event.searchableLocationName = place.getName();
        }

        @Override
        public void onError(Status status) {
            ViewUtils.showSnackbar(binding.getRoot(), status.getStatusMessage());
        }
    });
}
Also used : Status(com.google.android.gms.common.api.Status) PackageManager(android.content.pm.PackageManager) Bundle(android.os.Bundle) ApplicationInfo(android.content.pm.ApplicationInfo) AutocompleteSupportFragment(com.google.android.libraries.places.widget.AutocompleteSupportFragment) Event(com.eventyay.organizer.data.event.Event) PlaceSelectionListener(com.google.android.libraries.places.widget.listener.PlaceSelectionListener) Place(com.google.android.libraries.places.api.model.Place)

Example 8 with Place

use of com.google.android.libraries.places.api.model.Place in project open-event-orga-app by fossasia.

the class EventDetailsStepOne method setupPlacesAutocomplete.

private void setupPlacesAutocomplete() {
    ApplicationInfo ai = null;
    try {
        ai = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    Bundle bundle = ai.metaData;
    String placesApiKey = bundle.getString("com.google.android.geo.API_KEY");
    Places.initialize(getActivity().getApplicationContext(), placesApiKey);
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS));
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {

        @Override
        public void onPlaceSelected(Place place) {
            Timber.d(place.getAddress());
            Event event = binding.getEvent();
            event.latitude = place.getLatLng().latitude;
            event.longitude = place.getLatLng().longitude;
            event.locationName = place.getAddress();
            event.searchableLocationName = place.getName();
        }

        @Override
        public void onError(Status status) {
            ViewUtils.showSnackbar(binding.getRoot(), status.getStatusMessage());
        }
    });
}
Also used : Status(com.google.android.gms.common.api.Status) PackageManager(android.content.pm.PackageManager) Bundle(android.os.Bundle) ApplicationInfo(android.content.pm.ApplicationInfo) AutocompleteSupportFragment(com.google.android.libraries.places.widget.AutocompleteSupportFragment) Event(com.eventyay.organizer.data.event.Event) PlaceSelectionListener(com.google.android.libraries.places.widget.listener.PlaceSelectionListener) Place(com.google.android.libraries.places.api.model.Place)

Example 9 with Place

use of com.google.android.libraries.places.api.model.Place in project iNaturalistAndroid by inaturalist.

the class LocationChooserActivity method refreshPlaceQuery.

private void refreshPlaceQuery() {
    mHandler.removeCallbacksAndMessages(null);
    mHandler.postDelayed(() -> {
        if (mMap == null)
            return;
        String query = mLocationSearch.getText().toString();
        if (query.length() == 0) {
            mLoadingSearch.setVisibility(View.GONE);
            mLocationList.setVisibility(View.VISIBLE);
            return;
        }
        mLoadingSearch.setVisibility(View.VISIBLE);
        mLocationList.setVisibility(View.GONE);
        VisibleRegion region = mMap.getProjection().getVisibleRegion();
        RectangularBounds bounds;
        if (region.farRight.latitude >= region.nearLeft.latitude) {
            bounds = RectangularBounds.newInstance(region.nearLeft, region.farRight);
        } else {
            bounds = RectangularBounds.newInstance(region.farRight, region.nearLeft);
        }
        if (query.matches(REGEX_LAT_LNG)) {
            // Lat/lng search (not a place search)
            double lat = Double.valueOf(query.split(",")[0].trim());
            double lng = Double.valueOf(query.split(",")[1].trim());
            Logger.tag(TAG).info("Location search for lat/lng: " + lat + "/" + lng);
            mWaitForAllResults = new CountDownLatch(1);
            mPlaces = new ArrayList<>();
            INatPlace inatPlace = new INatPlace();
            inatPlace.id = null;
            inatPlace.title = String.format(getString(R.string.location_lat_lng), lat, lng);
            inatPlace.subtitle = null;
            inatPlace.latitude = lat;
            inatPlace.longitude = lng;
            inatPlace.accuracy = Double.valueOf(0);
            mPlaces.add(inatPlace);
            // Refresh results
            mPlaceAdapter = new LocationChooserPlaceAdapter(this, mPlaces);
            runOnUiThread(() -> {
                mLocationList.setAdapter(mPlaceAdapter);
                mLoadingSearch.setVisibility(View.GONE);
                mLocationList.setVisibility(View.VISIBLE);
            });
            mWaitForAllResults.countDown();
            return;
        }
        FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder().setSessionToken(mAutoCompleteToken).setLocationBias(bounds).setQuery(query).build();
        mQuery = query;
        mPlacesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
            if (!query.equals(mQuery))
                return;
            new Thread(() -> loadPlaceResults(response.getAutocompletePredictions())).start();
        }).addOnFailureListener((exception) -> {
            Logger.tag(TAG).error("Place not found: " + exception);
        });
    }, 500);
}
Also used : Address(android.location.Address) TypeFilter(com.google.android.libraries.places.api.model.TypeFilter) CameraUpdateFactory(com.google.android.gms.maps.CameraUpdateFactory) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) Arrays(java.util.Arrays) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) Uri(android.net.Uri) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) LocationListener(android.location.LocationListener) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) VisibleRegion(com.google.android.gms.maps.model.VisibleRegion) ActionBar(androidx.appcompat.app.ActionBar) Manifest(android.Manifest) Locale(java.util.Locale) Handler(android.os.Handler) View(android.view.View) Animation(android.view.animation.Animation) ContextCompat(androidx.core.content.ContextCompat) Log(android.util.Log) ConnectivityManager(android.net.ConnectivityManager) LatLng(com.google.android.gms.maps.model.LatLng) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) IntentFilter(android.content.IntentFilter) NetworkInfo(android.net.NetworkInfo) Place(com.google.android.libraries.places.api.model.Place) BroadcastReceiver(android.content.BroadcastReceiver) Geocoder(android.location.Geocoder) Logger(org.tinylog.Logger) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TextView(android.widget.TextView) ListView(android.widget.ListView) Location(android.location.Location) Marker(com.google.android.gms.maps.model.Marker) LocationManager(android.location.LocationManager) TextWatcher(android.text.TextWatcher) Context(android.content.Context) KeyEvent(android.view.KeyEvent) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Bridge(com.livefront.bridge.Bridge) HashMap(java.util.HashMap) Intent(android.content.Intent) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Editable(android.text.Editable) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) InputMethodManager(android.view.inputmethod.InputMethodManager) PermissionChecker(androidx.core.content.PermissionChecker) AnimationUtils(android.view.animation.AnimationUtils) SuppressLint(android.annotation.SuppressLint) MenuInflater(android.view.MenuInflater) MotionEvent(android.view.MotionEvent) Toast(android.widget.Toast) Menu(android.view.Menu) State(com.evernote.android.state.State) SupportMapFragment(com.google.android.gms.maps.SupportMapFragment) DialogInterface(android.content.DialogInterface) MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) Places(com.google.android.libraries.places.api.Places) IOException(java.io.IOException) Point(android.graphics.Point) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) Spinner(android.widget.Spinner) TimeUnit(java.util.concurrent.TimeUnit) Color(android.graphics.Color) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) Configuration(android.content.res.Configuration) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) GoogleMap(com.google.android.gms.maps.GoogleMap) EditText(android.widget.EditText) OnClickListener(android.view.View.OnClickListener) VisibleRegion(com.google.android.gms.maps.model.VisibleRegion) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with Place

use of com.google.android.libraries.places.api.model.Place in project android-places-demos by googlemaps.

the class PlaceAutocompleteActivity method onActivityResult.

// [START maps_places_on_activity_result]
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            // TODO: Handle the error.
            Status status = Autocomplete.getStatusFromIntent(data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
        // The user canceled the operation.
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
Also used : Status(com.google.android.gms.common.api.Status) Place(com.google.android.libraries.places.api.model.Place)

Aggregations

Place (com.google.android.libraries.places.api.model.Place)13 Status (com.google.android.gms.common.api.Status)8 Bundle (android.os.Bundle)6 Log (android.util.Log)6 LatLng (com.google.android.gms.maps.model.LatLng)6 PlacesClient (com.google.android.libraries.places.api.net.PlacesClient)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Intent (android.content.Intent)5 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)5 FetchPlaceRequest (com.google.android.libraries.places.api.net.FetchPlaceRequest)5 AutocompleteSupportFragment (com.google.android.libraries.places.widget.AutocompleteSupportFragment)5 PlaceSelectionListener (com.google.android.libraries.places.widget.listener.PlaceSelectionListener)5 Arrays (java.util.Arrays)5 Places (com.google.android.libraries.places.api.Places)4 AutocompletePrediction (com.google.android.libraries.places.api.model.AutocompletePrediction)4 AutocompleteSessionToken (com.google.android.libraries.places.api.model.AutocompleteSessionToken)4 RectangularBounds (com.google.android.libraries.places.api.model.RectangularBounds)4 TypeFilter (com.google.android.libraries.places.api.model.TypeFilter)4 FindAutocompletePredictionsRequest (com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest)4