1

I need to get the users latitude and longitude to display data in increasing order of distance.

I am using 2 phones in 2 different countries to test the app. It works fine with a bb bold 9700 when used in south asia. But does not with a bb 9650 when used in nyc. I tried using the bb gps api based classes and also google tower based gps classes.

Both don't seem to work in nyc with bb 9650.I used other location based apps like yelp etc which work perfectly.

Attaching both the codes

Phone GPS

public class GPS_Location { private String log; 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);
                            }
                    }
            }
    } 

}

cell tower google service

public class 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.horizontal_accuracy = locationObject // .getDouble("accuracy"); // 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;
            }
    }

}

1 Answer 1

0

Ideally you should be using multiple fix methods (your Criteria) to gather GPS information. Using just the default will not work in all circumstances, so you need to have fallback options. Here are the Criteria, in preferred order, that I use in the States and seems to do well. You just have to loop through them until you have a set that works.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.