Reputation: 1322
I have added PlaceAutocompleteFragment
in my android app and it works perfectly when I search for a place. My question is, how do I let the user enter Latitude and Longitude values and place a marker at that coordinate? I would just like to hide "No results found" section and let user hit enter and get the coordinates.
Below is what I have so far.
<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"
/>
Code when a place is selected:
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
String placeName = place.getName().toString();
LatLng latLng = place.getLatLng();
mMap.addMarker(new MarkerOptions().position(latLng).title(placeName));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
@Override
public void onError(Status status) {
Log.i(TAG, "An error occurred: " + status);
}
});
I want to implement a search bar that's very much like Google Maps.
Upvotes: 1
Views: 852
Reputation: 321
@parth-bhoiwala Please follow below link for your solution.
https://developers.google.com/places/android-api/autocomplete#add_an_autocomplete_widget
In that link scroll down to go to "Get place predictions programmatically" section
You can create a custom search UI as an alternative to the UI provided by the autocomplete widget. To do this, your app must get place predictions programmatically. Your app can get a list of predicted place names and/or addresses from the autocomplete service by calling GeoDataApi.getAutocompletePredictions()
Upvotes: 0
Reputation: 2691
Instead of using a PlaceAutoCompleteFragment, use a PlaceAutocompleteIntent:
try {
Intent intent = new
PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(getThis());
startActivityForResult(intent, RequestCodes.PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
//Do something
} catch (GooglePlayServicesNotAvailableException e) {
//Do something
}
This will send an intent instead of directly displaying a fragment. Then, via the onResult method, you can catch the failures, and perform the reverse geocoding there:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch(requestCode){
case RequestCodes.PLACE_AUTOCOMPLETE_REQUEST_CODE:
switch(resultCode){
case RESULT_OK:
Place place = PlaceAutocomplete.getPlace(this, data);
//Do something with the address you got.
break;
case RESULT_ERROR:
Status status = PlaceAutocomplete.getStatus(this, data);
//In this case, if you typed coordinates instead of an address,
//you can use them to perform a reverse geocoding as you want.
break;
case RESULT_CANCELED:
break;
}
break;
}
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
Upvotes: 2
Reputation: 11
[The Google Place API translats] a human-readable address into a location on a map.
What you are trying to do is reverse geocoding.
Android has a Geocoder you can use for this purpose. You would have to implement your own fragment to do this, but it will not work using the PlaceAutocompleteFragment
.
Upvotes: 1