fetching gps location in blackberry

Posted by SWATI on Stack Overflow See other posts from Stack Overflow or by SWATI
Published on 2010-05-12T05:40:52Z Indexed on 2010/05/12 5:44 UTC
Read the original article Hit count: 229

Filed under:

in my application i try to fetch users location but it always 0.0 for both latitude and longitude.I have seen blackberry forum but couldn't find what am i doing wrong???

code :

  package com.MyChamberApp;

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

    public class GPS_Location {
     static double longi;
     static double lati;

     public GPS_Location() {

     }

     public void location()
     {
      new LocationTracker();
     }

     class LocationTracker 
     {
      private LocationProvider provider;
      Criteria cr;

      public LocationTracker() 
      {
       resetGPS();
      }

      public void resetGPS() {

      try {
      cr = new Criteria();
      cr.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
      cr.setPreferredResponseTime(120000);
                cr.setCostAllowed(true);
      provider = LocationProvider.getInstance(cr);
      provider.getLocation(120);
      if (provider != null) 
       {
        provider.setLocationListener(new MyLocationListener(), 1,1,1);
       }
      } catch (Exception e) {}

      }

      public void run(){}

     private class MyLocationListener implements LocationListener 
         {
       public void providerStateChanged(LocationProvider provider,int newState)
       {
        if (newState == LocationProvider.TEMPORARILY_UNAVAILABLE)
        {
          provider.reset();
          resetGPS();
        }
       if (newState == LocationProvider.OUT_OF_SERVICE)
       {
        provider.reset();
        resetGPS();
       }
     }

     public void locationUpdated(LocationProvider provider,Location location) 
      {
       if (location != null && location.isValid()) {
        try {
      lati = location.getQualifiedCoordinates().getLatitude();
      longi = location.getQualifiedCoordinates().getLongitude();
         } catch (Exception e) {}
     }
      }
    }
   }
 }

i have tried this code on curve8300,bold9000 It works well on simulator but does not fetches value on device till i manually do not refresh my gps. thanks in advance!!!!!!!!!!!!!!!!

© Stack Overflow or respective owner

Related posts about blackberry