如何在blackberry中获取纬度和经度?
我正在黑莓手机上开发具有GPS功能的应用程序。所以首先我想得到当前的经纬度,但在等待GPS定位时,它会像超时一样下降。下面是我获取当前位置的纬度和经度的代码如何在blackberry中获取纬度和经度?,blackberry,Blackberry,我正在黑莓手机上开发具有GPS功能的应用程序。所以首先我想得到当前的经纬度,但在等待GPS定位时,它会像超时一样下降。下面是我获取当前位置的纬度和经度的代码 public class LocationScreen extends MainScreen { public LocationScreen() { super(); final LabelField lfLocation = new LabelField(); n
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。尝试此代码
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);
}
}
}
请尝试此代码
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);
}
}
}
制作简单的
线程
,并在运行方法
中写下下面的代码
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
在我的设备上尝试此功能,它可以正常工作。
我希望它能帮助您..简化
线程
,并在运行方法
中写下下面的代码
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
在我的设备上尝试此功能,它可以正常工作。
我希望这对你有帮助