黑莓GPS测试演示

时间:2012-08-03 12:33:38

标签: blackberry gps location demo

你能帮我创建Blackberry GPS Sample Demo吗?实际上我搞砸了很多Blackberry设备,有些设备有GPS接收器而其他设备没有。

如果设备有GPS接收器,它将如何搜索当前设备位置?如果设备没有像BB 8520这样的GPS怎么办?

请分享您对Blackberry中GPS的理解,如何通过GPS获取设备位置的纬度和经度。

谢谢

2 个答案:

答案 0 :(得分:1)

试试这个,这将检查gps。如果gps可用,那么它将获得该位置,否则,它将从网络中找到该位置。

在您的班级上调用此课程 - handleGPS gps=new handleGPS();

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

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

private static class GPSThread extends Thread{
    public void run() {
        Criteria myCriteria = new Criteria();
        myCriteria.setCostAllowed(false);
        int m_bbHandle = CodeModuleManager.getModuleHandle("net_rim_bb_lbs");

        if(m_bbHandle>0){
            try {

                int cellID = GPRSInfo.getCellInfo().getCellId();
                int lac = GPRSInfo.getCellInfo().getLAC();


                String urlString2 = "http://www.google.com/glm/mmap";

                // Open a connection to Google Maps API 
                ConnectionFactory connFact = new ConnectionFactory();
                ConnectionDescriptor connDesc;
                connDesc = connFact.getConnection(urlString2);

                HttpConnection httpConn2;
                httpConn2 = (HttpConnection)connDesc.getConnection();
                httpConn2.setRequestMethod("POST");

                // Write some custom data to Google Maps API 
                OutputStream outputStream2 = httpConn2.openOutputStream();//getOutputStream();
                WriteDataGoogleMaps(outputStream2, cellID, lac);

                // Get the response  
                InputStream inputStream2 = httpConn2.openInputStream();//getInputStream();
                DataInputStream dataInputStream2 = new DataInputStream(inputStream2);

                // Interpret the response obtained 
                dataInputStream2.readShort();
                dataInputStream2.readByte();

                int code = dataInputStream2.readInt();
                //Dialog.alert(code+"");

                if (code == 0) {
                    latitude= dataInputStream2.readInt() / 1000000D;
                    longitude=dataInputStream2.readInt() / 1000000D;

                    //Dialog.alert(latitude+"-----"+longitude);  

                    dataInputStream2.readInt();
                    dataInputStream2.readInt();
                    dataInputStream2.readUTF();

                } else {
                    System.out.println("Error obtaining Cell Id ");
                }
                outputStream2.close();
                inputStream2.close();
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
            }



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

                 try {
                     Location myLocation = myLocationProvider.getLocation(300);
                     latitude  = myLocation.getQualifiedCoordinates().getLatitude();
                     longitude = myLocation.getQualifiedCoordinates().getLongitude();
                    if(latitude==0 && longitude==0){
                        try {

                            int cellID = GPRSInfo.getCellInfo().getCellId();
                            int lac = GPRSInfo.getCellInfo().getLAC();


                            String urlString2 = "http://www.google.com/glm/mmap";

                            // Open a connection to Google Maps API 
                            ConnectionFactory connFact = new ConnectionFactory();
                            ConnectionDescriptor connDesc;
                            connDesc = connFact.getConnection(urlString2);

                            HttpConnection httpConn2;
                            httpConn2 = (HttpConnection)connDesc.getConnection();
                            httpConn2.setRequestMethod("POST");

                            // Write some custom data to Google Maps API 
                            OutputStream outputStream2 = httpConn2.openOutputStream();//getOutputStream();
                            WriteDataGoogleMaps(outputStream2, cellID, lac);

                            // Get the response  
                            InputStream inputStream2 = httpConn2.openInputStream();//getInputStream();
                            DataInputStream dataInputStream2 = new DataInputStream(inputStream2);

                            // Interpret the response obtained 
                            dataInputStream2.readShort();
                            dataInputStream2.readByte();

                            int code = dataInputStream2.readInt();
                            //Dialog.alert(code+"");

                            if (code == 0) {
                                latitude= dataInputStream2.readInt() / 1000000D;
                                longitude=dataInputStream2.readInt() / 1000000D;

                                //Dialog.alert(latitude+"-----"+longitude);  

                                dataInputStream2.readInt();
                                dataInputStream2.readInt();
                                dataInputStream2.readUTF();

                            } else {
                                System.out.println("Error obtaining Cell Id ");
                            }
                            outputStream2.close();
                            inputStream2.close();
                        } catch (Exception e) {
                            System.out.println("Error: " + e.getMessage());
                        }

                    }

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


private static void WriteDataGoogleMaps(OutputStream out, int cellID, int lac)
throws IOException {
    DataOutputStream dataOutputStream = new DataOutputStream(out);
    dataOutputStream.writeShort(21);
    dataOutputStream.writeLong(0);
    dataOutputStream.writeUTF("en");
    dataOutputStream.writeUTF("Android");
    dataOutputStream.writeUTF("1.0");
    dataOutputStream.writeUTF("Web");
    dataOutputStream.writeByte(27);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(3);
    dataOutputStream.writeUTF("");

    dataOutputStream.writeInt(cellID);
    dataOutputStream.writeInt(lac);

    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.flush();
}

}

答案 1 :(得分:1)

This之前的SO帖子应该指出正确的方向。

关于其他方法,您可以使用Triangulation,其中您可以使用来自至少3个手机信号塔的信号强度来获得您的位置的粗略估计。根据您需要的位置准确度,三角测量很可能比GPS更快,但GPS将更加精确(三角测量往往具有大约100米的最佳情况误差范围)。

但请注意,如果您在建筑物内或没有清晰的天空视野,GPS很可能无法正常工作。