2011-05-19 54 views
1

我需要獲取用戶的緯度和經度,以按距離遞增的順序顯示數據。黑莓無法獲取經緯度

我使用2個不同國家的2部手機來測試該應用程序。在南亞使用時,它可以與bb大膽的9700配合使用。但是在nyc中使用bb 9650時不會。 我嘗試使用基於bb gps api的課程以及谷歌塔基gps課程。

這兩個似乎都不能用nbc 9650工作。我使用其他基於位置的應用程序,如yelp等,完美的工作。

附加兩個碼

手機GPS

公共類GPS_Location { 私人字符串日誌; double longi; double lati;

public GPS_Location() 
    { 
      new LocationTracker(); 
    } 
    public boolean onClose() 
    { 
      Application.getApplication().requestBackground();  
      return false; 
    } 


    class LocationTracker extends TimerTask 
    { 

      private Timer timer; 
      private LocationProvider provider; 
      Criteria cr; 

      public LocationTracker() 
      { 
        timer = new Timer(); 
        cr= new Criteria(); 
        resetGPS(); 

        timer.schedule(this, 0, 60000); 
      } 

      public void resetGPS() 
      { 

        try 
        { 
          provider = LocationProvider.getInstance(cr); 
          if(provider != null) 
          { 
            /*provider.setLocationListener(null, 0, 0, 0); 
      provider.reset(); 
      provider = null;*/ 
            provider.setLocationListener(new MyLocationListener(), 3, -1, -1); 
          } 

          //provider = LocationProvider.getInstance(null); 

        } catch(Exception e) 
        { 

        } 
      } 


      public void run() 
      { 
        System.out.println("********************"); 
      } 

      private class MyLocationListener implements LocationListener 
      { 
        public void locationUpdated(LocationProvider provider, Location location) 
        { 
          if(location != null && location.isValid()) 
          {    
            QualifiedCoordinates qc = location.getQualifiedCoordinates(); 

            try 
            { 
              lati = location.getQualifiedCoordinates().getLatitude(); 
              System.out.println("********************latitude :: "+lati); 
              longi = location.getQualifiedCoordinates().getLongitude(); 
              System.out.println("********************longitude ::"+longi); 
              CustomSession.getInstance().setLatitude(lati); 
              CustomSession.getInstance().setLongitude(longi); 
            } 
            catch(Exception e) 
            { 

            } 
          } 

        } 

        public void providerStateChanged(LocationProvider provider, int newState) 
        { 
          //LocationTracker.this.resetGPS(); 
          if(newState == LocationProvider.TEMPORARILY_UNAVAILABLE) 
          { 
            provider.reset(); 
            provider.setLocationListener(null, 0, 0, -1); 
          } 
        } 
      } 
    } 

}

小區塔谷歌服務

