views:

473

answers:

1

I am creating an application for [BlackBerry][1].

I want to get the user's latitude and longitude and then generate the maps according to it.

How can I do that?

my code is:::

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

import net.rim.device.api.system.Application;




public class GPS_Location 
{
    private String log;
      double longi;
      double lati;

    public GPS_Location()
    {
        new LocationTracker();
    }
    public boolean onClose()
    {
        Application.getApplication().requestBackground();       
        return false;
    }


class LocationTracker extends TimerTask
{
    private  double longitude, latitude;

    private Timer timer;
    private LocationProvider provider;
    Criteria cr;

    public LocationTracker() 
    {
        timer = new Timer();
        cr= new Criteria();
        resetGPS();

        timer.schedule(this, 0, 60000);
    }

    public void resetGPS()
    {

        try 
        {
            provider = LocationProvider.getInstance(cr);
            if(provider != null) 
            {
                /*provider.setLocationListener(null, 0, 0, 0);
                provider.reset();
                provider = null;*/
                provider.setLocationListener(new MyLocationListener(), 3, -1, -1);
            }

            //provider = LocationProvider.getInstance(null);

        } catch(Exception e)
        { 

        }
    }


    public void run() 
    {
        System.out.println("********************");
    }

    private class MyLocationListener implements LocationListener 
    {
        public void locationUpdated(LocationProvider provider, Location location)
        {
            if(location != null && location.isValid())
            {               
                QualifiedCoordinates qc = location.getQualifiedCoordinates();

                try 
                {
                    lati = location.getQualifiedCoordinates().getLatitude();
                    System.out.println("********************latitude :: "+lati);
                    longi = location.getQualifiedCoordinates().getLongitude();
                    System.out.println("********************longitude ::"+longi);

                } 
                catch(Exception e)
                   { 

                   }
            }

        }

        public void providerStateChanged(LocationProvider provider, int newState)
        {
            //LocationTracker.this.resetGPS();
             if(newState == LocationProvider.TEMPORARILY_UNAVAILABLE)
                {
                    provider.reset();
                    provider.setLocationListener(null, 0, 0, -1);
                }
        }
    }
  } 
}
+1  A: 

A Google result (coincidentally Stack Overflow) reveals the API call getLocation(). This may provide you with a starting point for retrieving the longitude and latitude.

Addition; the following may be helpful (from a subsequent Google search using terms based on your comment): http://www.blackberryforums.com/developer-forum/133152-location-api.html. I would quote some of the code, but there is a fair bit of information there. Let's just hope the link remained valid, or you post your solution when found :)

Shadow
i used location.getQualifiedCordinates but yeilds 0 value for latitude n longitude,i even changed value of cordinates in simulator also
SWATI
hey Shadow i worked in the same way as link suggested but every time result is 0/0 (i m working on simulator storm 9000).
SWATI
hey frnz i got my error,it was with simulatorthe value was not properly set....but thanks fr the help!!!!!!!!!!!!!!
SWATI