android unable pick location from place picker - android

I am unable to pick the location from place picker.
It disables the select button when select place but when to search location and pick then it enable select button.
I am using play services version
com.google.android.gms:play-services-location:9.4.0
and com.google.android.gms:play-services:9.4.0
btn_locationSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(ChatThreadActivity.this),PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PLACE_PICKER_REQUEST:
Place place = PlacePicker.getPlace(this,data);
LatLng location = place.getLatLng();
String toastMsg = String.format("Place: %s", place.getName());
} }

If your problem is not solved yet. Enable Google Places API for Android in dev console.
It's because you didn't give permission to use Places on google maps. Just go to the link and enable Google Places API for Android.

It maybe a bit late but check out my answer to what sounds like the same problem (Disable select button while selecting location of anonymous location using Place picker google API in android)
Basically make sure that the API Key in your AndroidManifest.xml has the correct name. While com.google.android.maps.v2.API_KEY will work for the Maps API the Places API requires com.google.android.geo.API_KEY.
If you are using the Maps API then you can use the same key and both APIs will work using the latter kay name.

use this
<application>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
</application>
insted of this
com.google.android.maps.v2.API_KEY

The code looks fine. In fact, I tried re-running it and found no such observation; the SELECT button of the Use this location pop-up dialog was always enabled. However, when I manually disabled my Internet connection, the SELECT button became disabled. So, I believe, if your Internet connection is intermittent or has a problem, you might be able to observe the same thing.

Related

WifiP2pManager.discoverPeers fails in android 10

following code sample returns Error code: 0, which is the error code for internal error in android. Is there any workaround which can enable discovering peers in android 10 devices?
wifip2pmanager.discoverPeers(wifip2pmanagerChannel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
status.setText("Peer Discovery Started");
}
#Override
public void onFailure(int reason) {
status.setText("Error code:" + reason);
}
});
Exactly the same happened to me...
ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION are not enough. The user has to explcitly activate the location services!
(in my case turning on location solved the problem...)
This means: Either you activate location in your settings manually or you make a usability friendly request to the user to activate location services (looks similar to permission request window; see google maps)
See this question for example code of the latter. Hope this helps!
Edit: If you search for an anwser that not envolves any Google libs, see the anwser to this question.
In addition to the statement in the list, you also need to dynamically apply for this permission.

Google place picker closes immediately after launch

