0
我必须制作一个android应用程序,我需要找到用户当前位置,然后从数据库中获取一堆经纬度。在此之后,我需要找到用户当前位置和经纬度之间的距离,多头......并且对于小于200km的所有距离,我需要将它们添加到列表中。无法获取当前的地理位置android
问题: 我无法找到当前的用户位置,我总是获得0,0。我通过使用DDMS传递坐标来在模拟器上测试相同的结果。我也尝试过在实际的设备上仍然收到0,0作为经纬度。此外正在计算的距离不正确。
这里是我的代码:
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) {
}
}
我不能够明白是怎么回事了?
非常感谢您的帮助。
您的android Manifest.xml是否包含适当的授权? (http://developer.android.com/reference/android/Manifest.permission.html),你看到日志中的任何东西? – 2011-05-09 11:58:15
是的。我看到以下内容:05-09 17:48:59.877:INFO /当前loc(1573):lat 0.0 lng 0.0 05-09 17:48:59.877:INFO/checklocation loc(1573):lat 41.2834563 lng -70.0994605 – user590849 2011-05-09 12:20:10
为什么我从distanceTo方法获得距离0? – user590849 2011-05-09 12:24:43