-1

在我的應用程序中,需要取當前的位置信息,我使用下面的代碼;並稱之爲buttonclick,但它不能在設備和模擬器中工作,如果任何人有任何想法請幫助。GPS位置信息問題

private void getLocationServices(){ 
    Thread geoThread=new Thread(){ 
     public void run(){ 
      try{ 
       Criteria myCriteria = new Criteria(); 
       myCriteria.setCostAllowed(false); 
       try{ 
        LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria); 
        try{ 
         locationaddress=new AddressInfo(); 
         Location myLocation = myLocationProvider.getLocation(300); 
         _lattitude = myLocation.getQualifiedCoordinates().getLatitude(); 
         _longitude = myLocation.getQualifiedCoordinates().getLongitude(); 
         int intLatitude = (int) _lattitude * 100000; 
         int intLongitude = (int) _longitude * 100000; 
         try{ 
          Landmark[] results= Locator.reverseGeocode(intLatitude, intLongitude, Locator.ADDRESS); 
          if (results != null && results.length > 0) { 
           locationaddress=results[0].getAddressInfo(); 
           lblLoc.setText(locationaddress.getField(AddressInfo.CITY)); 
          } 
         }catch (Exception e) { 
          // TODO: handle exception 
         } 

        }catch (Exception e) { 
         // TODO: handle exception 
        } 

       }catch (Exception e) { 
        // TODO: handle exception 
       } 
      }catch (Exception e) { 
       // TODO: handle exception 
      } 
     } 
    }; 
    geoThread.start(); 
} 
+0

你有它不工作更多的細節?例如,你是否遇到異常?如果是這樣,在哪一行?或者你的結果與預期不同?需要一些關於什麼不工作的更多細節。 – 2011-02-08 16:59:04

回答

0

你在模擬器中模擬了GPS嗎?模擬 - > GPS位置,然後在對話框中添加一個位置或配置一條模擬路線。

0

第一步是在異常處理程序中添加一些日誌記錄。

這可能會告訴你一些異常被拋出。一旦你明白了,你可以看到代碼是如何做錯的,導致異常。

0

這可能不是GPS問題。很可能您嘗試更新UI字段而不在事件線程中。有關如何避免這種情況的信息,請參閱my answer to this related question

0
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()); 
} 

一個解釋這個「爛攤子」,請參閱:blackberry location-based services

+0

沒有解釋去與這混亂? – 2012-10-06 01:56:08