2011-08-02 83 views
0

任何人都可以請告訴我如何使用黑莓基於位置的服務? 我正在爲黑莓手機應用程序開發一個項目。我從來沒有黑莓,我沒有任何提供商的合同(只有3卡和移動設備9000操作系統4.6的SIM卡)。黑莓基於位置的服務

在項目中,我目前正嘗試使用以下代碼以檢索當前位置(起點)和目標位置(終點)的座標。它在模擬器上工作得很好,但在設備上什麼也沒有。我應該與供應商簽訂合同嗎?這是否只需要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(); 

預先感謝您

回答

1

要獲得任何版本的GPS位置之前5.0,你必須實例化這個事情

  1. 標準
  2. 位置提供者
  3. 位置對象(使用位置提供程序完成)

這裏的實例化的東西:

Criteria criteria = null; 
LocationProvider provider = null; 
javax.microedition.location.Location location = null; 

之後,你必須賦值的標準,使用標準得到LocationProvider的實例,並使用LocationProvider得到位置。

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輔助定位或小區站點的位置,在標準的詳細信息設置的位置:http://www.blackberry.com/developers/docs/4.5.0api/javax/microedition/location/Criteria.html

之後,讓你調用方法的座標: location.getQualifiedCoordinates()

而那就是...你應該從一個單獨的線程調用它。而且實際的位置管理代碼應該放在try-catch塊上,但IDE會幫助你。

在此代碼,我們看到可用的模式來獲得座標
0

(即,如果手機不具備GPS那麼它應該使用的SATAlite信息。)

的緯度和龍正由可用retreived模式。

創建一個mapview(MapView是地圖,你可以設置需要的規格,如縮放,拉特,lon等),然後調用地圖,設置zoom,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()); 
} 
+0

有些解釋在這裏會有所幫助。 –