views:

254

answers:

3

I am experimenting with making Blackberry widgets but having a little trouble.

My first trial involves displaying a button which, when clicked, calls a JavaScript function that should alert the phones latitude and longitude.

The function looks:

function whereAmI() {
var latitude = blackberry.location.latitude;
var longitude = blackberry.location.longitude;
alert("Lat: "+latitude+", Long: "+longitude);
}

But it only ever alerts "Lat: 0, Long: 0". I've checked and my GPS seems to be working ok.

I'm running OS 5.* on a Curve 8900.

Any help would be appreciated :)

A: 

Does your widget have permission to use GPS? Go to Options->Applications, select your app, then "Edit Permissions". Make sure "Location Data" (in Connections) is set to Allow.

Marc Novakowski
Hi, Marc. Thanks for the suggestion. I had a look and the application definitely has access to the location data, and nearly everything else, yet it still returns 0.
+1  A: 

I discovered that I wasn't signing my files properly - now that I have, everything works fine.

For kaban:

      // called when location object changes
  function locationCB()
  {
     alert("Latitude "  + blackberry.location.latitude);
     alert("Longitude " + blackberry.location.longitude);
     return true;
 }
 // test to see if the blackberry location API is supported
 if( window.blackberry && blackberry.location.GPSSupported)
 {
       document.write("GPS Supported");

       // Set our call back function
       blackberry.location.onLocationUpdate("locationCB()");

       // set to Autonomous mode
       blackberry.location.setAidMode(2);

       //refresh the location
       blackberry.location.refreshLocation();
 }
 else
 {
   document.write("This Device doesn't support the Blackberry Location API");
 }
sadly i wish you had provided more on what 'signing my files properly' meant, so this could have helped others...
Frederico
Good point. When you register with RIM to become a developer of apps or widgets, you get a code signing key which marks your apps with a unique code so that in the event of malicious use, the developer can be traced. There are a number of ways of signing your files, and I wasn't doing it properly. I can provide specifics if anyone else is interested.
If you have any links it would be helpful for others in the future! Thanks for the response :)
Frederico
A: 
kaban
kaban: I posted my problem on the RIM developer forum and was advised that the latitude and longitude only become available when they change, which tends to happen every few seconds. If you try to access them immediately in a script it will fail. You need to request them in an onchange handler. See the below post, where I will add this in an edit.