BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 06-03-2008, 11:14 AM   #1
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default Location api

Please Login to Remove!

I've been trying to develop a custom application that finds a user's coordinates and displays them. i'm later going to add more functionally to this program but frist want to get the coordinates working. I always receive a timed out while waiting to get location. I've change the timeout time serveral times and still recieve this message. please help!!! below is my code:
thanks

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.io.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import javax.microedition.io.*;
import java.util.*;
import java.io.*;
import javax.microedition.location.*;



class GPSDemo extends net.rim.device.api.ui.UiApplication
{
public static void main(String[] args)
{
GPSDemo instance = new GPSDemo();
instance.enterEventDispatcher();
}
public GPSDemo()
{
pushScreen(new G());
}
}

class G extends MainScreen
{
public G()
{

super();
// Set criteria for selecting a location provider:
Criteria cr= new Criteria();
cr.setCostAllowed(true);
cr.setPreferredResponseTime(60);
cr.setHorizontalAccuracy(5000);
cr.setVerticalAccuracy(5000);
cr.setAltitudeRequired(true);
cr.isSpeedAndCourseRequired();
cr.isAddressInfoRequired();
add(new RichTextField("Getting Coordinates...."));

try{
LocationProvider lp = LocationProvider.getInstance(cr);
add(new RichTextField("I'm in try statement"));
Location l = lp.getLocation(120); //always times out
Coordinates c = l.getQualifiedCoordinates();
// Coordinates c = l.getQualifiedCoordinates();
double longitude = 0;
double latitude = 0;
long timestamp = l.getTimestamp();
if(c != null )
{
// Use coordinate information
latitude = c.getLatitude();
longitude = c.getLongitude();
add(new RichTextField("Went inside if statement"));
}
//System.out.println("Lon" + longitude + " Lat "+ latitude + " course "+course+" speed "+speed+" timestamp "+timestamp);
}
catch(LocationException le)
{
add(new RichTextField("Location exception "+le));
}
catch(InterruptedException ie)
{
add(new RichTextField("Interrupted exception "+ie));
}
Offline  
Old 06-03-2008, 11:22 AM   #2
John Clark
BBF Moderator
 
John Clark's Avatar
 
Join Date: Jun 2005
Model: Z30
OS: 10.2.1.x
PIN: s & needles
Carrier: AT&T
Posts: 34,720
Default

Moved to Developer Forum
Offline  
Old 06-03-2008, 11:59 AM   #3
jwhanes
Knows Where the Search Button Is
 
Join Date: Mar 2008
Model: 8800
PIN: N/A
Carrier: cingular
Posts: 16
Default

Take a look at the LocationListener class. You should add a LocationListener to your LocationProvider object. Something like.


Criteria c = new Criteria();
try {
LocationProvider lp = LocationProvider.getInstance(c);
if( lp!=null ){
lp.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
}
} catch (LocationException le) {
;
}

And in your LocationListener implementation......

private class LocationListenerImpl implements LocationListener {
public void locationUpdated(LocationProvider provider, Location location) {
if(location.isValid()) {
heading = location.getCourse();
longitude = location.getQualifiedCoordinates().getLongitude();
latitude = location.getQualifiedCoordinates().getLatitude();
altitude = location.getQualifiedCoordinates().getAltitude();
speed = location.getSpeed();
}
}

public void providerStateChanged(LocationProvider provider, int newState) {
//no-op
}
}

In the above example the gps variables (heading, long, lat, etc.) get updated whenever the locationUpdated method gets called. This method gets called whenever the GPS receives some data.

So rather than immediately asking for data, (which was probably timing out, simply because there was no data) you have the GPS device tell you when it has data and keep those variables updated.

Hope this helps.
Offline  
Old 06-03-2008, 12:54 PM   #4
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default

Thanks for the reply but I get the error illegal start of expression when I put the private class LocationListenerImpl... in my code. what am i doing wrong.
Offline  
Old 06-03-2008, 01:15 PM   #5
jwhanes
Knows Where the Search Button Is
 
Join Date: Mar 2008
Model: 8800
PIN: N/A
Carrier: cingular
Posts: 16
Default

Well that could be a variety of things. Doesn't sound like its related to the Location/GPS stuff though. Just a syntax error.

Its important to note that the LocationListenerImpl is an internal CLASS. So make sure your brackets and such are all in order.
Offline  
Old 06-03-2008, 01:20 PM   #6
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default

This is the way I did it
Thanks so much

class G extends MainScreen
{
public G()
{

super();
// Set criteria for selecting a location provider:
Criteria cr= new Criteria();
cr.setCostAllowed(true);
cr.setPreferredResponseTime(60);
cr.setHorizontalAccuracy(5000);
cr.setVerticalAccuracy(5000);
cr.setAltitudeRequired(true);
cr.isSpeedAndCourseRequired();
cr.isAddressInfoRequired();
add(new RichTextField("Getting Coordinates...."));

try{
LocationProvider lp = LocationProvider.getInstance(cr);
if( lp!=null ){
lp.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
}
add(new RichTextField("I'm in try statement"));
Location l = lp.getLocation(120); //always times out
Coordinates c = l.getQualifiedCoordinates();
double longitude = 0;
double latitude = 0;
long timestamp = l.getTimestamp();
if(c != null )
{
// Use coordinate information
latitude = c.getLatitude();
longitude = c.getLongitude();
add(new RichTextField("Went inside if statement"));
}
add(new RichTextField("other side"));
//System.out.println("Lon" + longitude + " Lat "+ latitude + " course "+course+" speed "+speed+" timestamp "+timestamp);
}
catch(LocationException le)
{
add(new RichTextField("Location exception "+le));
}
catch(InterruptedException ie)
{
add(new RichTextField("Interrupted exception "+ie));
}



private class LocationListenerImpl implements LocationListener {
public void locationUpdated(LocationProvider provider, Location location) {
if(location.isValid()) {
longitude = location.getQualifiedCoordinates().getLongitude();
latitude = location.getQualifiedCoordinates().getLatitude();
altitude = location.getQualifiedCoordinates().getAltitude();
speed = location.getSpeed(); }
}
}

}
}
Offline  
Old 06-03-2008, 01:33 PM   #7
jwhanes
Knows Where the Search Button Is
 
Join Date: Mar 2008
Model: 8800
PIN: N/A
Carrier: cingular
Posts: 16
Default

It looks like the LocationListenerImpl class is inside the constructor of your 'G' class. It needs to be outside.
Offline  
Old 06-03-2008, 01:36 PM   #8
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default

i did that because when i put it outside the constructor i get error modifier private not allowed here
Offline  
Old 06-03-2008, 01:56 PM   #9
jwhanes
Knows Where the Search Button Is
 
Join Date: Mar 2008
Model: 8800
PIN: N/A
Carrier: cingular
Posts: 16
Default

Well here is a short example of how I use it. Most of the code has been removed, but the basics are there.

Code:
package com.mwa.gps;


public class GPS {                     

	public GPS() {
		;
	}

	private boolean currentLocation() {     
		
		Criteria c = new Criteria();                      
		boolean retval = true;
		
		try {
			LocationProvider lp = LocationProvider.getInstance(c);                
			if( lp!=null )
				lp.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);                                                                    
			else 
				retval = false;

		} catch (LocationException le) {                        
			;
		}   
		
		return retval;
	}
     
    public String GPSData() {      
		StringBuffer gpsData = new StringBuffer();        
	        
		if(currentLocation()) {        
			gpsData.append(longitude);                              
			gpsData.append(latitude);           
		}            
	
		return gpsData.toString();                       
	}
    
	private class LocationListenerImpl implements LocationListener {           
		public void locationUpdated(LocationProvider provider, Location location) {
			if(location.isValid()) {        
				heading = location.getCourse();
				longitude = location.getQualifiedCoordinates().getLongitude();
				latitude = location.getQualifiedCoordinates().getLatitude();
				altitude = location.getQualifiedCoordinates().getAltitude();
				speed = location.getSpeed();                                
			}
		}
    
		public void providerStateChanged(LocationProvider provider, int newState) {
			//no-op
		}      
	}
}
Offline  
Old 06-03-2008, 02:05 PM   #10
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default

Thanks for help but even when I put the private method where you have yours i still get the same 6 errors. I don't understand at all!!!
Offline  
Old 06-03-2008, 02:05 PM   #11
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Angry still can't get it

class G extends MainScreen
{

public G()
{

super();
// Set criteria for selecting a location provider:
Criteria cr= new Criteria();
cr.setCostAllowed(true);
cr.setPreferredResponseTime(60);
cr.setHorizontalAccuracy(5000);
cr.setVerticalAccuracy(5000);
cr.setAltitudeRequired(true);
cr.isSpeedAndCourseRequired();
cr.isAddressInfoRequired();
add(new RichTextField("Getting Coordinates...."));

try{
LocationProvider lp = LocationProvider.getInstance(cr);
if( lp!=null ){
lp.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
}
add(new RichTextField("I'm in try statement"));
Location l = lp.getLocation(120); //always times out
Coordinates c = l.getQualifiedCoordinates();
double longitude = 0;
double latitude = 0;
long timestamp = l.getTimestamp();
if(c != null )
{
// Use coordinate information
latitude = c.getLatitude();
longitude = c.getLongitude();
add(new RichTextField("Went inside if statement"));
}
add(new RichTextField("other side"));
//System.out.println("Lon" + longitude + " Lat "+ latitude + " course "+course+" speed "+speed+" timestamp "+timestamp);
}
catch(LocationException le)
{
add(new RichTextField("Location exception "+le));
}
catch(InterruptedException ie)
{
add(new RichTextField("Interrupted exception "+ie));
}

}


private class LocationListenerImpl implements LocationListener {
public void locationUpdated(LocationProvider provider, Location location) {
if(location.isValid()) {
longitude = location.getQualifiedCoordinates().getLongitude();
latitude = location.getQualifiedCoordinates().getLatitude();
altitude = location.getQualifiedCoordinates().getAltitude();
speed = location.getSpeed(); }
}
}
}
Offline  
Old 06-04-2008, 08:04 AM   #12
CELITE
Thumbs Must Hurt
 
Join Date: Dec 2005
Model: 8310
Carrier: Rogers
Posts: 138
Default

It would be very helpful if you made use of the code tags to format your code for us. What jwhanes provided should work. Could you tell us the kinds of errors you're getting?
Offline  
Old 06-04-2008, 08:17 AM   #13
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default

i get that the private LocationListerner... can't be there. To be more specific:
bin.GPS.G.LocationListenerImpl is not abstract and does not override abstract method providerStateChanged(javax.microedition.location.L ocationProvider,int) in javax.microedition.location.LocationListener
private class LocationListenerImpl implements LocationListener {

the other 5 errors are just can't find variables that are in this private so I know once I figure out what's wrong with private they will go away
Offline  
Old 06-04-2008, 08:22 AM   #14
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default

Hope this looks better
Code:

PHP Code:
class extends MainScreen
{
                       
