Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 12
Registered: ‎06-22-2012
My Device: developer
My Carrier: Vodafone

not getting GPS location

Hello

I have been working on getting GPS location I have been through many examples and articles but did not have any success yet.May be I am missing something very minor I guess but I am new to BB development.

 

Below is the code 

 

 


 

 

 

package mypackage;

 

import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;

 

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

 

/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen {

 

/**
* Creates a new MyScreen object
*/
    public MyScreen() {

        setTitle("GPS Demo");

        new GpsThread().start();

    }

 

    public boolean onClose() {
        UiApplication.getUiApplication().requestBackground();
        return false;
    }

 

    private class GpsThread extends Thread {

 

        Location currentLoc; // Stores component information of our position

 

        Criteria cr; // Settings for the GPS - we can read it at
        // different accuracy levels

 

        LocationProvider lp; // LocationProvider does the actual work of reading
        // coordinates from the GPS

 

            public GpsThread() {
                add(new LabelField(("GPS Constructor")));

                cr = new Criteria();

            }

 

            public void run() {


                add(new LabelField(("run")));
                showLocationToast();


            }

 

            private void setCriteria() {

 

            // I basically set no requirements on any of the horizontal,
            // vertical,
            // or
            // power consumption requirements below.
            // The distance components are set in meters if you do want to
            // establish
            // accuracy - the less the accuracy, the
            // quicker and more likely a successful read (I believe).
            // You can also set power consumption, between low, medium, high (or
            // no
            // requirement)
            // There are also a number of other settings you can tweak such as
            // minimum
            // response time, if altitude is required,
            // speed required, etc. It all depends on the exact application
            // you're
            // writing and how specific you need the info to
           cr.setCostAllowed(true);
           cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
           cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
           cr.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
           add(new LabelField(("Criteria set ")));


        }

 

        private void getLocation() throws InterruptedException {

 

            setCriteria();

            try {
                add(new LabelField(("getting location")));
                // Get a new instance of the location provider using the
                // criteria we
                // established above.
               if (lp != null) {
                    lp.setLocationListener(null, 0, -1, -1);
                    lp.reset();
                    lp = null;
                }


            add(new LabelField(("lp had something so resetting it")));
            lp = LocationProvider.getInstance(cr);

            add(new LabelField(("got locationprovider instance ... ")));

 

            // Now populate our location object with our current location
            // (with
           // a 60 second timeout)

           lp.setLocationListener(new MyLocationListener(), 120, -1, -1);


           currentLoc = lp.getLocation(60);

 

           String location = "Latitude : "
           + currentLoc.getQualifiedCoordinates().getLatitude()
           + "\n Longitude : "
           + currentLoc.getQualifiedCoordinates().getLongitude();
           add(new LabelField((location)));

           }

           // If we hit the timeout or encountered some other error, report it.
           catch (LocationException e) {
               // Dialog.alert("Error getting coordinates");
               add(new LabelField(e.getMessage()));
              return;
            }

       }

 

       private void showLocationToast() {


            add(new LabelField(("showing toast")));


            try {
                getLocation();
            } catch (InterruptedException e) {
                add(new LabelField(e.getMessage()));
            }

        
            add(new LabelField(("Got all info")));
            String location = "Latitude : "
            + currentLoc.getQualifiedCoordinates().getLatitude()
            + "\n Longitude : "
            + currentLoc.getQualifiedCoordinates().getLongitude();
            add(new LabelField("Showing toast..." + location));

        }

    }

}

 

 

 


 

 

 

 

class my listener just has dialog that must be shown when location is updated .I even tried to get single location without using listener

 

like 

lp.getLocation (60);

just before my where I had set my listener .But both times I get Label only upto where I have set listener.I will be using it Timer and TimerTask however I am able to get this demo right.

 

I am using 9900 for development and the app is supposed to be on 4.5.0

 

 

Developer
Posts: 59
Registered: ‎06-15-2012
My Device: 9800
My Carrier: XZY

Re: not getting GPS location

 

 

Hey Vishal

 

                refer thisGPS will help you.

Contributor
Posts: 12
Registered: ‎06-22-2012
My Device: developer
My Carrier: Vodafone

Re: not getting GPS location

No Jam321 , I am not looking for geocoding or location based search nor reverse goecoding .just need to get GPS co-ordinates.
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: not getting GPS location

Here are the two articles I recommend that everyone trying to do location coding review before starting their own code:

 

http://supportforums.blackberry.com/t5/Java-Development/Location-APIs-Start-to-finish/ta-p/571949

 

http://supportforums.blackberry.com/t5/Java-Development/Simple-Location-API/ta-p/1145951