我想获得当前位置的值。我使用此代码,但我只是获得此消息“等待位置更新...”。我应该在手机中进行配置以获取当前位置的地址??? 我的手机可以使用GPS状态。但我认为该位置无效,因为我没有得到警报对话框。如何获得当前位置的纬度和经度?
package mypackage;
import net.rim.device.api.gps.*;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import javax.microedition.location.*;
public class MultipleFixDemo extends UiApplication
{
private static int _interval = 1;
private EditField _status;
public static void main(String[] args)
{
new MultipleFixDemo().enterEventDispatcher();
}
public MultipleFixDemo()
{
MultipleFixScreen screen = new MultipleFixScreen();
screen.setTitle("Multiple Fix Demo");
_status = new EditField(Field.NON_FOCUSABLE);
screen.add(_status);
startLocationUpdate();
pushScreen(screen);
}
private void updateLocationScreen(final String msg)
{
invokeLater(new Runnable()
{
public void run()
{
_status.setText(msg);
}
});
}
private void startLocationUpdate()
{
try
{
BlackBerryCriteria myCriteria = new BlackBerryCriteria();
myCriteria.enableGeolocationWithGPS();
try
{
BlackBerryLocationProvider myProvider = (BlackBerryLocationProvider)LocationProvider.getInstance(myCriteria);
if ( myProvider == null )
{
Runnable showUnsupportedDialog = new Runnable()
{
public void run() {
Dialog.alert("Location service unsupported, exiting...");
System.exit( 1 );
}
};
invokeLater( showUnsupportedDialog );
}
else
{
myProvider.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
}
}
catch (LocationException le)
{
System.err.println("Failed to retrieve a location provider");
System.err.println(le);
System.exit(0);
}
}
catch (UnsupportedOperationException ue)
{
System.err.println("Require mode is unavailable");
System.err.println(ue);
System.exit(0);
}
return;
}
private class LocationListenerImpl implements LocationListener
{
public void locationUpdated(LocationProvider provider, Location location)
{
if(location.isValid())
{
Dialog.alert("if");
double longitude = location.getQualifiedCoordinates().getLongitude();
double latitude = location.getQualifiedCoordinates().getLatitude();
float altitude = location.getQualifiedCoordinates().getAltitude();
StringBuffer sb = new StringBuffer();
sb.append("Longitude: ");
sb.append(longitude);
sb.append("\n");
sb.append("Latitude: ");
sb.append(latitude);
sb.append("\n");
sb.append("Altitude: ");
sb.append(altitude);
sb.append(" m");
MultipleFixDemo.this.updateLocationScreen(sb.toString());
}
}
public void providerStateChanged(LocationProvider provider, int newState)
{
// Not implemented
}
}
private final static class MultipleFixScreen extends MainScreen
{
MultipleFixScreen()
{
super(DEFAULT_CLOSE | DEFAULT_MENU);
RichTextField instructions = new RichTextField("Waiting for location update...",Field.NON_FOCUSABLE);
this.add(instructions);
}
}
}
答案 0 :(得分:3)
试试这个代码对我有用。它将每1秒给予lat和long更新。
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 :(得分:0)
尝试使用此代码获取当前位置;
private LocationProvider provider;
private Location _location= null;
Criteria myCriteria = new Criteria();
myCriteria.setCostAllowed(true);
provider = LocationProvider.getInstance(myCriteria);
_location = provider.getLocation(4);
QualifiedCoordinates coordinates = _location.getQualifiedCoordinates();
String lon =Double.toString(coordinates.getLongitude());
String lat = Double.toString(coordinates.getLatitude());
答案 2 :(得分:0)
如果您正在处理Blackberry OS 5.0
并致信,请查看我对"How i can get Latitude and Longitude in blackberry?"的回答。它在2或3秒内获得位置。我希望它会帮助你。在设备中尝试并检查。