2011-11-08 2 views
0

저는 J2ME으로 첫 번째 BB 앱을 쓰고 있습니다. GPS 좌표를 가져 오는 방법을 설명하는 코드 스 니펫을 발견했습니다. 나는 널 포인터 예외 (전화에서)를 얻고 있지만 에뮬레이터에는 아무 것도없고 확실하지 않다.블랙 베리 J2ME 위치 서비스

어떤 도움을 주셔서 감사합니다.

코드 아래 : 메인 클래스의

public class handleGPS{ 
     static GPSThread gpsThread; 
     public static double latitude ; 
     public static double longitude; 

     public handleGPS(){ 
      gpsThread = new GPSThread(); 
      gpsThread.start(); 
     } 

     private static class GPSThread extends Thread{ 
      public void run() { 
       Criteria myCriteria = new Criteria(); 
       myCriteria.setCostAllowed(false); 

       try { 
        LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria); 

        try { 
         Location myLocation = myLocationProvider.getLocation(300); 
         latitude = myLocation.getQualifiedCoordinates().getLatitude(); 
         longitude = myLocation.getQualifiedCoordinates().getLongitude(); 
         System.out.print("latitude= "+latitude+" longitude="+longitude); 


        } 
        catch (InterruptedException iex) { 
         return; 
        } 
        catch (LocationException lex) { 
         return; 
        } 
       }catch (LocationException lex) { 
        return; 
       } 
       return; 
      } 
     } 
    } 

다음

try 
{ 
    // Set criteria for selecting a location provider: 
    Criteria cr= new Criteria(); 
    cr.setCostAllowed(true); 
    cr.setSpeedAndCourseRequired(true); 
    // Get an instance of the provider 
    LocationProvider lp= LocationProvider.getInstance(cr); 

    // Request the location, setting a 60 second timeout 
    Location l = lp.getLocation(300); //always times out 
    Coordinates c = l.getQualifiedCoordinates(); 
    double longitude = 0; 
    double latitude = 0; 
    float course = l.getCourse(); 
    float speed = l.getSpeed(); 
    long timestamp = l.getTimestamp();  
    if(c != null) 
    { 
     // Use coordinate information 
     latitude = c.getLatitude(); 
     longitude = c.getLongitude(); 
    } 
    System.out.println("Lon" + longitude + " Lat "+ latitude + " course "+course+" speed "+speed+" timestamp "+timestamp); 
} 
catch(LocationException le) 
{ 
    System.out.println("Location exception "+le); 
} 
catch(InterruptedException ie) 
{ 
    System.out.println("Interrupted exception "+ie); 
} 
+0

여기에없는 사항은 여러 가지가있을 수 있습니다 무엇을 전화 시간; 하늘에 대한 명확한 전망을 가지고 있는가? 언제 마지막으로 GPS를 찾았습니까? GPS가 잠시 차가운 상태이거나 하늘이 좋지 않은 경우 60 초가 짧습니다. IIRC 시뮬레이터는 LBS를 특별히 비활성화하지 않는 한 해당 위치가 0,0 임에도 불구하고 즉시 위치를 반환합니다. – Richard

+0

음, Richard에게 감사드립니다. 결국 옵션 -> 장치 -> 위치 설정으로 이동하여 위치 서비스를 켜서 문제를 없앴습니다. Location 서비스가 꺼져있는 경우, Midlet은 java.lang.NullPointerException을 발생시킵니다. 전화는 9780 btw입니다. 감사합니다 루벤 – Luben

답변

0

사용이 코드, 위의 클래스를 호출

handleGPS handleGPS=new handleGPS(); 
    int m_bbHandle = CodeModuleManager.getModuleHandle("net_rim_bb_lbs"); 
     if(m_bbHandle>0){ 

     Dialog.alert("GPS not found"); 
     } 
     else{ 
      Dialog.alert("GPS found"); 
      //your code 
      } 
+0

감사합니다. Rez, 오늘 나중에이 코드를 사용해 보겠습니다 ... – Luben