公共類JsonGenerator {

public void locating() throws IOException{ 
      byte[] postData = getGPSJsonObject().toString().getBytes(); 
      JSONObject jsonObject = null; 
      HttpConnection gpsConnection; 
      DataOutputStream os; 
      DataInputStream dis; 
      String gpsString = retrunURLString("http://www.google.com/loc/json"); 
      try { 
        gpsConnection = (HttpConnection) Connector.open(gpsString); 
        gpsConnection.setRequestMethod(HttpConnection.POST); 
        gpsConnection.setRequestProperty(
            HttpProtocolConstants.HEADER_CONTENT_LENGTH, String 
                .valueOf(postData.length)); 
        gpsConnection.setRequestProperty(
            HttpProtocolConstants.HEADER_CONTENT_TYPE, 
            "application/requestJson"); 
        os = gpsConnection.openDataOutputStream(); 
        os.write(postData); 
        int rc = gpsConnection.getResponseCode(); 
        if (rc != HttpConnection.HTTP_OK) { 
          return; 
        } 
        dis = gpsConnection.openDataInputStream(); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        int j = 0; 
        while ((j = dis.read()) != -1) { 
          baos.write(j); 
        } 
        byte[] data = baos.toByteArray(); 
        String jsonString = new String(data); 
        try { 
          jsonObject = new JSONObject(jsonString); 
        } catch (JSONException e) { 
          e.printStackTrace(); 
        } 
        JSONObject locationObject = jsonObject.getJSONObject("location"); 
        if (locationObject.getDouble("latitude") != 0.0 
            && locationObject.getDouble("longitude") != 0.0) { 
          System.out.println("Latitute is =================::::"+locationObject.getDouble("latitude")); 
          System.out.println("Llongitude is =================::::"+locationObject.getDouble("longitude")); 
          CustomSession.getInstance().setLatitude(locationObject.getDouble("latitude")); 
          CustomSession.getInstance().setLongitude(locationObject.getDouble("longitude")); 

// Global.horizo​​ntal_accuracy = locationObject // .getDouble( 「精度」) ; // Global.locAvailable = true; } } catch(JSONException e){TODO:handle exception e.printStackTrace(); }

} 

    public JSONObject getGPSJsonObject() { 
      JSONObject jsonString = new JSONObject(); 

      try { 
        jsonString.put("version", "1.1.0"); 
        jsonString.put("host", "maps.google.com"); 
        int x = RadioInfo.getMCC(RadioInfo.getCurrentNetworkIndex()); 
        jsonString.put("home_mobile_country_code", Integer.parseInt(Integer 
            .toHexString(x))); 
        jsonString.put("home_mobile_network_code", RadioInfo 
            .getMNC(RadioInfo.getCurrentNetworkIndex())); 

        int radio = RadioInfo.getNetworkType(); 
        if(radio==RadioInfo.NETWORK_CDMA){ 
          jsonString.put("radio_type", "cdma"); 
        } 
        else{ 
          jsonString.put("radio_type", "gsm"); 
        } 
        jsonString.put("carrier", RadioInfo.getCurrentNetworkName()); 
        jsonString.put("request_address", true); 
        jsonString.put("address_language", "en_GB"); 

        CellTower cellInfo = new CellTower(Integer.toHexString(x), GPRSInfo 
            .getCellInfo().getLAC(), GPRSInfo.getCellInfo().getRSSI(), 
            GPRSInfo.getCellInfo().getCellId(), 0, RadioInfo 
                .getMNC(RadioInfo.getCurrentNetworkIndex())); 
        Hashtable map = new Hashtable(); 
        map.put("mobile_country_code", new Integer(Integer 
            .parseInt(cellInfo.mobileCountryCode))); 
        map.put("location_area_code", 
            new Integer(cellInfo.locationAreaCode)); 
        map.put("signal_strength", new Integer(cellInfo.signalStrength)); 
        map.put("cell_id", new Integer(cellInfo.cellID)); 
        map.put("age", new Integer(0)); 
        map.put("mobile_network_code", new Integer(
            cellInfo.mobileNetworkCode)); 

        JSONArray array = new JSONArray(); 
        array.put(0, map); 

        jsonString.put("cell_towers", array); 
      } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
      return jsonString; 
    } 

    public static String retrunURLString(String url) { 
      String urlString = null; 
      if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { 
        // WIFI 
        urlString = url + ";interface=wifi"; 
      } else { 
        int coverageStatus = CoverageInfo.getCoverageStatus(); 
        ServiceRecord record = getWAP2ServiceRecord(); 
        if (record != null 
            && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) { 
          // WAP 2.0 
          urlString = url + ";deviceside=true;ConnectionUID=" 
              + record.getUid(); 
        } else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) { 
          // BES/MDS 
          urlString = url + ";deviceside=false"; 
        } else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) { 
          // Direct TCP/IP 
          urlString = url + ";deviceside=true"; 
        } else if ((coverageStatus & CoverageInfo.COVERAGE_BIS_B) == CoverageInfo.COVERAGE_BIS_B) { 
          // BIS 
          urlString = url + ";deviceside=false;ConnectionUID=" 
              + record.getUid(); 
        } 
      } 
      return urlString; 
    } 

    protected static ServiceRecord getWAP2ServiceRecord() { 
      ServiceBook sb = ServiceBook.getSB(); 
      ServiceRecord[] records = sb.getRecords(); 
      for (int i = 0; i < records.length; i++) { 
        String cid = records[i].getCid().toLowerCase(); 
        String uid = records[i].getUid().toLowerCase(); 
        if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1 
            && uid.indexOf("mms") == -1) { 
          return records[i]; 
        } 
      } 
      return null; 
    } 

    private class CellTower { 
      public String mobileCountryCode; 
      public int locationAreaCode; 
      public int signalStrength; 
      public int cellID; 
      public int age; 
      public int mobileNetworkCode; 

      private CellTower(String mcc, int lac, int ss, int ci, int a, int mnc) { 
        mobileCountryCode = mcc; 
        locationAreaCode = lac; 
        signalStrength = ss; 
        cellID = ci; 
        age = a; 
        mobileNetworkCode = mnc; 
      } 
    } 

}

回答

0

理想情況下,你應該使用多種方法修復(你的標準)來收集GPS信息。在任何情況下僅使用默認值都不起作用,因此您需要有後備選項。以下是按照優先順序排列的標準,我在美國使用並似乎表現良好。你只需要遍歷它們,直到你有一套工作。

//Speed optimal 
    BlackBerryCriteria speed = new BlackBerryCriteria(); 
    speed.setHorizontalAccuracy(50); 
    speed.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH); 
    speed.setCostAllowed(true); 
    speed.setPreferredResponseTime(10000); 

    //MS-Based 
    BlackBerryCriteria msBased = new BlackBerryCriteria(); 
    msBased.setPreferredPowerConsumption(BlackBerryCriteria.POWER_USAGE_MEDIUM); 
    msBased.setHorizontalAccuracy(50); 
    msBased.setVerticalAccuracy(50); 
    msBased.setCostAllowed(true); 
    msBased.setPreferredResponseTime(10000); 

    //Assisted mode 
    BlackBerryCriteria assisted = new BlackBerryCriteria(); 
    assisted.setPreferredPowerConsumption(BlackBerryCriteria.POWER_USAGE_HIGH); 
    assisted.setHorizontalAccuracy(50); 
    assisted.setVerticalAccuracy(50); 
    assisted.setCostAllowed(true); 
    assisted.setPreferredResponseTime(10000); 

    //Autonomous 
    BlackBerryCriteria autonomous = new BlackBerryCriteria(); 
    autonomous.setPreferredPowerConsumption(BlackBerryCriteria.POWER_USAGE_MEDIUM); 
    autonomous.setHorizontalAccuracy(BlackBerryCriteria.NO_REQUIREMENT); 
    autonomous.setVerticalAccuracy(BlackBerryCriteria.NO_REQUIREMENT); 
    autonomous.setCostAllowed(true); 
    autonomous.setPreferredResponseTime(180000); 

    //Cell site 
    BlackBerryCriteria cell = new BlackBerryCriteria(); 
    cell.setPreferredPowerConsumption(BlackBerryCriteria.POWER_USAGE_LOW); 
    cell.setHorizontalAccuracy(BlackBerryCriteria.NO_REQUIREMENT); 
    cell.setVerticalAccuracy(BlackBerryCriteria.NO_REQUIREMENT); 
    cell.setCostAllowed(true); 
    cell.setPreferredResponseTime(180000);