2012-01-06 58 views
1

我正在開發具有GPS功能的黑莓應用程序。所以首先我想獲得當前的經度和緯度,但它會像一樣在等待GPS位置時超時。在我的代碼中獲取當前位置的經緯度。我如何獲得黑莓的經度和緯度?

public class LocationScreen extends MainScreen 
{ 
     public LocationScreen() 
     { 
     super(); 
     final LabelField lfLocation = new LabelField(); 
     new Thread() 
     { 
      public void run() 
      { 
      LocationHandler lh = new LocationHandler(); 
      final String msg = "Long: " + lh.getCurrentLongitude() + ", Lat: " + lh.getCurrentLatitude(); 

      Application.getApplication().invokeLater(new Runnable() 
      { 
       public void run() 
       { 
       lfLocation.setText(msg); 
       add(new LabelField("Lat and Long=="+msg)); 
       } 
      }); 
      } 
     }.start(); 
     this.add(lfLocation); 
     } 
    } 

**Second Class** 

public class LocationHandler 
{ 
    private LocationProvider locationProvider; 
    private Location currentLocation; 
    public LocationHandler() 
    { 
    try 
    { 
     locationProvider = LocationProvider.getInstance(null); 
     currentLocation = locationProvider.getLocation(60); 
    } 
    catch (final Exception e) 
    { 
     Application.getApplication().invokeLater(new Runnable() 
     { 
     public void run() 
     { 
      Status.show(e.getMessage()); 
     } 
     }); 
    } 
    } 
    public double getCurrentLongitude() 
    { 
    if(currentLocation != null) 
     return currentLocation.getQualifiedCoordinates().getLongitude(); 
    return -1; 
    } 
    public double getCurrentLatitude() 
    { 
    if(currentLocation != null) 
     return currentLocation.getQualifiedCoordinates().getLatitude(); 
    return -1; 
    } 

我在做什麼錯誤請在我的代碼中幫助我。 Thanx提前。

回答

4

進行簡單Threadrun method寫下下面的代碼。

private BlackBerryCriteria blackBerryCriteria = null; 
private BlackBerryLocation blackBerryLocation = null; 
private BlackBerryLocationProvider blackBerryLocationProvider = null; 
double lat = 0.0; 
double lng = 0.0; 

blackBerryCriteria = new BlackBerryCriteria(); 

    if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_CELLSITE)) 
    { 
     blackBerryCriteria.setMode(GPSInfo.GPS_MODE_CELLSITE); 
    } 
    else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_ASSIST)) 
    { 
     blackBerryCriteria.setMode(GPSInfo.GPS_MODE_ASSIST); 
    } 
    else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_AUTONOMOUS)) 
    { 
     blackBerryCriteria.setMode(GPSInfo.GPS_MODE_AUTONOMOUS); 
    } 
    else 
    { 
     blackBerryCriteria.setCostAllowed(true); 
     blackBerryCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW); 
    } 

try { 
      blackBerryLocationProvider = (BlackBerryLocationProvider) BlackBerryLocationProvider.getInstance(blackBerryCriteria); 
      blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60); 

      QualifiedCoordinates qualifiedCoordinates = blackBerryLocation.getQualifiedCoordinates(); 

      lat = qualifiedCoordinates.getLatitude(); 
      lng = qualifiedCoordinates.getLongitude();}catch(Exception e) 
     { 
      System.out.println("Error in location :"+e.toString()); 
      System.out.println("Error in location :"+e.getMessage()); 
     } 

試試這個工作正常,在我的設備上使用BIS。 我希望它會幫助你..

+0

thanx的Replay..really這項工作精細。 – Hasmukh 2012-01-16 13:22:09

0

試試這個代碼.....

public class GetLatLon { 
private LocationProvider provider,_locationProvider; 
public static Timer timer; 
double longitude=0.0,lattitude=0.0; 
Criteria criteria; 
public GetLatLon() 
{ 
    startLocationUpdate(); 
} 

public class LocationListenerImpl implements LocationListener { 
    public void locationUpdated(LocationProvider provider, final Location location) { 
     System.out.println("---Location Updated-----"); 
     if (location.isValid()) { 
      System.out.println("---Location Valid----"); 
        longitude = location.getQualifiedCoordinates().getLongitude(); 
        lattitude= location.getQualifiedCoordinates().getLatitude(); 
        System.out.println("Lattitude :-=============================="+lattitude); 
        System.out.println("Longitude :-=============================="+longitude); 
        _locationProvider.setLocationListener(null, 0, 0, 0); 
      }else{ 
      System.out.println("else NOT valid--Cellsite valide=-----"); 
      setupCriteria(); 
      setupProvider(); 
     } 
     System.out.println("---Location Not Valid----"); 
    } 
    public void providerStateChanged(LocationProvider provider, int newState) {} 
} 

private void setupCriteria() { 
    criteria = new Criteria(); 
    criteria.setCostAllowed(true); 
    criteria.setHorizontalAccuracy(Criteria.NO_REQUIREMENT); 
    criteria.setVerticalAccuracy(Criteria.NO_REQUIREMENT); 
    criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW); 
} 


private void setupProvider() { 
    try { 
     try { 
      Thread.sleep(5000); 
     } catch (Throwable e) { 
      System.out.println(e.toString()); 
     } 
     provider = LocationProvider.getInstance(criteria); 
     provider.setLocationListener(
       new LocationListenerImpl(), 1, 1, 1); 
    } catch (Throwable t) { 
     System.out.println(t.toString()); 
    } 
} 
public void startLocationUpdate() 
{ 
    try{ 
     _locationProvider = LocationProvider.getInstance(null); 
     if (_locationProvider != null) { 
      _locationProvider.setLocationListener(
        new LocationListenerImpl(), 1, 1, 1); 
     } 
    }catch(LocationException le){ 
     System.out.println("----Exception Of Location--"+le); 
    } 
} 
} 
+0

雅thanx的rplay .. – Hasmukh 2012-01-06 13:06:21

+0

你有你的答案..... – 2012-01-06 14:12:15

+0

沒有它會顯示空白屏幕設備.. – Hasmukh 2012-01-07 07:48:32