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
Forums Advisor I
vaishali28
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Problem in retrieving latitude and longitude of the device

Hi

I want to get my device's latitude and longitude.

I am using  the below code. But i am getting the blank screen

package package1;

import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

public class UiAppScreen extends MainScreen 
{
	private LabelField latitudeLabel; 
	private LabelField longitudeLabel; 
	private RichTextField messageField; 
	
	public final class MyScreen extends MainScreen
	{
	     private LocationProvider locationProvider;
	     private  int interval = 1;
	     double lat;
	     double longt;

	    public MyScreen()
	    {   
	        setTitle("MyTitle");
	        startLocationUpdate();          
	    }

	    private boolean startLocationUpdate()
	    {
	        boolean retval = false;

	            try
	            {
	            locationProvider = LocationProvider.getInstance(null);

	            if ( locationProvider == null )
	            {

	            Runnable showGpsUnsupportedDialog = new Runnable()
	            {
	                public void run()
	                {

	                Dialog.alert("GPS is not supported on this platform, exiting...");
	                //System.exit( 1 );
	                }
	            };

	            UiApplication.getUiApplication().invokeAndWait( showGpsUnsupportedDialog ); // Ask event-dispatcher thread to display dialog ASAP.
	            }
	            else
	            {

	            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;
	            }

	            private class LocationListenerImpl implements LocationListener
	            {

	            public void locationUpdated(LocationProvider provider, Location location)
	            {
	                    if(location.isValid())
	                    {
	                        double longitude = location.getQualifiedCoordinates().getLongitude();
	                        double latitude = location.getQualifiedCoordinates().getLatitude();

	                        updateLocationScreen(latitude, longitude);              

	                    }
	            }

	            public void providerStateChanged(LocationProvider provider, int newState)
	            {
	            }
	        }

	         private void updateLocationScreen(final double latitude, final double longitude)
	            {
	                    UiApplication.getUiApplication().invokeAndWait(new Runnable()
	                    {
	                    public void run()
	                    {
	                    lat = latitude;
	                    longt = longitude;


	                    RichTextField txt=new RichTextField();
	                    txt.setText("Long=="+longt);

	                    RichTextField txt1=new RichTextField();
	                    txt1.setText("lat=="+lat);

	                    add(txt);
	                    add(txt1);


//	                  persistentLatitude.setContents(Double.toString(latitude));
//	                  persistentLongitude.setContents(Double.toString(longitude));



	                    }
	                    });
	            }
	}
}

 

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem in retrieving latitude and longitude of the device

Please use plain text.
Forums Advisor I
vaishali28
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Problem in retrieving latitude and longitude of the device

Hi if i run my code in simulator itsworking fine i am getting the latitude and longitude.But i am not getting anything in device.

Here is the code.

uiScreen.setMessage("Getting location..."); 
			LocationProvider provider = LocationProvider.getInstance(criteria); 
			Location location = provider.getLocation(-1); 
			QualifiedCoordinates qualifiedCoordinates = location.getQualifiedCoordinates(); 
			uiScreen.setLocation(qualifiedCoordinates.getLongitude(), qualifiedCoordinates.getLatitude()); 
			String message = "Successfully got location, method:"; 
			int method = location.getLocationMethod(); 
			if((method & Location.MTA_ASSISTED) == Location.MTA_ASSISTED) 
			{ 
				message += " Assisted GPS"; 
			} 
			if((method & Location.MTA_UNASSISTED) == Location.MTA_UNASSISTED) 
			{ 
				message += " Unassisted GPS"; 
			} 
			if((method & Location.MTE_CELLID) == 
			Location.MTE_CELLID) { 
			message += " Cell Site"; 
			} 
			message += "\nHorizontal (Longitude) Accuracy: "; 
			message += qualifiedCoordinates.getHorizontalAccuracy(); 
			message += "\nVertical (Latitude) Accuracy: "; 
			message += qualifiedCoordinates.getVerticalAccuracy(); 
			uiScreen.setMessage(message); 

 

Please use plain text.
Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10

Re: Problem in retrieving latitude and longitude of the device

Check in device settings gps is enabled or not..
Please use plain text.
Forums Advisor I
vaishali28
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Problem in retrieving latitude and longitude of the device

Its enabled

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem in retrieving latitude and longitude of the device

What criteria have you specified?

 

You code looks suspiciously like it runs on the Event Thread - unless you do an invoke later, then methods like:

uiScreen.setLocation(..)

will work on the Simulator and fail on device.

 

Remember that finding a location, especially a GPS location, takes a long time for the first fix.

 

As well as trying to fix you own code. I recommend you download, compile and install the Diagnostic code that is supplied in the link I gave you. Or the SimpleLocationApi from the other link.  These will demonstrate what you can actually do on your device.  So please do that and prove that some standard code will get a location before we start trying to fix your code. 

Please use plain text.
Forums Advisor I
vaishali28
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Problem in retrieving latitude and longitude of the device

https://github.com/blackberry/Samples-for-Java/tree/master/Simple%20Location%20API

I have used the above code and used all the three modes i.e GPS,OPTIMAL and GEOLOCATION.

But still i am not getting the location.

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem in retrieving latitude and longitude of the device

As noted, test with some sample code, if you don't get locations then the problem is in the device or environment, not your code. 

 

I recommend the Diagnostic extended, as it seems to test everything. 

Please use plain text.
Forums Advisor I
vaishali28
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Problem in retrieving latitude and longitude of the device

Hi

I have tried the Diagnostic extended sample but i don't understand what the code is doing??

Please use plain text.
Forums Advisor I
vaishali28
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Problem in retrieving latitude and longitude of the device

I have tried almost each and every method to calculate latitude and longitude but still i am not getting the location.

The problem is with device??

If the problem is with the device, then isn't it possible to get the location?

I am using bb OS 7.1

Please use plain text.