我正在开发具有GPS功能的Blackberry应用程序。所以首先我要获得当前的纬度和经度,但它会在等待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;
}
我在代码中犯的错误请帮帮我。 提前完成。
答案 0 :(得分:4)
制作简单的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
在我的设备中正常工作。
我希望它会对你有所帮助..
答案 1 :(得分: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);
}
}
}