I've tried the following in a number of 2.x android devices with phonegap version 2.0.0:
var onSuccess = function(position){
console.log("Success");
}
var onError = function(error){
console.log("Error");
}
gpsTrackWatchID = navigator.geolocation.watchPosition(
onSuccess,
onError,
{
enableHighAccuracy: true,
maximumAge: 5000,
timeout: 10000,
}
);
If I use the device indoors where there is no GPS signal onError is never called. The documentation states:
If the geolocationSuccess callback is not invoked within this time,
the geolocationError callback will be invoked with a
PositionError.TIMEOUT error code.
I've tried debugging this by putting a console.log in the watchPosition function in cordova.js, but the function seems never to be getting called. I wonder if the browser's native function is being executed instead of phonegap's?
First thing First, Do You set persmission in AndroidManifest correctly? Could You post the file here - especially the persission section?
Secondly, do You use Android 2.3.3 - if Yes, then You must use SDK with Google Maps APi as in normal API GPS location provider don't work.
Ant the last, do You have any JS errors on console, when registering for GPS location?
Related
I made an app that tracks users location now i need to get the speed also. I manage to put position.coords.speed but it always shows 0. I noticed that the gps tracker/icon is not actively tracking. What is the best practice on tracking the speed of the user? I am using the plugin cordova-plugin-geolocation
Have you requested high accuracy in the options when requesting location? For example:
navigator.geolocation.watchPosition(onSuccess, onError, {
enableHighAccuracy: true,
timeout: 30000,
maxAge: 0
});
If position.coords.speed is zero and the gps/location icon is not shown in the taskbar, this might be the cause.
I am currently working on an app using react native that requires location services to be on for some of its features. I have been researching if there is a simple way to test if the user has them turned on or not so I can display a different view if they aren't. I have not found any results other than ones that are specific to IOS or Android. Has anyone else found a solution to this?
Use the "react native android location services dialog box" module for android
https://www.npmjs.com/package/react-native-android-location-services-dialog-box
install
npm install react-native-android-location-services-dialog-box --save
js
import LocationServicesDialogBox from "react-native-android-location-services-dialog-box";
LocationServicesDialogBox.checkLocationServicesIsEnabled({
message: "Use Location ?",
ok: "YES",
cancel: "NO"
}).then(function(success) {
console.log(success); // success => "enabled"
).catch((error) => {
console.log(error.message); // error.message => "disabled"
});
Do a watchPosition or getCurrentPosition, Your second argument is a callback in case of failure, and its argument is the reason it failed.
So, from there you know when and why it fails. You'll need an if for the reason if you only want to act on 'location disabled' and not 'request timed out' for example.
You can use the callback to add a flag to your state for example.
You can also try out these 2 libraries:
https://github.com/c19354837/react-native-system-setting
https://github.com/efkan/react-native-system-settings
What I have is the following:
I get this error code 1 or error message: 'No location provider available.' when my phone location service is turned off.
Other time, when it times out, I get this error code 3 or error message: 'Location request timed out.'.
Thus, based on the error code and message, I display different toasts to inform the user.
You can refer to this position error doc on MDN
I was able to solve this by displaying an error message when the location could not be retrieved.
(error) => {
this.setState({servicesOn: false}),
console.log(error);
},
Geo-location watch only fires every minute. Using a Nexus-5, android 4.4.2, and cordova 3.3.1 (I've also tried 3.4.0). It makes no difference what I define maximumAge as. The location returned is correct. In other devices I have have tested it gets fired every second. I know I can fall back on using setTimeout with getCurrentPosition, but I'd like to understand this behaviour.
navigator.geolocation.watchPosition(
function(){
console.log("success");
},
function(){
console.log("fail");
},
{
enableHighAccuracy: true,
timeout: 30000
}
);
The Geolocation Plugin is not that great, and will actually be deprecated in the next Cordova release. They suggest that you just use the HTML5 Geolocation API instead, which uses the same syntax.
i'm trying to get the geolocation on Android Browser but nothing happens. I'm using a Samsung Galaxy S3 but i'm not sure about the version of my browser. Android version is 4.1.2
Here is my Code:
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
this is a code i copied and pasted from this site
it gives me the "navigator.geolocation"
but when it comes to "getCurrentPosition" my code stops working. Mobile Chrome works fine but this is not. I shared my position but still nothing happens. Any help will be appriciated.
Thanks.
Thanks everyone i found the solution,
i was getting the geolocation after some javascript operations. I tried to get the geolocation before document is ready. And it worked.
I know this is a bit old but it keeps coming up on searches so I thought I would add a tip that helped me.
Because I want to get the location right as the page loads I found that I needed to introduce a very short delay after the page loads. When I had no delay, I would get no error but also I would not activate the location protocols on the phone. This half second delay solved the issue. You can play with the delay and see if it solves your issues.
setTimeout(function() {getAutoLocation(true)},500);
I get the location in my "getAutoLocation(true)" function. This setTimeout only exists to introduce the delay.
seems like PhoneGap has a problem with geolocation
I have the same issue
I'm using S3 with Android 4.1.2, phonegap geolocation feature doesn't work
In order to get the geolocation without errors, you have to make that code block work before using the values provided by the geolocation because operations are carried out asynchronously, in this question i found the solution by loading my geolocation script before other .js files. This solved my problem and another trick for this issue is, geolocation works more stable when you give "always" permission for browser to read your location. After loading for the first time, you never encounter geolocation errors.
I found that some Android phones (old and new) don't run properly the function
getCurrentPosition, maybe trying to save some battery.
I played with the function watchPosition and then the high accuracy GPS kicked in.
Read this to know how to use the parameters properly:
http://dev.w3.org/geo/api/spec-source.html#watch-position
In my case, this worked:
{
maximumAge: 0, timeout: 2000, enableHighAccuracy: true
}
Hope this helps someone.
Did you give Internet permission in manifest?
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
I have created one app which locates the GeoLocation coordinates on iPad/iPhone but fails to do so on some of Android device even when the device is GPS enabled.It works on some of the Android Device(e.g Samsung Galaxy tab SCH-I800) and fails on some other(e.g Samsung Galaxy tab GT-P5113).
Any idea what could be the possible reason for the issue?
Sample code snippet:
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"> </script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
function onDeviceReady()
{
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
var onSuccess = function(position) {
pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
lat=position.coords.latitude;
lng=position.coords.longitude;
alert("lat: "+lat+" long "+lng); //i should get the lat/long here which i need to use further in my code
}
for devices where the app is not working it's going to onError function.
function onError(error) {
alert("error code is: "+error.code);//it gives error code 3 when on such devices
}
You need to set the "enableHighAccuracy" option when setting up the watch to use GPS on those Android devices:
navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true });
See this answer also: Phonegap Android and GPS satellite
Your app doesn't work because of this error:
PositionError.TIMEOUT
Returned when the device was unable to retrieve a position within the time specified in the geolocationOptions' timeout property. When using in conjunction with geolocation.watchPosition, this error could be called into the geolocationError callback every timeout milliseconds.
Set the "enableHighAccuracy" option to false and then if you get code 2 error ( u might get sometimes ) , just
Go to
Menu--->Settings---->Location & Security
and then check
Use Wireless Network.
This has solved my problem and hope it solves yours as well...!!
I had this problem with one if my projects and my customer was adamant that he could go into maps and it would find Hus geolocation almost immediately whereas in my app it took forever or didn't work.
The solution was to go back in time on the plugins git repository and find the java files that were removed, and use that version.
What has happened is the plugin maintainers have just used the native browser geolocation , essentially not using a plugin at all, but in some phones it's just not up to scratch. An alternative could ve to use crosswalk in your app.
I know its not a "good" solution, but its what was necessary to get an acceptable performance.