黑莓j2me定位服务

时间:2011-11-08 18:31:39

标签: blackberry java-me geolocation

我正在用J2ME编写我的第一个BB应用程序。我找到了一个代码片段,描述了如何获取GPS坐标。我得到一个空指针异常(在手机上),但在模拟器上没有,我不知道为什么。

我将不胜感激。

以下代码:

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

1 个答案:

答案 0 :(得分:0)

使用此代码

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;
            }
        }
    }

然后在你的主课上,调用上面的课程

    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
           }