    public 
G()  
    {
      
      
super(); 
                    
// Set criteria for selecting a location provider:
                    
Criteria cr= new Criteria();
                    
cr.setCostAllowed(true);
                    
cr.setPreferredResponseTime(60);
                    
cr.setHorizontalAccuracy(5000);
                    
cr.setVerticalAccuracy(5000);
                    
cr.setAltitudeRequired(true);
                    
cr.isSpeedAndCourseRequired();
                    
cr.isAddressInfoRequired();
                   
add(new RichTextField("Getting Coordinates...."));
                
                try{    
              
LocationProvider lp LocationProvider.getInstance(cr);
                    if( 
lp!=null ){
                            
lp.setLocationListener(new LocationListenerImpl(), _interval11); 
                                }           
                    
add(new RichTextField("I'm in try statement"));
                    
Location l lp.getLocation(120); //always times out
                    
Coordinates c l.getQualifiedCoordinates();
                    
double longitude 0;
                    
double latitude 0;
                    
long timestamp l.getTimestamp();             
                    if(
!= null 
                    {
                        
// Use coordinate information
                        
latitude c.getLatitude();
                        
longitude c.getLongitude();
                        
add(new RichTextField("Went inside if statement"));
                    }
                     
add(new RichTextField("other side"));
                     
//System.out.println("Lon" + longitude + " Lat "+ latitude + " course "+course+" speed "+speed+" timestamp "+timestamp);
                
}
               catch(
LocationException le)
                {
                    
add(new RichTextField("Location exception "+le));
                }
                catch(
InterruptedException ie)
                {
                    
add(new RichTextField("Interrupted exception "+ie));
            }
            
    }
    
    
             private class 
LocationListenerImpl implements LocationListener 
            public 
void locationUpdated(LocationProvider providerLocation location) {
                if(
location.isValid()) { 
                    
longitude location.getQualifiedCoordinates().getLongitude();
                    
latitude location.getQualifiedCoordinates().getLatitude();
                    
altitude location.getQualifiedCoordinates().getAltitude();
                    
speed location.getSpeed(); }
            }
        }

Offline  
Old 06-04-2008, 08:55 AM   #15
CELITE
Thumbs Must Hurt
 
Join Date: Dec 2005
Model: 8310
Carrier: Rogers
Posts: 138
Default

This code in no longer necessary since you're using the listener:

PHP Code:

