When i search place in Autocomplete i get 'nothing found on request' in Xamarin.Android. How to fix this?
Inialization API
if (!PlacesApi.IsInitialized)
{
PlacesApi.Initialize(context, "MY_API_KEY");
}
Call Place Autocomplete
List<Place.Field> fields = new List<Place.Field>();
fields.Add(Place.Field.Id);
fields.Add(Place.Field.Name);
fields.Add(Place.Field.LatLng);
fields.Add(Place.Field.Address);
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.Overlay, fields)
.SetCountry("US")
.Build(this);
StartActivityForResult(intent, 0);
Related
I have a weird problem when dealing with Google Maps Autocomplete / Places API, it autocompletes the given address just fine:
Street Streetnumber, City, Country = Römerstraße 101, Reith bei Seefeld, Österreich
With the autocompleted value, I am trying to get the address_component data.
const autocomplete = new this.api.places.Autocomplete(
options.input,
{types: ['address'], sessiontoken: sessionToken}
),
inputObj = $(options.input);
inputObj.prop('placeholder', inputObj.data('geocode-autocomplete-placeholder'));
autocomplete.setFields(['address_component']);
autocomplete.addListener('place_changed', () => {
try {
callback(options, {'queryResult': {'res': [autocomplete.getPlace()]}}, this);
} catch (e) {
this.disableGeocoding();
console.log(e);
}
});
Problem is with the returned result, where "city" is missing:
I would expect every information that has been autocompleted to be in the "address_components" data.
Questions:
Why is city missing, and not found in another node, although autocompleted?
How can/should i get the city information?
According to these two links,
https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
https://developers.google.com/places/web-service/details
It says locality of response is represents the city part of the address.
So you can see locality part in the response.
Refer those links to get more information.
Is there any parameter I can add to a request such as the one in this function to display a name for an opened location, instead of showing the coordinates?
url(loc, label) {
const prefix = "https://www.google.com/maps/search/?api=1&query=";
url = prefix + `${loc.latitude},${loc.longitude}`;
return url;
}
For example this is possible in Apple Maps with the label in the function below:
url(loc, label) {
const latLng = `${loc.latitude},${loc.longitude}`;
return `maps:0,0?q=${latLng}(${label})`;
}
Maps URLs for Search only support two parameters; query and the optional query_place_id. There is no label parameter at this time. You can use query_place_id to show a textual name though, and Google specifically recommends this when querying lat/lng coordinates.
See this example: https://www.google.com/maps/search/?api=1&query=47.5951518,-122.3316393&query_place_id=ChIJKxjxuaNqkFQR3CK6O1HNNqY
Android intents do support using a label parameter.
Hope this answers your question.
I am using Places.GeoDataApi for Android and I get different search results depending on the location of the device performing the request. I need the results to be consistently located inside the bounds. I don't see where that could be setup in the getAutocompletePredictions request. Is there anything I am missing?
I get address/place autocomplete suggestions using the GoogleApiClient and Places API through:
Places.GeoDataApi.getAutocompletePredictions()
The method requires a GoogleApiClient object, a String to autocomplete, and a LatLngBounds object to limit the search range. This is what my usage looks like:
LatLngBounds bounds = new LatLngBounds(new LatLng(38.46572222050097, -107.75668023304138),new LatLng(39.913037779499035, -105.88929176695862));
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.build();
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi.getAutocompletePredictions(googleApiClient, "Starbucks", bounds, null);
Version in use: com.google.android.gms:play-services-location:8.3.0
Documentation:
https://developers.google.com/places/android/autocomplete
Good news. As of April 2018 Google added possibility to specify how to treat the bounds in autocomplete predictions. Now you can use the getAutocompletePredictions() method of GeoDataClient class with boundsMode parameter
public Task<AutocompletePredictionBufferResponse> getAutocompletePredictions (String query, LatLngBounds bounds, int boundsMode, AutocompleteFilter filter)
boundsMode - How to treat the bounds parameter. When set to STRICT predictions are contained by the supplied bounds. If set to BIAS predictions are biased towards the supplied bounds. If bounds is null then this parameter has no effect.
source: https://developers.google.com/android/reference/com/google/android/gms/location/places/GeoDataClient
You can modify your code to something similar to:
LatLngBounds bounds = new LatLngBounds(new LatLng(38.46572222050097, -107.75668023304138),new LatLng(39.913037779499035, -105.88929176695862));
GeoDataClient mGeoDataClient = Places.getGeoDataClient(getBaseContext());;
Task<AutocompletePredictionBufferResponse> results =
mGeoDataClient.getAutocompletePredictions("Starbucks", bounds, GeoDataClient.BoundsMode.STRICT, null);
try {
Tasks.await(results, 60, TimeUnit.SECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
e.printStackTrace();
}
try {
AutocompletePredictionBufferResponse autocompletePredictions = results.getResult();
Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
+ " predictions.");
// Freeze the results immutable representation that can be stored safely.
ArrayList<AutocompletePrediction> al = DataBufferUtils.freezeAndClose(autocompletePredictions);
for (AutocompletePrediction p : al) {
CharSequence cs = p.getFullText(new CharacterStyle() {
#Override
public void updateDrawState(TextPaint tp) {
}
});
Log.i(TAG, cs.toString());
}
} catch (RuntimeExecutionException e) {
// If the query did not complete successfully return null
Log.e(TAG, "Error getting autocomplete prediction API call", e);
}
I hope this helps!
I got the same problem.
Unfortunately, there is no way to get places in specific bounds using Google Places API Android.
However, you can still use Nearby Search using Google Places API Web Service.
Documentation here :
https://developers.google.com/places/web-service/search?hl=fr
You should then be able to set the bounds in parameters, and get the places inside the bounds from the JSON response, as explained in this answer :
https://stackoverflow.com/a/32404701/5446285
I posted about it an issue https://code.google.com/p/gmaps-api-issues/issues/detail?id=8387&thanks=8387&ts=1437921771
But still I want to know if anybody faced this and know the solution for this...
First I'm using nearBySearch http web service to get places around me via:
https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters
https://developers.google.com/places/webservice/search
Second step I use GeoDataApi.getPlaceById API for Android to get more details
about the places from step 1 above.
https://developers.google.com/places/android/place-details
I assume that PlaceId is 'Global reference' across all Google maps platforms.
The bug is: Sometimes (and even not so rarely) PlaceID's from nearBySearch does't exist in GeoDataApi.getPlaceById, and instead I get result of this form:
{
"html_attributions" : [],
"status" : "INVALID_REQUEST"
}
PlaceID for example: ChIJJcoXzj8oAxURY8ZGJx4crNo
Edit: This is the android code:
Places.GeoDataApi.getPlaceById(mGoogleApiClient, stringIDSArray).setResultCallback(new ResultCallback<PlaceBuffer>() {
#Override
public void onResult(PlaceBuffer places) {
if (places.getStatus().isSuccess()) {
//Actually for the specified PlaceID (ain't here real "Array") we expect loop iteration
for (int i=0; i<places.getCount(); i++) {
//str is NULL for the problematic PlaceID
String str = places.get(i).getName().toString();
}
}
places.release();
}
});
Thanks,
I am using a gmap autocomplete and sometimes the user doesn't hit any choice in the suggestion list. For example he types "Paris" in the input field and believes the search will be performed with Paris, however has the 'place_changed' of the gmap autcomplete was never called, the search cannot be perfomed.
How can I select by default the first choice of the suggestion list when the user doesn't make any choice ? I believe I could adapt the solution provided for the "enter issue" described here (Google maps Places API V3 autocomplete - select first option on enter) with a blur event handling, however this doesn't work.
Any hint ?
I think this is kind of defeating the purpose of the auto complete.
You could just retrieve the autocomplete predictions pro grammatically like so:
function initialize()
{
var service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({ input: 'some address' }, callback);
}
function callback(predictions, status)
{
if (status != google.maps.places.PlacesServiceStatus.OK)
{
alert(status);
return;
}
// Take the first result
var result = predictions[0]
// do as you wish with the result
}
Hope that helps