i am writing my first android application with Phonegap. My problem is
very strange:
When i call a javascript function in script, it doesn't trigger the
GPS. But when i put the same function on a link, it works fine. Here
is my js code
function Ag(){
var _self = this;
this.init = function(){
this.watchPosition();
}
this.watchPosition = function(){
navigator.geolocation.getCurrentPosition(this.onGpsReceive,
this.onGpsFail);
}
this.onGpsReceive = function(loc){
alert("lat"+loc.coords.latitude);
$("#location").html(loc.coords.latitude + " - " +
loc.coords.longitude);
}
this.onGpsFail = function(){
$("#location").html("Error getting coordinates");
}
this.init();
}
I've tried it in onready block, deviceready block and also .bind
("deviceready", function(){
after defining a function in script tag
function startApp(){
var a = new Ag();
}
and calling in body onload event worked but it never calls success or
fail functions and the GPS receiver is not triggered. What am i doing
wrong? Thanks in advance.
As described in other thread(s) (http://groups.google.com/group/
phonegap/t/b79408f990426bb7), we are aware of problems existing with
PhoneGap Android 2.0+, but we are having a hard time tackling the
problem since us Canadians don't get Android 2.0+ devices for at least
another week or so (at least in B.C. - Telus is releasing the Motorola
Milestone soon so that will be our first one).
If you are triggering a getCurrentPosition call in the onload event,
the phonegap.js file may not be entirely loaded so it may not work.
You say you've tried to attach the call to the deviceready event with
no success?
Could you also clarify what you mean by putting the call in a link? Do
you mean having an HTML element like this in your phonegap page: <a
href="javascript:navigator.geolocation.getCurrentPosition
(win,fail);">Hi</a> ? If so, this again may be related to the timing
of phonegap loading itself and using the onload event to trigger a
call to the GPS, since by the time you click on a link the phonegap.js
file is likely loaded properly and gps calls work, but onload it may
not be ready yet.
I think you are simply having a timing issue with respect to all of
PhoneGap's JavaScript loading, as per my post above. A simple test
would be to wrap your initial onload script in a slightly-delayed
timeout:
function startApp(){
setTimeout('var a = new Ag(); ',100);
}
... and then I am assuming you are using the JavaScript variable 'a'
somewhere in your code?
Either try the timeout, or try attaching your initialization function
to the 'deviceready' event.
On Feb 1, 1:05 pm, eyurdakul <ejder.yurda...@gmail.com> wrote:
> ok, sorry for the missing info. I am using android 1.5 google apis
> level3 sdk and debugging on my HTC hero (android 1.5)
this is my setupApp function which just creates an instance of Ag
function setupApp(){
augmentedReality = new Ag();
};
if i put it in a link
<a href="#" onclick="setupApp()">start</a>
it runs till getLocation but success callback never runs (error
neither). I had also no success with deviceready event
Cheers
Mark.
$(document).deviceready(function() {
function Ag(){
var _self = this;
this.init = function(){
this.watchPosition();
}
this.watchPosition = function(){
navigator.geolocation.watchPosition(this.onGpsReceive,
this.onGpsFail);
}
this.onGpsReceive = function(loc){
$("#location").html(loc.coords.latitude + " - " +
loc.coords.longitude);
}
this.onGpsFail = function(){
$("#location").html("Error getting coordinates");
}
this.init();
}
Ag.prototype = {
position: {
lat: null,
lon: null,
alt: null
}
};
new Ag();
});
i am acctually not goint to use the instance twice in my code. So
theorically, it should work.
BTW, i've added some extra java code in phonegap but they seem to work
as expected. There is one more WebView instance in the main instance
appView = new WebView(this.getApplicationContext());
agView = (WebView) findViewById(R.id.agView);
i've added a new class and put an instance in the third view which is
cv
agView.addView(cv);
agView.addView(appView);
so agView is the content view. Would that change the functionality of
Phonegap somehow?
I think part of the problem is that the jQuery code you're using isn't
compatible with the 'deviceready' event that is PhoneGap-specific. In
this line of code:
$(document).deviceready(function() {
the problem is I don't think the jQuery object returned by the $
function has a deviceready function handle on it. How about using the
standard way of attaching the event, like so?
document.addEventListener('deviceready', setupApp, false);
ALTERNATIVELY, if you're not feeling the event love, take all the code
you have in your current deviceready function other than calling a new
instance of AG, put it in the global scope, and then wrap the setupApp
function in a timer in body onload:
<head>
...
<script>
function Ag(){
var _self = this;
this.init = function(){
this.watchPosition();
}
this.watchPosition = function(){
navigator.geolocation.watchPosition
(this.onGpsReceive,
this.onGpsFail);
}
this.onGpsReceive = function(loc){
$("#location").html(loc.coords.latitude + " - " +
loc.coords.longitude);
}
this.onGpsFail = function(){
$("#location").html("Error getting coordinates");
}
this.init();
}
Ag.prototype = {
position: {
lat: null,
lon: null,
alt: null
}
};
function setupApp(){
augmentedReality = new Ag();
};
</script>
</head>
<body onload="setTimeout('setupApp();',250);">
...
Let me know how it goes!
1. function startApp(){
alert("test");
//new Ag();
};
</script>
</head>
<body onload="setTimeOut('startApp();', 2000);">
this doesn't work. even no alert. Only way to trigger that function is
still <a onclick="startApp()" href="#">start</a>
2. navigator.geolocation.watchPosition(suc, fail); doesn't call the
callbacks. The phonegap.js seems to be right, so it should work. I've
defined this function again in my class:
this.watchPosition = function(){
this.postMsg("gps is fixed");
setInterval("instance.getLocation(instance.onGpsReceive,
instance.onGpsFail)", 10000, this);
};
(getLocation is a shorthand of navigator.geolocation.getCurrentPosition
())
I load the camerapreview first on a surface and overlay another
transparent webview on it which is the phonegap layer so the camera
preview is visible under the html. Would this process use too much
resources so the webkit layer can't load. There is a significant delay
between the camerapreview and loading html (0.5-1 sec).
I am using Phonegap 0.8 on Windows7 x64 and compiled the project on
linux with the make file and mailed to my windows env. would that make
any difference since the makefile doesn't do anything more than
generating the latest javascript framework. I could try the edge
version too but it is an android 2.0 project and we don't have android
2.0 yet here in Belgium too :). Greetings, Ejder.
You coudl create an interval loop that would pool the typeof object
until it's initialized and fire the app initialization only after it
fully loaded - we had this problem with the network object when we
wanted to test if there is network connection at app start. Though it
was all on an iPhone app - although I guess it might be the same
issue.
regards
Tom
function allLoaded(){
if(jQuery && typeof(navigator.geolocation) != "undefined"){
new Ag();
clearInterval(intval);
}
}
var intval = setInterval("allLoaded();", 1000);
Now everything starts and works fine.
Although you can switch it to 1ms interval from what I've figured out
it usually took few ms to load up everything - although few ms enough
to crash the app :)
Tom
Thanks for posting about this, we're going to have to look into these
timing issues and fix it with a properly-dispatched deviceready event.
I'll post it on our issue tracker at lighthouseapp; here's the link in
case you guys aren't aware that it's there!
http://phonegap.lighthouseapp.com