                    Location l 
lp.getLocation(120); //always times out
                    
Coordinates c l.getQualifiedCoordinates();
                    
double longitude 0;
                    
double latitude 0;
                    
long timestamp l.getTimestamp();             
                    if(
!= null 
                    {
                        
// Use coordinate information
                        
latitude c.getLatitude();
                        
longitude c.getLongitude();
                        
add(new RichTextField("Went inside if statement"));
                    } 
But I don't see any reason from your code that you should be getting errors. Would you care to elaborate?
Offline  
Old 06-04-2008, 09:01 AM   #16
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default Here you go

I think that that's what has been so frustrating for me because I follow the documentation I found and my code looks like everyone elses' that I find on the internet but mine doesn't work. Maybe I'll give you the whole thing, maybe it's just something small I forgot. I'm using RIM Java Blackberry Environment to code and here is the code from top to bottom. I took out the part you said I don't need but got the same six errors.

Last edited by eforr; 06-04-2008 at 09:03 AM..
Offline  
Old 06-04-2008, 09:03 AM   #17
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Default

PHP Code:
package bin.GPS;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.io.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import javax.microedition.io.*;
import java.util.*;
import java.io.*;
import javax.microedition.location.*;

 class 
GPSDemo extends net.rim.device.api.ui.UiApplication 
{
    public static 
void main(Stringxxx91;xxx93; args)
    {
       
GPSDemo instance = new GPSDemo();
        
instance.enterEventDispatcher();
    }
    public 
GPSDemo() 
    {
        
pushScreen(new G()); 
    }
}

class 
extends MainScreen
{
                       
    public 
G()  
    {
      
      
super(); 
                    
// Set criteria for selecting a location provider:
                    
Criteria cr= new Criteria();
                    
cr.setCostAllowed(true);
                    
cr.setPreferredResponseTime(60);
                    
cr.setHorizontalAccuracy(5000);
                    
cr.setVerticalAccuracy(5000);
                    
cr.setAltitudeRequired(true);
                    
cr.isSpeedAndCourseRequired();
                    
cr.isAddressInfoRequired();
                   
add(new RichTextField("Getting Coordinates...."));
                
                try{    
              
LocationProvider lp LocationProvider.getInstance(cr);
                    if( 
lp!=null ){
                            
lp.setLocationListener(new LocationListenerImpl(), _interval11); 
                                }           
                    
add(new RichTextField("I'm in try statement"));
                                        
add(new RichTextField("other side"));
                     
//System.out.println("Lon" + longitude + " Lat "+ latitude + " course "+course+" speed "+speed+" timestamp "+timestamp);
                
}
               catch(
LocationException le)
                {
                    
add(new RichTextField("Location exception "+le));
                }
                catch(
InterruptedException ie)
                {
                    
add(new RichTextField("Interrupted exception "+ie));
            }
            
    }
    
    
             private class 
LocationListenerImpl implements LocationListener 
            public 
void locationUpdated(LocationProvider providerLocation location) {
                if(
location.isValid()) { 
                    
longitude location.getQualifiedCoordinates().getLongitude();
                    
latitude location.getQualifiedCoordinates().getLatitude();
                    
altitude location.getQualifiedCoordinates().getAltitude();
                    
speed location.getSpeed(); }
            }
        }

Offline  
Old 06-04-2008, 11:58 AM   #18
richard.puckett
Talking BlackBerry Encyclopedia
 
richard.puckett's Avatar
 
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
PIN: N/A
Carrier: T-Mobile
Posts: 212
Default

eforr: In all seriousness, you need to learn Java. Right now you're just blindly moving code around - and doing so in the JDE, which isn't helping anything (the Eclipse IDE will underline undefined variables and flag incomplete interface implementations).

The errors that you mentioned were all trivial to find and resolve. You're not exactly starting with HelloWorld here, so if you plan on continuing with development you need to get a good book or two ("Thinking in Java" is great), hang out on some Java forums, and get up to speed on the language itself. Otherwise, there will simply be no end to the errors.

Now, the LocationListenerImpl implements LocationListener and needs to implement both methods from that interface; it only implemented one. Further, many of the variables were not declared. Basic stuff.

I don't know if the code now *works* but it compiles.

Code:
class G extends MainScreen
{
	private int _interval = -1;

	public G()  
	{

		super(); 
		// Set criteria for selecting a location provider:
		Criteria cr= new Criteria();
		cr.setCostAllowed(true);
		cr.setPreferredResponseTime(60);
		cr.setHorizontalAccuracy(5000);
		cr.setVerticalAccuracy(5000);
		cr.setAltitudeRequired(true);
		cr.isSpeedAndCourseRequired();
		cr.isAddressInfoRequired();
		add(new RichTextField("Getting Coordinates...."));

		try{    
			LocationProvider lp = LocationProvider.getInstance(cr);
			if( lp!=null ){
				// Must declare "_interval" somewhere.  I did this above.  See Javadocs
				// for an explanation of what this parameter does.
				lp.setLocationListener(new LocationListenerImpl(), _interval, 1, 1); 
			}           
			add(new RichTextField("I'm in try statement"));
			add(new RichTextField("other side"));
			//System.out.println("Lon" + longitude + " Lat "+ latitude + " course "+course+" speed "+speed+" timestamp "+timestamp);
		}
		catch(LocationException le)
		{
			add(new RichTextField("Location exception "+le));
		}

	}


	private class LocationListenerImpl implements LocationListener { 
		public void locationUpdated(LocationProvider provider, Location location) {
			if(location.isValid()) {
				// These needed to be declared.  Don't have to be declared right here,
				// but they need to be declared *somewhere* that's visible from here.
				double longitude = location.getQualifiedCoordinates().getLongitude();
				double latitude = location.getQualifiedCoordinates().getLatitude();
				double altitude = location.getQualifiedCoordinates().getAltitude();
				float speed = location.getSpeed(); }
		}

		public void providerStateChanged(LocationProvider provider, int newState) {
			// MUST implement this.  Should probably do something useful with it as well.
		}
	}
}
__________________
Do your homework and know how to ask a good question.

Last edited by richard.puckett; 06-04-2008 at 12:02 PM..
Offline  
Old 06-04-2008, 12:45 PM   #19
eforr
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 7100i
PIN: N/A
Carrier: nextel
Posts: 23
Smile Thank You

Richard,

Thanks alot for your comments. the program now works and gets the coordinates. In my defense I do need to re-learn java because the last time i coded in it was sorphmore year in college '04. This, hopefully, was a one-time request from my boss that he anted to see if i could figure out. I'm actually a web developer and i do all my back end coding in visual basic.net. If you ever need any aid in vb.net let me know or find me at asp.net forums because that's usually where i'm at. I really do thank you for your help though because while coding is coding and I still remember how to just display something in java, all the syntax properties have left the building.

thanks
Offline  
Old 06-04-2008, 01:11 PM   #20
richard.puckett
Talking BlackBerry Encyclopedia
 
richard.puckett's Avatar
 
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
PIN: N/A
Carrier: T-Mobile
Posts: 212
Default

No problem - hope my comments didn't come across as too harsh. It just looked like you might have been heading down a painful road.

Glad it's working for you now!
__________________
Do your homework and know how to ask a good question.
Offline  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


ResQGARD Military Kit Impedance Threshold Device 12-0706-000 NSN 6515015758173 picture

ResQGARD Military Kit Impedance Threshold Device 12-0706-000 NSN 6515015758173

$25.00



BECO Universal Impedance Bridge Model 315A, Brown Electro Measurement Corp. 315A picture

BECO Universal Impedance Bridge Model 315A, Brown Electro Measurement Corp. 315A

$139.00



TC ESI Impedance Bridge Model 250-DA Serial 1394 Electro-MeasurementS Oregon USA picture

TC ESI Impedance Bridge Model 250-DA Serial 1394 Electro-MeasurementS Oregon USA

$69.99



Digital Ohmmeter LCD Audio Impedance Test Meter Speaker Voice Resistor System picture

Digital Ohmmeter LCD Audio Impedance Test Meter Speaker Voice Resistor System

$56.99



BECO Universal Impedance Bridge Model 315A, Brown Electro Measurement Corp. 315A picture

BECO Universal Impedance Bridge Model 315A, Brown Electro Measurement Corp. 315A

$129.99



SOLAR ELECTRONICS MODEL 8012-50-R-24-BNC LINE IMPEDANCE STABILIZATION NETWORK picture

SOLAR ELECTRONICS MODEL 8012-50-R-24-BNC LINE IMPEDANCE STABILIZATION NETWORK

$900.00







Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.