Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: Location base information

Hi,

 

As you said,  I copied from GPS DEMO source code to get longitude and latitude coordinate without GPS.

I could not get longitude and latitude.

I just copied and pasted from the demo sample code that comes with JDE except server connection part.

 

Do I need to do anything else on device?

 

Below is the code I tested:

 

 

// try to start the GPS thread that listens for updates if ( startLocationUpdate() ) { startServerConnectionThread(); // if successful, start the thread that communicates with the server }

 

 

 

 

private boolean startLocationUpdate() { boolean retval = false; try { _locationProvider = LocationProvider.getInstance(null); if ( _locationProvider == null ) { // We would like to display a dialog box indicating that GPS isn't supported, but because // the event-dispatcher thread hasn't been started yet, modal screens cannot be pushed onto // the display stack. So delay this operation until the event-dispatcher thread is running // by asking it to invoke the following Runnable object as soon as it can. Runnable showGpsUnsupportedDialog = new Runnable() { public void run() { Dialog.alert("LBS is not supported on this platform, exiting..."); System.exit( 1 ); } }; UiApplication.getUiApplication().invokeLater(showGpsUnsupportedDialog);// ask event-dispatcher thread to display dialog ASAP } else { // only a single listener can be associated with a provider, and unsetting it involves the same // call but with null, therefore, no need to cache the listener instance // request an update every second _locationProvider.setLocationListener(new LocationListenerImpl(), _interval, 1, 1); retval = true; } } catch (LocationException le) { System.err.println("Failed to instantiate the LocationProvider object, exiting..."); System.err.println(le); System.exit(0); } return retval; }

 

Listener:

private class LocationListenerImpl implements LocationListener { //members -------------------------------------------------------------- private int captureCount; private int sendCount; //methods -------------------------------------------------------------- public void locationUpdated(LocationProvider provider, Location location) { if(location.isValid()) { heading = location.getCourse(); longitude = location.getQualifiedCoordinates().getLongitude(); latitude = location.getQualifiedCoordinates().getLatitude(); altitude = location.getQualifiedCoordinates().getAltitude(); speed = location.getSpeed(); //horizontal distance to send to server float horizontalDistance = speed * _interval; _horizontalDistance += horizontalDistance; //horizontal distance for this waypoint _wayHorizontalDistance += horizontalDistance; //distance over the current interval float totalDist = 0; //moving average grade for(int i = 0; i < GRADE_INTERVAL - 1; ++i) { _altitudes[i] = _altitudes[i+1]; _horizontalDistances[i] = _horizontalDistances[i+1]; totalDist = totalDist + _horizontalDistances[i]; } _altitudes[GRADE_INTERVAL-1] = altitude; _horizontalDistances[GRADE_INTERVAL-1] = speed*_interval; totalDist= totalDist + _horizontalDistances[GRADE_INTERVAL-1]; float grade = (totalDist==0.0F)? Float.NaN : ( (_altitudes[4] - _altitudes[0]) * 100/totalDist); // running total of the vertical distance gain float altGain = _altitudes[GRADE_INTERVAL-1] - _altitudes[GRADE_INTERVAL-2]; if (altGain > 0) _verticalDistance = _verticalDistance + altGain; captureCount += _interval; //if we're mod zero then it's time to record this data captureCount %= CAPTURE_INTERVAL; // Information to be sent to the server if ( captureCount == 0 ) { //minimize garbage creation by appending only character primitives, no extra String objects created that way _messageString.append(round(longitude,4)); _messageString.append(';'); _messageString.append(round(latitude,4)); _messageString.append(';'); _messageString.append(round(altitude,2)); _messageString.append(';'); _messageString.append(_horizontalDistance); _messageString.append(';'); _messageString.append(round(speed,2)); _messageString.append(';'); _messageString.append(System.currentTimeMillis()); _messageString.append(':'); sendCount += CAPTURE_INTERVAL; _horizontalDistance = 0; } //if we're mod zero then it's time to send sendCount %= SENDING_INTERVAL; synchronized(this) { if (sendCount == 0 && _messageString.length() != 0) { if (_oldmessageString == null) { _oldmessageString = _messageString.toString(); //debug System.out.println(_oldmessageString); } else { _oldmessageString = _oldmessageString + _messageString.toString(); //debug System.out.println(_oldmessageString); } _messageString.setLength(0); //_serverConnectThread.sendUpdate(_oldmessageString); //------------ Commented out } } // Information to be displayed on the device StringBuffer sb = new StringBuffer(); sb.append("Longitude: "); sb.append(longitude); sb.append("\n"); sb.append("Latitude: "); sb.append(latitude); sb.append("\n"); sb.append("Altitude: "); sb.append(altitude); sb.append(" m"); sb.append("\n"); sb.append("Heading relative to true north: "); sb.append(heading); sb.append("\n"); sb.append("Speed : "); sb.append(speed); sb.append(" m/s"); sb.append("\n"); sb.append("Grade : "); if(Float.isNaN(grade))sb.append(" Not available"); else sb.append(grade+" %"); //FirstQueryScreen.this.updateLocationScreen(sb.toString()); } }

 

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Location base information

I think this works for carrier provided stuff on my 7130 but I have some variants and trying to pull out the piece that I think I'm using,

haven't looked lately,

 

try {
  Criteria cr=new Criteria();
  cr.setPreferredPowerConsumption(cr.POWER_USAGE_LOW);
  LocationProvider lp = LocationProvider.getInstance(cr);
    Location l = lp.getLocation(180*0+10);

  Coordinates c = l.getQualifiedCoordinates();

  if ( c != null ) {
    // use coordinate information
    double latitude  = c.getLatitude();
    double longitude = c.getLongitude();
    double  altitude  = c.getAltitude();
   return "&longitude="+longitude+"&latitude="+latitude+"&altitude="+altitude;
  }
 
  } catch (Exception e ) {
    // Not able to retrieve location information
    // This is returning a space containing string sometimes.
    //return "&gpsx="+e.toString();
   return "&gpsx="+e.getClass().getName();
  }

Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: Location base information

Hi

 

 

 

private boolean startLocationUpdate() { boolean retval = false; try { Criteria cr=new Criteria(); cr.setPreferredPowerConsumption(cr.POWER_USAGE_LOW); _locationProvider = LocationProvider.getInstance(cr); if ( _locationProvider == null ) { // We would like to display a dialog box indicating that GPS isn't supported, but because // the event-dispatcher thread hasn't been started yet, modal screens cannot be pushed onto // the display stack. So delay this operation until the event-dispatcher thread is running // by asking it to invoke the following Runnable object as soon as it can. System.out.println("LBS is not supported on this platform, exiting..."); Runnable showGpsUnsupportedDialog = new Runnable() { public void run() { Dialog.alert("LBS is not supported on this platform, exiting..."); System.exit( 1 ); } }; UiApplication.getUiApplication().invokeLater(showGpsUnsupportedDialog);// ask event-dispatcher thread to display dialog ASAP } else { // only a single listener can be associated with a provider, and unsetting it involves the same // call but with null, therefore, no need to cache the listener instance // request an update every second //_locationProvider.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);--DK System.out.println("Location provider is not null, trying to get coordinates."); try { Location l = _locationProvider.getLocation(180*0+10); Coordinates c = l.getQualifiedCoordinates(); if ( c != null ) { // use coordinate information latitude = c.getLatitude(); longitude = c.getLongitude(); altitude = c.getAltitude(); } } catch (InterruptedException e) { // TODO Auto-generated catch block Status.show("Error to get coordinate: " + e.getMessage()); //e.printStackTrace(); } retval = true; } } catch (LocationException le) { System.err.println("Failed to instantiate the LocationProvider object, exiting..."); System.err.println(le); Status.show("Error to get Location: " + le.getMessage()); //System.exit(0); } return retval; }

Anything else do I need to do?

 

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Location base information

What BlackBerry model and handheld software version are you testing on?  You can see this under Options, About.  Are you using a real BlackBerry device or the BlackBerry Simulator?

 

Do you see GPS information if you go to Options, (Advanced Options), GPS or Location Based Services and use the Refresh GPS menu item?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: Location base information

Hi Mark,

 

I'm using BlackBerry 8320 Curve, T-Mobile.

Version is v4.2.2.180(Platform 2.5.0.36.

 

I'm now testing on device but not simulator.

I see GPS Data Source as NONE.

 

I don't see Location Based Service Menu or refresh GPS menu item.

 

Thanks,

DK.

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Location base information

The BlackBerry 8320 does not have built in GPS hardware.  Have you paired the BlackBerry 8320 with a Bluetooth GPS receiver?  You'll need to do so to see this option and use the JSR 179 GPS APIs.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Location base information

What is preventing the cell tower location system from giving a location fix? That is theoretically

available without GPS and even required AFAIK for e911 location determination. What is the likelihood carrier

will make it available for a developer ( did you try approaching the developer contacts at your carrier?).

 

I don't have GPS, bluetooth or otherwise AFAIK on my 7130 and it started working after I contacted the carrier.

 

Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: Location base information

Mark,

 

But I downloaded Google Map application and that application is getting the current location information without GPS.

 

So I'm wondering how that application could get the information.

 

Thanks,

DK. 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Location base information

Google has their own cell tower database- another example of how the carrier hide stuff.

Notice the radius on the "fix" you get. It is possible that you have some location facility that java

isn't finding but the google app doesn't prove that.

Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: Location base information

Did you call your customer service of you carrier?

Then, what did you ask to them?