This is a problem that I've been stuck for weeks, but still, I can't solve it. My friend's google place picker worked, but not mine (even though we have the same code).
But once in 20-30 tries, it opens the UI lets me have a look at nearby places around my location (but that's a very very rare case).
I've done everything I possibly could.
1) I've enabled Google Places API for Android and generated my API key. I've also added my SHA-1 certificate fingerprint and package name.
2)I've imported everything related to google play services that I could in gradle.
implementation 'com.google.android.libraries.places:places-compat:1.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-vector-drawable:26.+'
implementation 'com.google.android.gms:play-services-places:16.0.0'
3)I've also given my API key inside of meta-data in my application tag in AndroidManifest.xml
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_place_api" />
</application>
4)I've copied the exact code that I got from Place-picker documentation site https://developers.google.com/places/android-sdk/placepicker
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(maps.this), PLACE_PICKER_REQUEST);
} catch (
GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (
GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
#Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
5)I've even enabled billing for my project, where Google charged me Re 1.
This is very important for my project, so please someone help me. I beg of you.
Edit: I'm getting some kind of error in my logcat too:
Place Picker closing due to PLACES_API_ACCESS_NOT_CONFIGURED
the Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
2019-05-12 08:07:57.106 2818-3899/? E/Volley: [171] BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/placesandroid/v1/placePicker?key=AIzaSyCz-bC6nazJur-gHEgVoZMujFDyVvs2n9M
2019-05-12 08:07:57.110 2818-20254/? E/Places: Places API for Android does not seem to be enabled for your app. See https://developers.google.com/places/android/signup for more details.
2019-05-12 08:07:57.112 2818-20254/? E/AsyncOperation: serviceID=65, operation=PlacePickerQuota
EDIT#2: Is there any solution to this? Please answer me.
You seem not to have enabled place API, below is the link to the steps to get that done.
https://support.google.com/googleapi/answer/6158841?hl=en
if you check your logcat well enough, maybe by selecting the types of log you want to see, you can select Error instead of verbose, then you will see the error like below
E/Places: Places API for Android does not seem to be enabled for your app. See https://developers.google.com/places/android/signup for more details.
I believe the issue is related to the deprecated version of Places SDK for Android. You have the following statement in gradle
implementation 'com.google.android.gms:play-services-places:16.0.0'
This refers to the Google Play version of Places SDK, that was deprecared. In order to use new static library of Places SDK the gradle should use
implementation 'com.google.android.libraries.places:places:1.1.0'
Please double check all steps mentioned in the documentation of new library to be sure that you migrated to new version correctly:
https://developers.google.com/places/android-sdk/start
I hope this helps!

Google Place Picker widget not working

According to This Official Post
I'm following below steps to integrate Place Picker UI dialog with Map.
launcher code
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
inside onActivityResult()
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(this, data);
tvChooseLocation.setText(place.getName());
}
}
I have added all permissions & API_KEY in Manifest file
Problem is I am not able to select location from Place Picker dialog.
Select button is disabled by default.
I have solved this so I don't quite know which one of these steps solves it
In console.developer.google.com
I enable Places API and Maps API, and download the json from here.
Also, get the API key from Credential page.
In Android Studio
In manifest, add in between <application> ... </application>
<meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_KEY_FROM_CREDENTIAL_PAGE"/>
In onCreate(), add this (though I don't quite sure this is needed or not)
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, this)
.build();
Everything else just follow the tutorial in here. Also, I tested it in release version. If you still have the same problem, try make a release one. I failed to get the location in emulator but it work just fine in my phone.
Hopefully this can help anyone in the future.
Try enabling google places api from google developer console
Link to console
--Wanted to comment though but do not have permission.

Google Places API Picker display Only Certain Types of Places

I am making certain android app that requires user to choose a place. I am planning to user google places API.Link: https://developers.google.com/places/android/
This API gives gives nice way to do so through through Place Picker. https://developers.google.com/places/android/placepicker
Now suppose I want to only show user places of food type (restaurant, hotel etc.) . Using Places Browser API this can be done by adding "type=restraunts" attribute in the requests.
Is there a way to show only places of certain types using the Google Places Picker for android ?
We are in 2017 and apparently this feature is still not implemented yet.
Even so, you can check the type(s) of the selected place on the activityResult and use it. In my case, I wanted the app only accept Hospitals and related services, so here's the code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//...
boolean isAnHospital = false;
selectedplace = PlacePicker.getPlace(this, data);
for (int i : selectedplace.getPlaceTypes()) {
if (i == Place.TYPE_DOCTOR || i == Place.TYPE_HEALTH || i == Place.TYPE_HOSPITAL) {
isAnHospital = true;
break;
}
}
if(isAnHospital){
//Right type of place
}else{
//Tell to the user to select an appropriate place
}
Hope it helps in some way. Sorry for the bad english.
This is still not possible (14 September 2015).
Here is the open issue.
(info copied from related post, and reverified)

How to implement offline speech recognition [duplicate]

