Meteor on android device can't get GPS position - android

I have a mobile application done in Meteor + ionic and I'm trying to get the user GPS location using this simple code:
navigator.geolocation.getCurrentPosition( function ( result ) {
console.log( result );
Session.set( 'pos', result );
}, function ( err ) {
console.log( err );
});
All I get is a warning saying that getCurrentPosition deprecated on insecure locations.
Why is my app seen as an insecure location since I'm running it with meteor run android-device which basically installs it on my phone then runs it like any other app?
If I open up the chrome inspector and control the app's browser I can switch the url from http://meteor.local to https://meteor.local making it a "secure location" but all I get is a timeout error when getCurrentPosition is fired.
So...how does one get the user's position inside a meteor mobile application?

According to the documentation you need to use the cordova geolocation plugin. I believe this is because the webview used by cordova may not have all the features of chrome on your mobile -- which is a natural but wrong assumption.
According to the geolocation plugin readme you need to add the plugin:
meteor add mdg:geolocation.
and then you can use:
Geolocation.currentLocation()
There might be a way to do this all with navigator.geolocation, but if you use the native functionality provided via the plugin then you won't need to worry much about security consideration in browsers. It will also ensure that things will work on older devices that may use a version of the webview that doesn't yet implement the geo API.

I had the same problem when using the navigator.geolocation. It seems like the error function will never be fired because the timeout default value is actually infinity. To solve it is to actually set a minimum timeout value.
navigator.geolocation.getCurrentPosition(success,error,{timeout:5000});

Related

What to do if in react-native DeviceMotion is not available?

In my react-native app I use DeviceMotion from expo-sensors, when running await DeviceMotion.isAvailableAsync() line it returns false.
In any documentation that I read, it's not said what to do in this case. Is it a limitation of the device itself? Or is there a way to enable it? I couldn't find anything in settings. How do I enable it if it is possible?

Capacitor Device Plugin works on ionic serve, but not on my phone when testing with Android Studio

I have an issue with the Device plugin from Capacitor. When using Device.getLanguageCode() I get the correct language code when I am testing the application in the browser with ionic serve. However, when I am testing the application on my own phone with android studio, the language code is not retrieved. I tried to print the language code to the screen, but there is nothing coming up.
I suspect that it may be because the app is just in testing-mode, but it is still installed on the phone, so shouldn't it be able to get the device info? Help is much appreciated!
I am using ionic with react.
Code:
/* --------------------- Setting Language --------------------- */
const { Device } = Plugins;
const languageCtx = useContext(LanguageContext);
let fixedLangCode = 0; // 0 is default language code (English).
Device.getLanguageCode().then((langCode) => {
if (langCode.value.includes("NO")) {
fixedLangCode = 1; //1 is Norwegian language code.
}
languageCtx.updateLanguageCode(fixedLangCode);
});
/* ---------------------------------------------------------- */
Error message:
There does not seem to be any error message. The only thing that is mentioned when starting up the application about the getLanguageCode is this:
V/Capacitor/Plugin: To native (Capacitor plugin): callbackId: 27212778, pluginId: Device, methodName: getLanguageCode
V/Capacitor: callback: 27212778, pluginId: Device, methodName: getLanguageCode, methodData: {}
And the Device plugin is mentioned among all the other plugins like this:
D/Capacitor: Registering plugin: Device
I have found the problem and solution. Turns out there was nothing wrong, it was just a difference between the getLanguageCode.value result in the browser and on the phone.
In the browser, the getLanguageCode.value returns "nb-NO".
In the mobile application, the getLanguageCode.value returns "nb".
Since my program was designed to detect "NO" and not "nb" it seemed like it didn't work.

Geocoder.getFromLocation grpc failed on Android real device

I need to get the city and state from latitude and longitude. Geocoder does this function to get the city and state. But I am getting an error java.io.IOException: grpc failed in below line only on Android real device. It is working fine in emulators.
new Geocoder(context).getFromLocation(latLng.latitude, latLng.longitude, 1).get(0);
Also I have been calling this function using Asynctask which is suggested in another post Geocoder grpc failed in stack overflow but nothing works for me.
And I have restart the device as well but still it is failing.
Help needed here. Thanks in advance.
Note: I tested in Google Pixel XL, Samsung S6 edge, Samsung S4.
It looks like this is ongoing issue that was reported in the Google issue tracker both for real devices and emulators. You can refer to the following bugs:
https://issuetracker.google.com/issues/64418751
https://issuetracker.google.com/issues/64247769
Unfortunately, Google haven't solved these issues yet.
As a workaround you can consider using the Geocoding API web service. Please note that there is a Java client library for web services that you can find on Github:
https://github.com/googlemaps/google-maps-services-java
Using Java client library for web services you can implement reverse geocoding lookup that shouldn't give you the error that you experience with native Android geocoder.
The Javadoc for client library is located at
https://googlemaps.github.io/google-maps-services-java/v0.2.5/javadoc/
I hope this helps!
Check your internet connection .
what is going on is at some point between the search and the results rendering , the connection is either to slow or not working at all . so the default error is no GRCP found. your code needs to handle this type of errors and prevent the crash of the map
Happened for me both with a real and an emulated device. Solved my restarting the app after cleaning the project
For me, it was the device time. I was doing some tests, and I had to change the device time. The device time must be set correctly, otherwise you get this error.
It is a reported bug in Android, check #xomena answer for the link.
Alternately, you can use the web services. Add this to gradle dependencies:
api 'com.google.maps:google-maps-services:0.10.2'
Sample code to get addresses:
val geoApiCtx = GeoApiContext.Builder()
.apiKey(Util.readGoogleApiKey(ctx))
.build()
val addresses = GeocodingApi.reverseGeocode(geoApiCtx, loc).await()
When I run with emulator I got the same problem.
The problem has solved after i run with real device.
Making the call to fetch an address via geocoder is recommended to do via an IntentService....but if you're in rapid development mode and don't want to bother setting up a service just yet, you can use a try/catch block
example in Kotlin:
try{
val addressList = geocoder.getFromLocationName(locationName, 3)
//...
}catch(e: IOException) {
when{
e.message == "grpc failed" -> {/* display a Toast or Snackbar instead*/ }
else -> throw e
}
}
What I did was, I changed my accuracy to GPS only, in this case, the application was not calculating my location. I again changed the accuracy to high accuracy and the application started working, no crash at all. you can change your GPS accuracy from location setting on your android mobile.
Using an emulator. I had to update the Google Play Services on the device and then it worked!

