本文介绍了如何在黑莓中获取当前位置的纬度和经度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个具有 GPS 功能的应用程序.如何获取当前位置的经纬度.
I am developing an app which has GPS functionality. How can I get latitude and longitude of the current location.
推荐答案
我自己找到了解决方案.以下代码对我来说很好用:
I found a solution by myself. The following code works fine for me:
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));
}
});
}
}
这篇关于如何在黑莓中获取当前位置的纬度和经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!