Geolocation doesn't stop in Phonegap - javascript

When I run
window.onload = function () {
document.addEventListener("deviceready", getGeolocation);
}
function getGeolocation() {
navigator.geolocation.getCurrentPosition( successCallback, errorCallback, {maximumAge: 0});
}
or
function getGeolocation() {
watchGeoMarkerProcess = navigator.geolocation.watchPosition(updateCallback, errorCallback);
}
and then
function updateCallback(position) {
if (position.coords.accuracy < 100) {
navigator.geolocation.clearWatch(watchGeoMarkerProcess);
}
}
in my app on iOS 5 using phonegap it seems to get stuck, since the geolocation indicator-icon stays in the top bar and it never goes away, which I take to mean that the GPS doesn' get turned off. Also, sometimes I don't get any coords at all, throwing a time-out error.
I don't think there is anything wrong with the code since it works just fine as a webapp.
Any ideas?

navigator._geo is the 'real' implementation I believe. I've seen recent git commits where they are trying to over-ride navigator.geolocation but apparently failing on iOS. Looking at the phonegap source code gave me the idea to try the real call instead.
Here is the git commit:
http://mail-archives.apache.org/mod_mbox/incubator-callback-commits/201203.mbox/%3C20120307000809.B82AA5D82#tyr.zones.apache.org%3E
Here is another thread on the problem:
https://groups.google.com/forum/?fromgroups#!topic/phonegap/W32yYpV28W8
UPDATE: I have some measure of success now:
Edit your phonegap.js, comment out lines 3451-3453 which look like this:
__proxyObj(navigator.geolocation, navigator._geo,...
You will get an ugly permission alert.. but the location should work. The reasoning behind this change is that you will now use safari's location detection, not PhoneGaps.
UPDATE2: ..and the problem with PhoneGap turned out to be a conflict with another javascript library, in this case dragdealer.js. So double check for any suspicious variable names like "Location" or "Position" in any other javascript you are using. For some reason this conflict was not a problem on platforms other than iOS.

For what it's worth I have a same problem and these fixes did not work. But they may for you:
make sure you get the location after onDeviceReady() has been called
try using navigator._geo.getCurrentPosition

I had the same problem, although on Android.
Adding the enableHighAccuracy option caused it to start working:
navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true} );

Related

NightwatchJS method .resizeWindow not working

A while back, I had an issue using the .maximizeWindow() from NightwatchJS. I was able to resolve this issue by using the following:
"chromeOptions" : {
"args" : ["start-maximized"]
},
However, this was more a work around than a fix. Currently, I need to resize the window, not just maximize it, and want to use the .resizeWindow() method. I also need this to work in browsers other than Chrome, so the above fix is inadequate. Below is my current setup in a test I am writing. This code worked fine in the past and just recently I noticed that the window was no longer resizing correctly. Any thoughts on why it might be failing? Note: this is written in Coffeescript, not JS.
module.exports = {
"Geoprocessing Commands and Assert Tests": (browser) ->
browser
.launchAs "anonymous"
.assert.title "<app name>"
.iFrameReady()
.frame 0, () ->
browser
.resizeWindow 1800, 1000
.pageDisplayed()
.pause 1000
...
.frameParent()
browser.end()
}

Uncaught TypeError: $player.jPlayer is not a function

I'm helping a friend with his site and after updating his WordPress installation to address the recent security issue, the JPlayer plugin that was handling audio on his site stopped working.
Chrome's console shows the error in the title, but I don't know JS well enough to be able to debug it properly. I'm pretty sure that the plugin itself is loaded correctly, along with JQuery, in the page header. I checked it against the plugin's instructions and it all appears fine.
I've also updated the plugin itself to ensure that it's not some compatibility issue.
I did not build his site, nor am I familiar with this particular plugin at all, I'm just trying to see if it's an easy fix or if I have to restore a backup.
I assume it has something to do with how his web designer (they had a falling out) implemented it in the main.js file, but that's about as far as I've gotten.
Help?
Really condensing and removing parts of main.js, it looks like
var $player = false,
$(document).ready(function() {
if(!$player) {
$("#jPlayer").jPlayer({
ready: function() {
$player = $(this); // IT'S BEING SET HERE !
PlaylistPlay(playlistObject,trackIndex);
}
});
} else {
PlaylistPlay(playlistObject,trackIndex);
}
});
function PlaylistPlay(lePID,trackIndex) {
playTrack(trackIndex);
}
function playTrack(index) {
$player.jPlayer("setMedia", {mp3: trackObject.mp3,oga: trackObject.oga}).jPlayer("play");
}
If you look closely at that, you'll see that there is a distinct possibility that PlaylistPlay can be called without $player being set to $(this), it's actually almost a certaintity, which means that $player is false, and doing
false.jPlayer(...
doesn't really work, see the console output that confirms the variable is false
The plugin is not initializing correctly. On $(document).ready() it's trying to initialize the plugin and it's reporting a Flash error.
Here's the significant part of the code:
$("#jPlayer").jPlayer({
...
error: function(event) {
var out = "<p id=\"noSolution\">Sorry, you need an HTML5 capable browser or flash to be able to listen to music on this website.<br /> Reason: ";
switch(event.jPlayer.error.type) {
case $.jPlayer.error.FLASH:
out += "A problem with the Flash insertion on the page.";
break;
...
}
}
...
});
Digging a bit deeper, I can trace this back to the vimeo.jplayer in the specific code block:
_flash_volume: function(a) {
try {
this._getMovie().fl_volume(a)
} catch (b) {
this._flashError(b)
}
}
That function is throwing an exception because this._getMovie() does not have a property named fl_volume.
The error you actually see is a side-effect of this failure. You could try removing the line: this._flashError(b) from the above statement and see if the error can be safely ignored.

AJAX code insert not firing *sometimes*

I'm trying to modify an ajax callback without actually having access to the original code (don't ask...), and I had found a similar version of the below code on Stack Overflow, which I thought worked great! I put it onto my site, tested it in all browsers, even tested on mobile with success.
So I deployed it this week and, lo and behold, I discovered that for somewhere between a quarter and a third of users this isn't firing correctly. I've been trying like hell to replicate the problem, but again, all of my testing has been successful.
My analytics have told me that the problem is existing for IE, Firefox, and Chrome, and the most up-to-date versions of each, with no tendency toward one browser or another. And from the nature of the problem, I know that the original ajax callback is functioning correctly; it's just my new extra code that's broken.
The site specific code I've written shouldn't be posted online, and stackoverflow isn't jsLint, so I've just replaced it with "some code" here. Suffice to say, I've pored over it looking for potential errors, but I was wondering if anyone knew of any reason why the below code would not work in some but not all cases. Because the below is the only code I'm not sure about.
function init(){
"use strict";
var send = window.XMLHttpRequest.prototype.send,
onReadyStateChange;
function sendReplacement(data) {
if(this.onreadystatechange) {
this._onreadystatechange = this.onreadystatechange;
}
this.onreadystatechange = onReadyStateChangeReplacement;
return send.apply(this, arguments);
}
function onReadyStateChangeReplacement() {
if(this._onreadystatechange) {
var end = this._onreadystatechange.apply(this, arguments);
}
some code
if (this._onreadystatechange){return end}
}
window.XMLHttpRequest.prototype.send = sendReplacement;
}

