2012-01-03 200 views
1

我正在開發一個具有GPS功能的應用程序。我怎樣才能獲得當前位置的經緯度。如何獲取黑莓當前位置的經度和緯度?

+1

已經嘗試此 http://stackoverflow.com/questions/5594715/blackberry-gps-getting-current-location-address/5618129#5618129 – Mercy 2012-01-03 11:06:03

+1

這個問題這裏討論請覈實 http://stackoverflow.com/questions/7672808/gps-lat-lon-issue-in-blackberry-storm/7673953#7673953 – 2012-01-03 11:14:06

回答

1

我自己找到了解決方案。以下代碼適用於我:

package mypackage; 

import javax.microedition.location.Location; 
import javax.microedition.location.LocationException; 
import javax.microedition.location.LocationListener; 
import javax.microedition.location.LocationProvider; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.component.RichTextField; 
import net.rim.device.api.ui.container.MainScreen; 

public final class MyScreen extends MainScreen 
{ 
    private LocationProvider locationProvider; 
    private static int interval = 1; 
    double lat; 
    double longt; 

    public MyScreen() 
    { 
     setTitle("MyTitle"); 
     startLocationUpdate(); 
    } 

    private boolean startLocationUpdate() 
    { 
     boolean retval = false; 

     try 
     { 
      locationProvider = LocationProvider.getInstance(null); 

      if (locationProvider == null) 
      { 
       Runnable showGpsUnsupportedDialog = new Runnable() 
       { 
        public void run() 
        { 
         Dialog.alert("GPS is not supported on this platform, exiting..."); 
         //System.exit(1); 
        } 
       }; 

       UiApplication.getUiApplication().invokeAndWait(showGpsUnsupportedDialog); // Ask event-dispatcher thread to display dialog ASAP. 
      } 
      else 
      { 
       locationProvider.setLocationListener(new LocationListenerImpl(), interval, 1, 1); 

       retval = true; 
      } 
     } 
     catch (LocationException le) 
     { 
      System.err.println("Failed to instantiate the LocationProvider object, exiting..."); 
      System.err.println(le); 
      System.exit(0); 
     } 
     return retval; 
    } 

    private class LocationListenerImpl implements LocationListener 
    { 
     public void locationUpdated(LocationProvider provider, Location location) 
     { 
      if(location.isValid()) 
      { 
       double longitude = location.getQualifiedCoordinates().getLongitude(); 
       double latitude = location.getQualifiedCoordinates().getLatitude(); 

       updateLocationScreen(latitude, longitude);    
      } 
     } 

     public void providerStateChanged(LocationProvider provider, int newState) 
     { 
     } 
    } 

    private void updateLocationScreen(final double latitude, final double longitude) 
    { 
     UiApplication.getUiApplication().invokeAndWait(new Runnable() 
     { 
      public void run() 
      { 
       lat = latitude; 
       longt = longitude; 

       RichTextField txt=new RichTextField(); 
       txt.setText("Long=="+longt); 

       RichTextField txt1=new RichTextField(); 
       txt1.setText("lat=="+lat); 

       add(txt); 
       add(txt1); 
       //persistentLatitude.setContents(Double.toString(latitude)); 
       //persistentLongitude.setContents(Double.toString(longitude)); 
      } 
     }); 
    } 
} 
1

如果您需要快速獲取當前位置,請使用以下代碼。它適用於OS 6和更高:

private void findGeolocation() { 
    Thread geolocThread = new Thread() { 
     public void run() { 
      try { 
       if (!LocationInfo.isLocationOn()) { 
        LocationInfo.setLocationOn(); 
       } 

       BlackBerryCriteria myCriteria = new BlackBerryCriteria(); 
       myCriteria 
         .enableGeolocationWithGPS(BlackBerryCriteria.FASTEST_FIX_PREFERRED); 

       try { 
        BlackBerryLocationProvider myProvider = (BlackBerryLocationProvider) LocationProvider 
          .getInstance(myCriteria); 
        try { 
         BlackBerryLocation myLocation = (BlackBerryLocation) myProvider 
           .getLocation(-1); 
         _longitude = myLocation.getQualifiedCoordinates() 
           .getLongitude(); 
         _latitude = myLocation.getQualifiedCoordinates() 
           .getLatitude(); 

         showResults(_latitude, _longitude); 

        } catch (InterruptedException e) { 
         showException(e); 
        } catch (LocationException e) { 
         showException(e); 
        } 
       } catch (LocationException e) { 
        showException(e); 
       } catch (Exception e) { 
        showException(e); 
       } 
      } catch (UnsupportedOperationException e) { 
       showException(e); 
      } catch (Exception e) { 
       showException(e); 
      } 
     } 
    }; 

    geolocThread.start(); 
} 

private void showResults(double _latitude, double _longitude) { 
    System.out.println("latitude = " + _latitude + "longitude" + _longitude); 

    Application.getApplication().invokeLater(new Runnable() { 
     public void run() { 
      Dialog.alert("latitude = " + _latitude + "longitude" + _longitude); 
     } 
    }); 
} 

private void showException(final Exception e) { 
    Application.getApplication().invokeLater(new Runnable() { 
     public void run() { 
      Dialog.alert(e.getMessage()); 
     } 
    }); 
} 
+0

是的,感謝重播。 – Hasmukh 2012-09-28 09:33:25

相關問題