【问题标题】:How i can get Latitude and Longitude in blackberry?我如何在黑莓中获得纬度和经度?
【发布时间】:2012-01-06 11:29:49
【问题描述】:

我正在开发具有 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;
  }

我在代码中做错了什么,请帮助我。 提前谢谢。

【问题讨论】:

    标签: blackberry


    【解决方案1】:

    制作简单的Thread 并在run 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 试试这个,它可以正常工作。 我希望它会帮助你..

    【讨论】:

      【解决方案2】:

      试试这个代码.....

      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);
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-06
        • 1970-01-01
        • 2023-04-02
        • 2019-10-11
        • 2011-08-18
        • 1970-01-01
        相关资源
        最近更新 更多