Here's the code I'm using trying to get GPS coordinates. It seems like every time it times out and ends up returning 0 for both the latitude and longitude even though I've both set a location in my Simulate -> GPS Location.. window and gone into the Options -> Advanced Options -> GPS -> Refresh Location.
In my main application class this function gets called from a button.
Code:
public void GetCurrentLocation() {
Criteria ct = new Criteria();
//ct.setAddressInfoRequired(false);
//ct.setAltitudeRequired(false);
ct.setCostAllowed(true);
ct.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
ct.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
ct.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
//ct.setPreferredResponseTime(10000);
//ct.setSpeedAndCourseRequired(false);
try {
myLP = LocationProvider.getInstance(ct);
myLP.setLocationListener(new myLocListener(this), -1, -1, -1);
} catch (Exception ex) {
return;
}
}
public void GotLocation(Location myLoc) {
if(myLoc != null) {
QualifiedCoordinates qc = myLoc.getQualifiedCoordinates();
//Call webservice here.
}
}
In the myLocListener.java
Code:
package net.gasbot;
import javax.microedition.location.*;
public class myLocListener implements LocationListener
{
GasBotScreen gb;
public myLocListener(GasBotScreen gb)
{
this.gb = gb;
}
public void locationUpdated(LocationProvider provider, Location location)
{
QualifiedCoordinates qc = location.getQualifiedCoordinates(); //I set a breakpoint here, but it rarely gets called, and when it does, the coordinates are both 0.
provider.setLocationListener(null, 0, 0, 0);
gb.GotLocation(location);
}
public void providerStateChanged(LocationProvider provider, int newState)
{
}
}
Can someone please let me know what I'm doing wrong? I'm setting the listener to null after it gets an updated location as I only need it once.
I've also tried using the .getLocation(-1) and it doesn't work either. Is there something wrong with my simulator? Or just something wrong with my code?