I am using latest Place Picker control which Android provides. It's really easy to use.
Now, the questions are:
Can I restrict the searching area to India only?
Can I change the default marker icon which resides in the PlacePicker control?
NOTE: I am not using any MapView or Fragment for displaying any map. I am using PlacePicker and not using GoogleMap.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(mActivity, data);
Log.d(TAG, "onActivityResult: " + place.getAddress());
if (place.getAddress() != null) {
// ask for geolocation data
Geocoder gcd = new Geocoder(mActivity, Locale.getDefault());
try {
addresses = gcd.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
if (addresses.get(0).getCountryCode().equalsIgnoreCase(getString(R.string.india))) {
//write the code here
} else {
Toast.makeText(mActivity, "Invalid places", Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
Related
I'm trying to get a Place from the Autocomplete of Google maps and then using its' longitude and latitude but every time I get a Place with the right name and all the other attributes got null in them. Does anyone know how to fix that?
Example of a Place I get:
I/System.out: Place{address=null, addressComponents=null, businessStatus=null, attributions=[], id=ChIJOwg_06VPwokRYv534QaPC8g, latLng=null, name=New York, openingHours=null, phoneNumber=null, photoMetadatas=null, plusCode=null, priceLevel=null, rating=null, types=null, userRatingsTotal=null, utcOffsetMinutes=null, viewport=null, websiteUri=null}
The code [the problem is at "setTargetPlace" (method is down the page)]:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place newTargetPlace = Autocomplete.getPlaceFromIntent(data);
System.out.println(newTargetPlace);
dataCenter.setTargetPlace(newTargetPlace);
dataCenter.createParty();
String response = dataCenter.getNextResponse();
System.out.println(response);
String[] commandAndPara = dataCenter.getCommandFromMsg(response);
String command = commandAndPara[0];
String para = commandAndPara[1];
if(command.equals(DataCenter.MSG_OK)) {
dataCenter.setPartyCode(para);
moveToParty();
}
else {
if(command.equals(DataCenter.MSG_FULL)) {
System.out.println("FULL");
}
else { //ERROR
System.out.println("ERROR");
}
}
} 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);
}
The method:
public void setTargetPlace(Place targetPlace) {
this.targetPlace = targetPlace;
this.target = this.targetPlace.getName();
LatLng targetCords = this.targetPlace.getLatLng();
this.targetX = targetCords.longitude;
this.targetY = targetCords.latitude;
}
The reason why you are getting the latitude and longitude as null is because you didn't include LAT_LNG as part of the fields you want to retrieve. See the code snippet below to see how you can include it
val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)
So when you create the intent and pass the field variable, and then go ahead to launch it like this
val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields)
.build(requireContext())
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)
it will return with the latitude and longitude in onActivityResult()
I followed this guide on implementing placesautocomplete
https://developers.google.com/places/android-sdk/autocomplete
I followed the intent one not the fragment. Nothing happens when I select a place. It just shows me this picture when I click on a place :
enter image description here
I tried implementing the fragment version but same thing. Nothing happens.
#OnClick(R.id.editText) void searchLocation() {
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).setTypeFilter(TypeFilter.ADDRESS).setCountry("PH").build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
displayLocationSettingsRequest(getApplicationContext());
break;
case AUTOCOMPLETE_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
LatLng latLng = place.getLatLng();
String address = place.getAddress();
if (getIntent().getIntExtra("switch", 0) == 0) {
returnIntent.putExtra("pickup", address);
returnIntent.putExtra("latlng", latLng);
} else {
returnIntent.putExtra("dropoff", address);
returnIntent.putExtra("latlng", latLng);
}
selectedAddress = address;
editText.setText(address);
location.setText(address);
Toast.makeText(getApplicationContext(), address, Toast.LENGTH_SHORT).show();
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(getApplicationContext(), status.toString(), Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
Toast.makeText(getApplicationContext(), "CANCELLED", Toast.LENGTH_SHORT).show();
}
}
}
I am forced to cancel the operation as selecting a place doesnt do anything and so resultcode always returns RESULT_CANCELLED
I have a problem with google map api. I used Api Key in my project and also i linked release SHA1 Key. It is working correctly in debug apk, but in release apk place autocompleteview is close automatically. I checked the Logcat, in logcat the api key is different from the key i used in meta-data in android manifest file.
private void manualLocation() {
try {
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build((Activity) context);
startActivityForResult(intent, 1);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.e("Tag", "Place: " + place.getAddress() + place.getPhoneNumber());
Log.e("Tag", "Place: " + place.getLatLng() + place.getPlaceTypes());
if (signup.matches("1")) {
latitude = String.valueOf(place.getLatLng().latitude);
longitude = String.valueOf(place.getLatLng().longitude);
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.e("Tag", status.getStatusMessage());
locationStatus = "0";
AppPreferences.savePreferences(Location.this, "locationStatus", locationStatus);
} else if (resultCode == RESULT_CANCELED) {
locationStatus = "0";
AppPreferences.savePreferences(Location.this, "locationStatus", locationStatus);
// The user canceled the operation.
}
}
}
}
I used the key in meta-data "AIzaSyBH9saN7RRHev1QNKWqtIjejojvqJuCswU" but in Logcat the key is different. i show you the Logcat. Can anyone help me?
BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/placesandroid/v1/autocompleteWidget?key=AIzaSyBb2MNLJEmWt-FLJqN28D_-WuxQiMwzUhU
this is the Logcat response
I am trying to launchGoogle Map's Search activity from my application to fetch address from user selected location. I could launch the search activity as per Google' guidelines, but I am having trouble in figuring out how to launch search activity for the result (in this case user selected location as address).
From below screen, User could pick "Peet's Coffee & Tea" as her location and control should return back to my application with its address.
Thanks for your time,
I could achieve this by using Google PlacePicker API by doing so
try
{
PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(getActivity());
// Start the Intent by requesting a result, identified by a request code.
startActivityForResult(intent, ET_PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e)
{
GooglePlayServicesUtil
.getErrorDialog(e.getConnectionStatusCode(), getActivity(), 0);
} catch (GooglePlayServicesNotAvailableException e)
{
Toast.makeText(getActivity(), "Google Play Services is not available.",
Toast.LENGTH_LONG)
.show();
}
and in ActivityResult() -
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == ET_PLACE_PICKER_REQUEST)
{
if (resultCode == Activity.RESULT_OK)
{
final Place place = PlacePicker.getPlace(getContext(), data);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
final String placeId = place.getId();
//Your Code
}
}}
We are using Google PlaceAutocomplete for city picker. We need to get country code for picked city. I am trying to use place.getLocale() but its null. Is there a way I can get Country ISO code from PlaceAutocomplete returned data.
in gradle:
compile 'com.google.android.gms:play-services-places:10.0.1'
code:
private void openCityPicker() {
try {
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
.build();
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.setFilter(typeFilter)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
com.google.android.gms.location.places.Place googleApiPlace = PlaceAutocomplete.getPlace(this, data);
Log.d(TAG, "onActivityResult: " + googleApiPlace.getAddress());
Log.d(TAG, " googleApiPlace.getLocale().getCountry(): " + googleApiPlace.getLocale().getCountry());
Log.d(TAG, " googleApiPlace.getLocale().getDisplayCountry(): " + googleApiPlace.getLocale().getDisplayCountry());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
late but right answer
you can get country code using this
add Place.Field.ADDRESS_COMPONENTS field into your fields list
List<Place.Field> fields = Arrays.asList(Place.Field.ADDRESS_COMPONENTS);
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields)
.setTypeFilter(TypeFilter.CITIES)
.build(mActivity);
startActivityForResult(intent, requestCode);
when you get the result into onActivityResult() you'll get full details of location into that you will find country
if (place.getAddressComponents() != null) {
List<AddressComponent> addressComponents = place.getAddressComponents().asList();
for (int i = 0; i < addressComponents.size(); i++) {
if (addressComponents.get(i).getTypes().get(0).equals("country")) {
countryCode = addressComponents.get(i).getShortName();
break;
}
}
}