cannot obtain current geographic location in android
i have to make an android application in which i need to find the users current location and then obtain a bunch of lat and longs from a database. After this i need to find the distance between the users current location and the lat ,longs.... and for all those distances which are less than 200km i need to add them to a list.
PROBLEM: i am not able to find the current user location and i always obtain 0,0. I have tested the same on an emulator by passing the co-ordinates using DDMS. I have also tried the same on an actual device still i receive 0,0 as the lat, long. Also the distance being calculated is incorrect.
Here is my code:
LocationManager lm;
LocationListener ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nearactivity);
mHandler= new Handler();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
showProgress();
}
private void showProgress() {
pd = new ProgressDialog(NearActivity.this);
pd.setTitle("Nautic-Dates");
pd.setMessage("Loading");
pd.show();
GetLocations gl = new GetLocations();
gl.execute();
try {
setListAdapter(new CustomeAdapter(gl.get()));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pd.dismiss();
}
public class GetLocations extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... arg0) {
HandleDatabase hb = new HandleDatabase();
String locations[] = hb.getLocationsFromDatabase();
ArrayList<String> nearlocations = new ArrayList<String>();
CustomGeoCoder cg = new CustomGeoCoder();
for (int i = 0; i < locations.length; i++) {
String details[] = locations[i].split("--");
if (details[0].trim().equals("")
|| details[1].trim().equals("")||details[0].trim().equals("null")
|| details[1].trim().equals("null")) {
continue;
} else {
float distance = cg.checkLocation(details[0], details[1]);
if (distance<RADIUS) {
nearlocations.add(locations[i]+"--"+distance);
}
}
}
return nearlocations;
}
}
public class CustomGeoCoder {
public float checkLocation(String string, String string2) {
Location locationA = new Location("pointA");
Log.i("current loc", "lat " + lat + " lng " + lng);
Log.i("checklocation loc", "lat " + string + " lng " + string2);
locationA.setLatitude(lat);
locationA.setLongitude(lng);
Location locationB = new Location("pointB");
try {
locationB.setLatitude(Double.parseDouble(string));
} catch (NumberFormatException e) {
try {
locationB.setLatitude(Float.parseFloat(string));
} catch (NumberFormatException e1) {
try {
locationB.setLatitude(Integer.parseInt(string));
} catch (NumberFormatException e2) {
e2.printStackTrace();
return RADIUS+1;
}
}
}
try {
locationB.setLongitude(Double.parseDouble(string2));
} catch (NumberFormatException e) {
try {
locationB.setLongitude(Float.parseFloat(string2));
} catch (NumberFormatException e1) {
try {
locationB.setLongitude(Integer.parseInt(string2));
} catch (NumberFormatException e2) {
e2.printStackTrace();
return RADIUS+1;
}
}
}
float distance = locationA.distanceTo(locationB);
//this code is returning wrong values.
return distance/1000;
// Log.i("distance", "" + distance);
// if (distance / 1000 > RADIUS)
// return false;
// else
// return true;
}
}
private class mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
lat = location.getLatitude();
lng = location.getLongitude();
Toast.makeText(NearActivity.this,"lng="+lng+" lat="+lat, Toast.LENGTH_SHORT);
showProgress();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
i am not able to understand what is going wrong?
thank you in advance for your he开发者_StackOverflow中文版lp.
I've loaded your code in Eclipse, and although I haven't got it to run, because I obviously don't have your HandleDatabase
class code, nor your CustomAdapter
. However, what struck me, is it appears you're reusing using lat and lon as object members for the Activity, or you've not defined them as variables within the methods they're used. I suggest breaking up your code into self contained units which people here could more easily debug for you.
精彩评论