Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
manukumar
Posts: 57
Registered: 04-14-2009

How to get the current location of the emulator

Hi friends,

 

I want to write a Location based application which displays the current location of the emulator.

 

Here is the code which I have written:

 

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.location.*;

 

 

/**

 * 

 */

public class LocationExample extends MIDlet implements LocationListener, CommandListener//,ProximityListener

{

   public LocationExample() {    }

    

    Form f;

    Command showLocation;

    Command Exit;

    

    LocationProvider lp;

    Location m_currLoc;

    Criteria cr;

    AddressInfo address;

    

    public void startApp()

    {

        f = new Form("LocationExample");

        showLocation = new Command("ShowLocation",Command.SCREEN,0);

        Exit = new Command("Exit",Command.EXIT,1);

        f.addCommand(showLocation);

        f.addCommand(Exit);

        f.setCommandListener(this);

        Display.getDisplay(this).setCurrent(f);

    }

    

    public void pauseApp()

    {

        

    }

    

    public void destroyApp(boolean unconditional)

    {

        

    }

    

    public void locationUpdated(LocationProvider provider, Location location) 

    {

        System.out.println("locationUpdated() called!!!");

       

        try

        {

            System.out.println("location in locationUpdated() is : "+location);

            //System.out.println("LOCATION USING PROVIDER:"+((provider.getLocation(120)).getAddressInfo()).getField(AddressInfo.CITY));

            

            m_currLoc = location;

            

            QualifiedCoordinates qc = location.getQualifiedCoordinates();

      

      System.out.println(  "Lat: " + qc.getLatitude() + "\n" +

        "Lon: " + qc.getLongitude() + "\n" +

        "Alt: " + qc.getAltitude() + "\n");

      

            

            

            if(m_currLoc != null)

            {

                System.out.println("m_curLoc is not null!!!");

                address = m_currLoc.getAddressInfo();

                if(address != null)

                {

                    String city = address.getField(AddressInfo.CITY);

                    if(city != null)

                    f.append("CITY->"+city);

                    else

                    System.out.println("CITY VALUE IS "+city);

                    

                    String country = address.getField(AddressInfo.COUNTRY );

                    if(country != null)

                    f.append("\nCOUNTRY->"+country);

                    else

                    System.out.println("nCOUNTRY VALUE IS "+country);

                    

                    String country_code = address.getField(AddressInfo.COUNTRY_CODE);

                    if(country_code != null)

                    f.append("\nCOUNTRY_CODE->"+address.getField(AddressInfo.COUNTRY_CODE ));

                    else

                    System.out.println("country_code VALUE IS "+country_code);

                    

            

                    String district = address.getField(AddressInfo.DISTRICT);

                    if(district != null)

                    f.append("\nDISTRICT->"+address.getField(AddressInfo.DISTRICT ));

                    else

                    System.out.println("district VALUE IS "+district);

                    

            

                    String postalcode = address.getField(AddressInfo.POSTAL_CODE);

                    if(postalcode != null)

                    f.append("\nPOSTAL_CODE->"+address.getField(AddressInfo.POSTAL_CODE ));

                    else

                    System.out.println("postalcode VALUE IS "+postalcode);

                    

                    String street = address.getField(AddressInfo.STREET);

                    if(street != null)

                    f.append("\nSTREET->"+address.getField(AddressInfo.STREET ));

                    else

                    System.out.println("street VALUE IS "+street);

                    

                    String state = address.getField(AddressInfo.STATE);

                    if(state != null)

                    f.append("\nSTATE->"+address.getField(AddressInfo.STATE ));

                    else

                    System.out.println("state VALUE IS "+state);

                }

                else

                f.append("ADDRESS INFO IS NULL!!!");

            }

            else

            f.append("LOCATION IS NULL");

        }

        catch(Exception e)

        {

            System.out.println("GOT EXCEPTION AT GETTING THE ADDRESS VALUES:"+e);

        }

    }

    

