黑莓 - 从GPS,网络服务或手机信号塔获取位置

时间:2012-08-15 08:24:45

标签: blackberry location coordinates

我正试图在BlackBerry中获取位置。我可以通过GPS或Google Web服务成功获得该位置。如果GPS不可用(在建筑物内)或手机不支持,则通过Google Web服务获取位置。但是,我想更新此类以执行以下操作:

1)检查GPS是否可用 - >从GPS获取位置 2)否则检查互联网是否可用 - > (是)从Google Webservice获取位置 3)(否)从移动网络蜂窝塔获取位置 4)在给定间隔(10-20秒)后连续更新位置

请帮助。

public class GPSHandler { 

       private GPSThread _gpsThread;
       private Coordinates _location;
       private boolean _gotLocation;
       private GPSListener _listener;

       /** this class will be a Singleton, as the device only has one GPS system */
       private static GPSHandler _instance;

       /** @return the Singleton instance of the GPSHandler */
       public static GPSHandler getInstance() {
          if (_instance == null) {
             _instance = new GPSHandler();
          }
          return _instance;
       }

       /** not publicly accessible ... use getInstance() */
       private GPSHandler() { 
          _gpsThread = new GPSThread();
          // NOTE: you might not want to call start() unless you have a listener.
          //  Otherwise, just make sure to handle the case where the location is
          //  received before setListener() is called.
          _gpsThread.start(); 
       }

       public void setListener(GPSListener listener) {
          // only supports one listener this way
          _listener = listener;   
       }

       private void setLocation(final Coordinates value) {
          _location = value;
          if (value.getLatitude() != 0.0 || value.getLongitude() != 0.0) {
             _gotLocation = true;
             if (_listener != null) {
                // this assumes listeners are UI listeners, and want callbacks on the UI thread:
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                   public void run() {
                      _listener.onLocationReceived(value);
                   }
                });
             }
          }
       }

       private class GPSThread extends Thread {

          private void getLocationFromGoogle() {
             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(); 

                final int code = dataInputStream2.readInt(); 
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                   public void run() {
                      Dialog.alert(code + "");   
                   }
                });                        

                if (code == 0) { 
                   final double latitude = dataInputStream2.readInt() / 1000000D; 
                   final double longitude = dataInputStream2.readInt() / 1000000D; 
                   setLocation(new Coordinates(latitude, longitude, 0.0f));

                   UiApplication.getUiApplication().invokeLater(new Runnable() {
                      public void run() {
                         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()); 
             } 
          }

          private void tryGetLocationFromDevice() {
             _gotLocation = false;
             try {
                Criteria myCriteria = new Criteria(); 
                myCriteria.setCostAllowed(false); 
                LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria); 

                try { 
                   Location myLocation = myLocationProvider.getLocation(300); 
                   setLocation(myLocation.getQualifiedCoordinates()); 
                } catch ( InterruptedException iex ) { 
                   System.out.println(iex.getMessage());
                } catch ( LocationException lex ) { 
                   System.out.println(lex.getMessage());
                } 
             } catch ( LocationException lex ) { 
                System.out.println(lex.getMessage());
             }

             if (!_gotLocation) { 
                getLocationFromGoogle();
             } 
          }

          public void run() { 
             int bbMapsHandle = CodeModuleManager.getModuleHandle("net_rim_bb_lbs");    // OS 4.5 - 6.0
             int bbMapsHandle60 = CodeModuleManager.getModuleHandle("net_rim_bb_maps"); // OS 6.0
             if (bbMapsHandle > 0 || bbMapsHandle60 > 0) { 
                tryGetLocationFromDevice();
             } else {
                getLocationFromGoogle();
             }
             return; 
          } 
       } 

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

    } 

0 个答案:

没有答案