04-17-2009 02:27 AM
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)).getAddress
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(Addr
else
System.out.println("country_code VALUE IS "+country_code);
String district = address.getField(AddressInfo.DISTRICT);
if(district != null)
f.append("\nDISTRICT->"+address.getField(AddressI
else
System.out.println("district VALUE IS "+district);
String postalcode = address.getField(AddressInfo.POSTAL_CODE);
if(postalcode != null)
f.append("\nPOSTAL_CODE->"+address.getField(Addre
else
System.out.println("postalcode VALUE IS "+postalcode);
String street = address.getField(AddressInfo.STREET);
if(street != null)
f.append("\nSTREET->"+address.getField(AddressInf
else
System.out.println("street VALUE IS "+street);
String state = address.getField(AddressInfo.STATE);
if(state != null)
f.append("\nSTATE->"+address.getField(AddressInfo
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,
}
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.
04-17-2009 02:34 AM
NullPointerException indicates some object is null.
To test GPS on Simulator you can use the On Simulator Select Simulate -> GPS Location.
04-17-2009 02:44 AM
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
04-17-2009 02:51 AM
04-17-2009 02:54 AM
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.
04-17-2009 02:58 AM
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.
04-17-2009 03:02 AM
Did you get a chance to go thru this Developer Doc?
http://na.blackberry.com/eng/deliverables/5814/GPS
04-17-2009 06:34 AM
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.
04-17-2009 06:40 AM
04-17-2009 07:10 AM
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.