    public void providerStateChanged(LocationProvider provider,int newState)

    {

        System.out.println("providerStateChanged() called!!!");

    }

    

    public void commandAction(Command c, Displayable s)

    {

        if(c == showLocation)

        {

            String location_version = System.getProperty("microedition.location.version");

            System.out.println("LOCATION VERSION IS:"+location_version);

            if(location_version != null)

            {

                System.out.println("commandAction() called!!!");

                startFindingCurrLocation();

            }

        }

        else

        if(c == Exit)

        {

            lp.setLocationListener(null, -1, -1, -1);

            this.notifyDestroyed();

        }

    }

    

    public void startFindingCurrLocation()

    {

        Runnable r = new Runnable()

        {

            public void run()

            {

                try

                {

lp = LocationProvider.getInstance(null);

lp.setLocationListener(LocationExample.this,30,-1,-1);

}

catch(Exception e)

{

System.out.println("Exception got at getting location:"+e); e.printStackTrace();

}

   }

};

new Thread(r).start();

   }

 

But it didn't worked out for me. I am getting NullPointerException in locationUpdate( )

 

I am using the 8130-JDE emulator. 

 

Am I missing some thing.

 

Can any one please help me. 

 

Thanks,

Manoj. 

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: 07-15-2008

Re: How to get the current location of the emulator

NullPointerException indicates some object is null.

 

To test GPS on Simulator you can use the On Simulator Select Simulate -> GPS Location.


Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
manukumar
Posts: 57
Registered: 04-14-2009

Re: How to get the current location of the emulator

I already selected GPS Location from the sumulate and provided required info. 

 

Still the problem persists. 

 

The output of the above app is:

 

LOCATION VERSION IS:1.0.1

commandAction() called!!!

locationUpdated() called!!!

location in locationUpdated() is : javax.microedition.location.Location@45d8e693

GOT EXCEPTION AT GETTING THE ADDRESS VALUES:java.lang.NullPointerException 

 

What should I do?

 

Thanks,

Manoj 

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: 07-15-2008

Re: How to get the current location of the emulator

With JDE there is a "gpsdemo", you can check for the implementation.

Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
manukumar
Posts: 57
Registered: 04-14-2009

Re: How to get the current location of the emulator

The info I provided for GPS Location are:

Name: hyderabad

latitude: 17.123211

longitude: 78.23243

Altitude: 0

Satellites: 3

 

but my problem didn't solved.

 

Thanks,

Manoj. 

 

Please use plain text.
Developer
manukumar
Posts: 57
Registered: 04-14-2009

Re: How to get the current location of the emulator

Now it works fine for me if I set the altitude to 10.

 

But AddressInfo is becoming null.

 

How can I get the geo location data, like city name, country name, country code...etc.

 

Thanks,

Manoj. 

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: 07-15-2008

Re: How to get the current location of the emulator

Did you get a chance to go thru this Developer Doc?

 

http://na.blackberry.com/eng/deliverables/5814/GPS_and_BlackBerry_Maps-4.7.0-US.pdf


Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
manukumar
Posts: 57
Registered: 04-14-2009

Re: How to get the current location of the emulator

I didn't find answer to my question: given a latitude and longitude, getting the address info like city name, country name, postal code...etc.

 

Can any one please help me how to get the addressInfo for a given latitude and longitude.

 

I am testing on 8130-JDE simulator. 

 

Thanks,

Manoj. 

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: 07-15-2008

Re: How to get the current location of the emulator

Write a web service which will take lat/lon and get you the details. Find if any webservices available on web.

Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
manukumar
Posts: 57
Registered: 04-14-2009

Re: How to get the current location of the emulator

Hi BBDeveloper,have you tried my app which I have posted in this thread. if not can you please try and let me know whether it is working on real device or not. 

 

Thanks,

Manoj. 

Please use plain text.