×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    21

    problem in getting loaction from location api

    i want find find my application's location using location API. but i am getting null location:
    here is my code snippet


    Code:
    public void startGPS() 
    	{
             if (locationProvider == null) 
    		{
                createLocationProvider();
    
                locationThread = new Thread() 
    			{
                    public void run()
    				{
                            	createLocationListener();           	
    				}
                };
                locationThread.start();
           	}       
        } 
    	private void createLocationProvider() 
    	{
            Criteria cr = new Criteria(); 
            try 
    		{
                	locationProvider = LocationProvider.getInstance(cr);
            } 
    		catch (Exception e) 
    		{
               	midlet.log("GPS.createLocationProvider: " + e);
            }
        }
    public void providerStateChanged(LocationProvider provider, int newState) {}
    	private void createLocationListener()
    	{
              // 2cd value is interval in seconds
    		try 
    		{
                	locationProvider.setLocationListener(this, 60, -1, -1); 
            } 
    		catch (Exception e) 
    		{
               	midlet.log("GPS.createLocationListener: " + e);
            } 	
       	}
        private void getLocation(Location location)
    	{
            try 
    		{	
            	
            	QualifiedCoordinates qualifiedCoordinates=location.getQualifiedCoordinates();
            	if(qualifiedCoordinates!=null)
            	{
            		lat=qualifiedCoordinates.getLatitude();
            		lon=qualifiedCoordinates.getLongitude();
            		midlet.sendloc(lat, lon);
            	}else
            	{
            		midlet.log("Coordinates are null!");
            	}
    		}
            catch (Exception e) 
            {
                midlet.log("GPS.getLocation: " + e);
            }
    	}
        public void locationUpdated(LocationProvider provider, final Location location) 
        {
            // get new location from locationProvider
             
            try 
            {
            		Thread getLocationThread = new Thread()
            		{
            			public void run()
            			{
            				getLocation(location);
            			}	
                    };getLocationThread.start();
            } 
            catch (Exception e) 
            {
               midlet.log("GPS.locationUpdated: " + e);
            }
        }
    Last edited by wizard_hu_; 2013-03-25 at 10:08. Reason: [CODE][/CODE] - unfortunately the indents uses both spaces and tabs...

  2. #2
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,165

    Re: problem in getting loaction from location api

    having problems on reading your code, thus would not know whats wrong with it, anyway have you checked our wiki examples already: http://www.developer.nokia.com/Commu...PI_for_Java_ME there would be one for using the location API and it has worked just fine.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    21

    Re: problem in getting loaction from location api

    Quote Originally Posted by lbstracker View Post
    private void getLocation(Location location)
    {
    try
    {

    QualifiedCoordinates qualifiedCoordinates=location.getQualifiedCoordinates();
    if(qualifiedCoordinates!=null)
    {
    lat=qualifiedCoordinates.getLatitude();
    lon=qualifiedCoordinates.getLongitude();
    midlet.sendloc(lat, lon);
    }else
    {
    midlet.log("Coordinates are null!");
    }
    }
    catch (Exception e)
    {
    midlet.log("GPS.getLocation: " + e);
    }
    }
    i am getting here location coordinate as null

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem in getting loaction from location api

    You asked for location updates every 60 seconds, and you will get an update whether or not the device knows where you are. If it doesn't know where you are, you will receive a Location object that does not contain valid coordinates. Check the Location object's isValid() method to see if it should contain a valid location.

    Oh, you don't need to create so many threads. You don't need, for example, to call setLocationListener() from a separate thread. I'd recommend you follow symbianyucca's advice to look at some examples.

    If you post code, you can enclose it in "code" tags to make it format properly. If you type:

    Code:
    [code]
    public void someMethod() {
        someCode();
    }
    [/code]
    will appear as:

    Code:
    public void someMethod() {
        someCode();
    }
    Graham.
    Last edited by grahamhughes; 2013-03-24 at 11:04.

Similar Threads

  1. Problem Location.
    By aeramcm in forum Mobile Java General
    Replies: 2
    Last Post: 2010-10-13, 17:45
  2. Problem in Getting Location.....
    By furqankamani in forum Symbian
    Replies: 2
    Last Post: 2009-06-11, 08:57
  3. Location API Problem
    By martinsjulio in forum Mobile Java General
    Replies: 3
    Last Post: 2008-09-07, 22:02
  4. Location API Problem
    By sandeepsandeep in forum Mobile Java General
    Replies: 1
    Last Post: 2008-08-27, 15:59

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×