Phonegap Geolocation on Android

I am using Phonegap Build version 3.4.0 with the geolocation plugin version 0.3.7.
My app should display the users location on a map.
This works very well under iOS.
But depending on the location settings in Android I am getting very different results.
If I am trying to get a quick location fix with enableHighAccuracy: false and the user has disabled Wireless Location Access, my error callback gets called with the error code 2. This is what I expected.
But if I am trying to get the best result with enableHighAccuracy: true and the user has disabled GPS Access the error callback does not get called. Instead it just times out.
Is this known behaviour and I have to live with it or is there something I could do?
I am using boilerplate code:
var positionOptions = {
enableHighAccuracy: true,
timeout: 1000*30,
maximumAge: 0
};
function positionSuccess(location) {
console.log(location);
};
function positionError(error) {
console.log(error);
};
navigator.geolocation.getCurrentPosition(
positionSuccess,
positionError,
positionOptions
);
Edit: this is tested on Huawei/Android 4.0.3, Moto G/Android 4.4.2, Samsung GT-I9100/Android 4.1.2, LG ???/Android 4.1.2
The geolocation on Cordova can be very tricky to get working correctly. I'm still occasionally battling with it myself. If you get a timeout make sure the geolocation code is even compatible with your test device by extending the timeout to ~300,000 ms (5 minutes). Yeah, if you're in a region with a bad signal it can take that long. Also try getting your location via wifi. And test on a live device (check out TestDevice). Android simulators are iffy.
If geolocation is crucial to your application, you should alert your users of the problem when you get each of these error codes. Tell your user that they need to enable one form of access or another and how to go about doing that.
Also, you can consider running getCurrentPosition where enableHighAccuracy: false, and if that fails run getCurrentPosition again but this time set enableHighAccuracy: true. Only after both methods have timed out, prompt the user with an error.
By the way, in my experience while on an Android, Cordova is usually able to lock onto a location within 2-3 seconds when using 3/4G in an urban environment. If not by then it pretty much never locks on. Good luck!

geolocation.watchposition not working with network based location off

I am building a location aware app for Android using sencha 2.3.1 + phonegap 3.3.0, and use geolocation to show a list of destinations ordered by proximity to the user location.
Everything works fine if the setting for network-based location is on. But if I turn it off and leave gps only based location then it never gets detected.
The code is very basic, I tried both the sencha path:
Ext.device.Geolocation.watchPosition({
frequency: 10000,
callback: function(position) {
AppHelper.setCurrentLocation(position);//yeah!!
},
failure: function() {
AppHelper.setLocationModeOff();//oouch!
}
});
and the phonegap path:
var success = function(position) {
AppHelper.setCurrentLocation(position);
};
var fail = function() {
AppHelper.setLocationModeOff();
};
navigator.geolocation.watchPosition(success, fail,{ timeout: 30000 });
Result is the same, I tried waiting for a couple minutes for the geolock to happen but no sign of location service.
The phonegap location plugin is installed, the permissions are correct, and everything works if network location is enabled.
I am testing on a Sony Tablet S.
I am aware of this bug:
How to get user position by GPS in Sencha
But I checked the code and its is definetely fixed in sencha 2.3.1
I wil try on other devices but I was wondering if anybody experienced the same.
TIA.
EDIT:
I made an experiment using standard html5 code on my android device using systems default browser (webkit based).
I would access my settings page and change settings at each run.
Accessing http://html5demos.com/geo I get the following:
I started the device with network geolocation on and gps based location off:
I get a message that asks if I want to share my location I say yes, only this time everything works fine.
Turn off network based geolocation, gps still off:
Location detection fails. (as expected)
Turn on GPS, leave network geolocation off:
Geolocation fails silently, I don't get a request to use my location and subsequent activation of GPS based gelocation.
So my conclusion is: this is similar to what I noticed in phonegap. If network based geolocation is on everything works as expected. But if I have no network connection and gps is on that is pretty useless in my html5 app.
At least this is what happens on my device (sony tablet)
From my understanding of HTML5 geolocation API, that is expected behaviour, you just can't control if under the hood it is using gps or network location.
What's probably happening is that watchPosition(), when it is called, tries first a raw, quick positioning using network, then goes with the gps positioning to give you a better result (assuming you called it with the enableHighAccuracy parameter).
If you are disabling network positioning it probably gets stuck at the first point: as stated in cordova/phonegap's docs the permission to access coarse location is a requirement for geolocation to work.
(in app/AndroidManifest.xml)
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I am guessing that revoking access to coarse location from the device settings somehow "overrides" the user permission given when installing the app.

Categories

Resources