2011-09-03 2 views
0

나는 Blackberry의 GPS를 사용하는 것을 배우고 있으며 GPS를 다루는 Beginning Blackberry 책의 섹션을 따라 왔지만 코드가 무언가 잘못되어 있기 때문에 앱에만 적용됩니다. '위치 찾기'까지. 나는 붐에서 소스를 다운로드하려고 시도했다. 나는 뭔가를 놓친다.하지만 그 역시 작동하지 않았다. SDK에서 샘플 앱을 테스트 한 결과 장치의 GPS가 작동하는 것을 알고 있습니다. 아래는 제 코드입니다. 당신이 LocationProvider.getInstance(criteria)에서 LocationException를 얻을 수 있는지Blackberry - GPS 코딩 시작, 앱에서 위치를 찾지 못함

LocationHandler.java

public class LocationHandler extends Thread{ 
    private MyScreen screen; 

    public LocationHandler(MyScreen screen){ 
     this.screen = screen; 
    } 

    public void run(){ 
     Criteria criteria = new Criteria(); 
     criteria.setVerticalAccuracy(50); 
     criteria.setHorizontalAccuracy(50); 
     criteria.setCostAllowed(true); 
     criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH); 

     try{ 
      screen.setMessage("Getting location..."); 
      LocationProvider provider = LocationProvider.getInstance(criteria); 
      Location location = provider.getLocation(-1); 

      QualifiedCoordinates qualifiedCoordinates = location.getQualifiedCoordinates(); 

      screen.setLocation(qualifiedCoordinates.getLongitude(), qualifiedCoordinates.getLatitude()); 
      String message = "Successfully got location, method: "; 

      int method = location.getLocationMethod(); 

      if((method & Location.MTA_UNASSISTED)==Location.MTA_ASSISTED){ 
       message += "Unassisted GPS"; 
      } 

      if((method & Location.MTE_CELLID)==Location.MTE_CELLID){ 
       message += "Cell site"; 
      } 

      message += "\nHorizontal (Longitude) Accuracy: "; 
      message += qualifiedCoordinates.getHorizontalAccuracy(); 

      message += "\nVertical (latitude) Accuracy: "; 
      message += qualifiedCoordinates.getVerticalAccuracy(); 

      screen.setMessage(message); 
     }catch(LocationException e){ 
      screen.setMessage("Location Exception: " + e.getMessage()); 
     }catch (InterruptedException ex){ 
      screen.setMessage("InteruptedException: " + ex.getMessage()); 
     } 
    } 
} 

MyScreen.java

public final class MyScreen extends MainScreen 
{ 
    private LabelField latitudeLbl; 
    private LabelField longitudeLbl; 
    private RichTextField messageField; 

    public MyScreen() 
    {   
    HorizontalFieldManager latManager = new HorizontalFieldManager(); 
    latManager.add(new LabelField("Latitude: ")); 
    latitudeLbl = new LabelField(""); 
    latManager.add(latitudeLbl); 

    add(latManager); 

    HorizontalFieldManager longManager = new HorizontalFieldManager(); 
    longManager.add(new LabelField("Longitude: ")); 
    longitudeLbl = new LabelField(""); 
    longManager.add(longitudeLbl); 

    add(longManager); 
    messageField = new RichTextField(); 
    add(messageField); 
    } 

    private void update(){ 
     LocationHandler handler = new LocationHandler(this); 
     handler.start(); 
    } 

    protected void makeMenu(Menu menu, int instance) { 
     super.makeMenu(menu, instance); 
     menu.add(new MenuItem("Update", 10, 10) { 
     public void run() { 
     update(); 
     } 
     }); 
    } 

    public void setLocation(double longitude, double latitude){ 
     synchronized(UiApplication.getEventLock()){ 
      longitudeLbl.setText(Double.toString(longitude)); 
      latitudeLbl.setText(Double.toString(latitude)); 
     } 
    } 

    public void setMessage(String message){ 
     synchronized(UiApplication.getEventLock()){ 
      messageField.setText(message); 
     } 
    } 
} 

답변

2

시도를 감지합니다. 그렇다면 OS가 귀하의 기준 LP와 일치하는 것을 찾지 못하므로 다른 기준을 사용하십시오.

유효한 LP를 반환하면 여전히 provider.getLocation(-1)에서 실패 할 수 있습니다. 다양한 예외/경우가 있습니다. 자세한 내용은 API을 확인하십시오. 대부분의 경우 여기서 LocationException (위치를 검색 할 수 없거나 시간 초과 기간이 만료 된 경우) 또는 SecurityException (호출하는 응용 프로그램에 위치 정보를 쿼리 할 권한이없는 경우) 중 하나가 표시됩니다.

+0

BTW, BB GPS API는 그냥 안됐다. 해당 API를 사용하여 멋진 GPS 앱을 만드는 것은 매우 어렵습니다. OS 5 또는 6 용으로 개발할 경우 최신 BB GPS API (BlackBerry 확장 프로그램 JSR 179)의 혜택을 누릴 수 있습니다. net.rim.device.api.gps.BlackBerryLocationProvider 클래스를 확인하십시오. –

+0

답장을 보내 주셔서 감사합니다. 어떻게하면 해당 줄에서 LocationException을 확인할 수 있습니까? BTW 내가 장치에서 디버깅을했지만 시뮬레이터에서 디버깅을 시도했을 때 효과가있었습니다. – RapsFan1981

+0

try-catch 블록으로 줄을 감싸는 것으로. 시뮬레이터는 GPS를 시뮬레이트하는 나쁜 작업을합니다 : (1) 허용 모델을 무시합니다 (모든 앱 권한이 '허용'으로 설정된 것처럼 동작합니다). (2) 실제 기기에서 위치 업데이트를 즉시 제공하지만 시간이 오래 걸립니다. . –

관련 문제