It looks as though Google has made offline speech recognition available from Google Now for third-party apps. It is being used by the app named Utter.
Has anyone seen any implementations of how to do simple voice commands with this offline speech rec? Do you just use the regular SpeechRecognizer API and it works automatically?
Google did quietly enable offline recognition in that Search update, but there is (as yet) no API or additional parameters available within the SpeechRecognizer class. {See Edit at the bottom of this post} The functionality is available with no additional coding, however the user’s device will need to be configured correctly for it to begin working and this is where the problem lies and I would imagine why a lot of developers assume they are ‘missing something’.
Also, Google have restricted certain Jelly Bean devices from using the offline recognition due to hardware constraints. Which devices this applies to is not documented, in fact, nothing is documented, so configuring the capabilities for the user has proved to be a matter of trial and error (for them). It works for some straight away – For those that it doesn't, this is the ‘guide’ I supply them with.
Make sure the default Android Voice Recogniser is set to Google not
Samsung/Vlingo
Uninstall any offline recognition files you already have installed
from the Google Voice Search Settings
Go to your Android Application Settings and see if you can uninstall
the updates for the Google Search and Google Voice Search
applications.
If you can't do the above, go to the Play Store see if you have the
option there.
Reboot (if you achieved 2, 3 or 4)
Update Google Search and Google Voice Search from the Play Store (if
you achieved 3 or 4 or if an update is available anyway).
Reboot (if you achieved 6)
Install English UK offline language files
Reboot
Use utter! with a connection
Switch to aeroplane mode and give it a try
Once it is working, the offline recognition of other languages,
such as English US should start working too.
EDIT: Temporarily changing the device locale to English UK also seems to kickstart this to work for some.
Some users reported they still had to reboot a number of times before it would begin working, but they all get there eventually, often inexplicably to what was the trigger, the key to which are inside the Google Search APK, so not in the public domain or part of AOSP.
From what I can establish, Google tests the availability of a connection prior to deciding whether to use offline or online recognition. If a connection is available initially but is lost prior to the response, Google will supply a connection error, it won’t fall-back to offline. As a side note, if a request for the network synthesised voice has been made, there is no error supplied it if fails – You get silence.
The Google Search update enabled no additional features in Google Now and in fact if you try to use it with no internet connection, it will error. I mention this as I wondered if the ability would be withdrawn as quietly as it appeared and therefore shouldn't be relied upon in production.
If you intend to start using the SpeechRecognizer class, be warned, there is a pretty major bug associated with it, which require your own implementation to handle.
Not being able to specifically request offline = true, makes controlling this feature impossible without manipulating the data connection. Rubbish. You’ll get hundreds of user emails asking you why you haven’t enabled something so simple!
EDIT: Since API level 23 a new parameter has been added EXTRA_PREFER_OFFLINE which the Google recognition service does appear to adhere to.
Hope the above helps.
I would like to improve the guide that the answer https://stackoverflow.com/a/17674655/2987828 sends to its users, with images. It is the sentence "For those that it doesn't, this is the ‘guide’ I supply them with." that I want to improve.
The user should click on the four buttons highlighted in blue in these images:
Then the user can select any desired languages. When the download is done, he should disconnect from network, and then click on the "microphone" button of the keyboard.
It worked for me (android 4.1.2), then language recognition worked out of the box, without rebooting. I can now dictates instructions to the shell of Terminal Emulator ! And it is twice faster offline than online, on a padfone 2 from ASUS.
These images are licensed under cc by-sa 3.0 with attribution required to stackoverflow.com/a/21329845/2987828 ; you may hence add these images anywhere along with this attribution.
(This the standard policy of all images and texts at stackoverflow.com)
A simple and flexible offline recognition on Android is implemented by CMUSphinx, an open source speech recognition toolkit. It works purely offline, fast and configurable It can listen continuously for keyword, for example.
You can find latest code and tutorial here.
Update in 2019: Time goes fast, CMUSphinx is not that accurate anymore. I recommend to try Kaldi toolkit instead. The demo is here.
In short, I don't have the implementation, but the explanation.
Google did not make offline speech recognition available to third party apps. Offline recognition is only accessable via the keyboard. Ben Randall (the developer of utter!) explains his workaround in an article at Android Police:
I had implemented my own keyboard and was switching between Google
Voice Typing and the users default keyboard with an invisible edit
text field and transparent Activity to get the input. Dirty hack!
This was the only way to do it, as offline Voice Typing could only be
triggered by an IME or a system application (that was my root hack) .
The other type of recognition API … didn't trigger it and just failed
with a server error. … A lot of work wasted for me on the workaround!
But at least I was ready for the implementation...
From Utter! Claims To Be The First Non-IME App To Utilize Offline Voice Recognition In Jelly Bean
I successfully implemented my Speech-Service with offline capabilities by using onPartialResults when offline and onResults when online.
I was dealing with this and I noticed that you need to install the offline package for your Language. My language setting was "Español (Estados Unidos)" but there is not offline package for that language, so when I turned off all network connectivity I was getting an alert from RecognizerIntent saying that can't reach Google, then I change the language to "English (US)" (because I already have the offline package) and launched the RecognizerIntent it just worked out.
Keys: Language setting == Offline Voice Recognizer Package
It is apparently possible to manually install offline voice recognition by downloading the files directly and installing them in the right locations manually. I guess this is just a way to bypass Google hardware requirements.
However, personally I didn't have to reboot or anything, simply changing to UK and back again did it.
Working example is given below,
MyService.class
public class MyService extends Service implements SpeechDelegate, Speech.stopDueToDelay {
public static SpeechDelegate delegate;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
try {
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
((AudioManager) Objects.requireNonNull(
getSystemService(Context.AUDIO_SERVICE))).setStreamMute(AudioManager.STREAM_SYSTEM, true);
}
} catch (Exception e) {
e.printStackTrace();
}
Speech.init(this);
delegate = this;
Speech.getInstance().setListener(this);
if (Speech.getInstance().isListening()) {
Speech.getInstance().stopListening();
} else {
System.setProperty("rx.unsafe-disable", "True");
RxPermissions.getInstance(this).request(permission.RECORD_AUDIO).subscribe(granted -> {
if (granted) { // Always true pre-M
try {
Speech.getInstance().stopTextToSpeech();
Speech.getInstance().startListening(null, this);
} catch (SpeechRecognitionNotAvailable exc) {
//showSpeechNotSupportedDialog();
} catch (GoogleVoiceTypingDisabledException exc) {
//showEnableGoogleVoiceTyping();
}
} else {
Toast.makeText(this, R.string.permission_required, Toast.LENGTH_LONG).show();
}
});
}
return Service.START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
#Override
public void onStartOfSpeech() {
}
#Override
public void onSpeechRmsChanged(float value) {
}
#Override
public void onSpeechPartialResults(List<String> results) {
for (String partial : results) {
Log.d("Result", partial+"");
}
}
#Override
public void onSpeechResult(String result) {
Log.d("Result", result+"");
if (!TextUtils.isEmpty(result)) {
Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onSpecifiedCommandPronounced(String event) {
try {
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
((AudioManager) Objects.requireNonNull(
getSystemService(Context.AUDIO_SERVICE))).setStreamMute(AudioManager.STREAM_SYSTEM, true);
}
} catch (Exception e) {
e.printStackTrace();
}
if (Speech.getInstance().isListening()) {
Speech.getInstance().stopListening();
} else {
RxPermissions.getInstance(this).request(permission.RECORD_AUDIO).subscribe(granted -> {
if (granted) { // Always true pre-M
try {
Speech.getInstance().stopTextToSpeech();
Speech.getInstance().startListening(null, this);
} catch (SpeechRecognitionNotAvailable exc) {
//showSpeechNotSupportedDialog();
} catch (GoogleVoiceTypingDisabledException exc) {
//showEnableGoogleVoiceTyping();
}
} else {
Toast.makeText(this, R.string.permission_required, Toast.LENGTH_LONG).show();
}
});
}
}
#Override
public void onTaskRemoved(Intent rootIntent) {
//Restarting the service if it is removed.
PendingIntent service =
PendingIntent.getService(getApplicationContext(), new Random().nextInt(),
new Intent(getApplicationContext(), MyService.class), PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
super.onTaskRemoved(rootIntent);
}
}
For more details,
https://github.com/sachinvarma/Speech-Recognizer
Hope this will help someone in future.

Categories

Resources