notification.confirm callback not firing phonegap iOS

I'm using Phonegap (Cordova) v1.9, and this app is solely for iPhone4+ use.
I've tried the two methods below, on their own and simultaneously.
Phonegap's deviceready event has been fired previous to this code.
I'm using the iPhone configuration utility to watch the console, but I'm not getting anything from the code below.
Also, I'm using Sencha Touch (v1.1) and jQuery. No conflicts noticed throughout the app.
Please help?
function onConfirmCallback(button){
console.log("onConfirmCallback");
console.log(button);
}
var toConfirm = navigator.notification.confirm("You don't have a pincode set.\nWould you like to set one now?", onConfirmCallback, "Pincode", "Yes,No");
toConfirm.onAlertDismissed = function(index, label){
console.log("onAlertDismissed");
console.log(index);
}
Have you tried looking for exceptions to see what might be happening?
try
{
var toConfirm = navigator.notification.confirm("You don't have a pincode set.\nWould you like to set one now?", onConfirmCallback, "Pincode", "Yes,No");
toConfirm.onAlertDismissed = function(index, label) {
console.log("onAlertDismissed");
console.log(index);
}
}
catch (ex)
{
console.log(ex.message);
}
Edit:
The example mentioned in the comment below (from https://groups.google.com/group/phonegap/browse_thread/thread/126f56f4e296e555) is for PhoneGap 0.9.2. I have not seen any examples for v1.3 where the confirm function returns an object as your using with 'toConfirm'. Maybe that is causing your problem. Have you tried the following (removing all other code to do with toConfirm to simplify the problem)?
navigator.notification.confirm("You don't have a pincode set.\nWould you like to set one now?", onConfirmCallback, "Pincode", "Yes,No");
http://docs.phonegap.com/en/1.3.0/phonegap_notification_notification.md.html#notification.confirm
Also, is there any reason your not using a newer version of PhoneGap (Cordova)?

setTimeout not called in android webkit

On my android phone (2.1) I'm seeing a strange behavior with setTimeout when keeping a finger pressed on the touchscreen for a while.
This very simple code in fact works fine (1 call each second), until I scroll for a while the window (2-3 seconds are sufficient), when it stops being called
$(document).ready(function(){
spam();
});
function spam(){
console.log("cia")
setTimeout(spam, 1000);
}
I has the same Problem.
The solution was for me to define the called function as a variable, than passing ist as parameter to the setTimeout.
Try this:
var spam = function(){
console.log("cia")
setTimeout(spam, 1000);
}
$(document).ready(function(){
spam();
});
I had this issue before on my device when doing some development but neither of these solutions worked for me.
From the reading I did it's reasonably well documented that this does happens but seems to be no consistent way of resolving it.
What worked for me was closing the window I had my test site up down, clearing the cache, exiting the browser then opening task manager and shutting down the process. When I opened my browser again and went to my test site the standard code I had originally started working again.
My only guess is that the browser itself get's itself into some weird state where it doesn't run standard inbuilt browser functions (neither setTimeout() or setInterval() worked for me but both the javascript functions did exist).
I was testing with a Samsung Galaxy S running Android 2.1, I don't know if this will help anyone else but it's what worked for me.
try this
function spam(){
console.log("cia")
setTimeout("spam()", 1000);
}
setTimeout:
/**
#param {String|Function} vCode
#param {Number} iMillis
#return Number
*/
window.setTimeout = function(vCode,iMillis) {};
For me Varriotts answer didn't work ... the only way I could get setTimeout working on the Android phone i used for testing (running v 2.something) is by the following notation:
function foo() {}
window.setTimeout(foo, 200);
This looks weird, passing just the name of a function, but after hours of trying around, it was the only way it worked.
I tried this and it solved my problem.
setTimout(function(){aFunction(text);}, 200);

Categories

Resources