有谁可以告诉我如何使用黑莓基于位置的服务? 我正在开发一个黑莓移动应用程序的项目。我以前从未吃过黑莓,也没有与任何提供商签订合同(只需3张SIM卡和9000 OS 4.6移动设备)。
在项目中,我目前正在尝试使用以下代码来检索当前位置(起点)和目标位置(终点)的坐标。它在模拟器上运行得很好但在设备上没什么。我应该与提供商签订合同吗?这需要只有GPS或互联网,还是两者兼而有之?
代码:
String destination = "London";
final Landmark[] landmarks = Locator.geocode(destination.replace('\n', ' '), null);
Coordinates endPoint = landmarks[0].getQualifiedCoordinates();
// Get a location provider.
LocationProvider provider = LocationProvider.getInstance(null);
if (provider == null)
{
throw new IllegalStateException("No LocationProvider Available!!");
}
// Try to fetch the current location and get the coordinates of the current location.
Coordinates startPoint = provider.getLocation(-1).getQualifiedCoordinates();
double destiinationlatitude = endPoint.getLatitude();
double currentlatitude = startPoint.getLatitude();
提前谢谢
答案 0 :(得分:1)
要在5.0之前的任何版本上获取GPS位置,您必须实例化此事
以下是您实例化的内容:
Criteria criteria = null;
LocationProvider provider = null;
javax.microedition.location.Location location = null;
之后,您必须为Criteria分配值,使用条件获取LocationProvider的实例,并使用LocationProvider获取Location。
criteria = new Criteria();
criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
criteria.setHorizontalAccuracy(50);
criteria.setVerticalAccuracy(50);
criteria.setCostAllowed(true);
provider = LocationProvider.getInstance(criteria);
location = provider.getLocation(5);
请注意,标准将确定您是否使用GPS,Wifi辅助位置或Cellsite位置,有关标准设置的更多信息:http://www.blackberry.com/developers/docs/4.5.0api/javax/microedition/location/Criteria.html
之后,要获取您调用方法的坐标:location.getQualifiedCoordinates()
那就是......你应该从一个单独的线程中调用它。而且实际的位置管理代码应该在try-catch块上,但IDE会帮助你。
答案 1 :(得分:0)
在此代码中,我们看到哪些模式可用于获得坐标(如果手机没有GPS,那么它应该使用satalite信息。)
Lat和Long正在通过可用模式进行检索。
创建了一个mapview(MapView是地图,你设置了所需的规格,如缩放,lat,lon等),然后你调用地图,设置缩放,lat,lon等将应用于反映在屏幕上的地图。
CustomMapField mMapField;
Coordinates mCoordinates;
BlackBerryCriteria blackBerryCriteria = null;
BlackBerryLocation blackBerryLocation = null;
BlackBerryLocationProvider blackBerryLocationProvider = null;
double Doublelat = 0.0;
double Doublelng = 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();
Doublelat = qualifiedCoordinates.getLatitude();
Doublelng = qualifiedCoordinates.getLongitude();
mCoordinates = new Coordinates(Doublelat, Doublelng, 0);
MapView mapView = new MapView();
mapView.setLatitude(finalintlat);
mapView.setLongitude(finalintlng);
mapView.setZoom(10);
MapsArguments mapsArgs = new MapsArguments(mapView);
Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, mapsArgs);
}catch(Exception e){
System.out.println("Error in location :"+e.toString());
System.out.println("Error in location :"+e.getMessage());
}