获取当前位置黑莓的经纬度
Posted
技术标签:
【中文标题】获取当前位置黑莓的经纬度【英文标题】:get latitude and longitude of current location blackberry 【发布时间】:2012-03-14 11:12:55 【问题描述】:我想获取当前位置的值。我使用此代码,但我只收到此消息“等待位置更新...”。我应该在手机中进行配置以获取当前位置的地址吗??? 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);
【问题讨论】:
这个问题与***.com/questions/8757160/…非常相似 【参考方案1】:试试这个代码,它会为我工作。它会每 1 秒更新一次纬度和经度。
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));
);
【讨论】:
【参考方案2】:尝试使用此代码获取当前位置;
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());
【讨论】:
【参考方案3】:如果您正在处理Blackberry OS 5.0
和信件,请查看我对"How i can get Latitude and Longitude in blackberry?" 的回复。它在 2 或 3 秒内获得位置。我希望它会帮助你。在设备中试用并检查。
【讨论】:
以上是关于获取当前位置黑莓的经纬度的主要内容,如果未能解决你的问题,请参考以下文章