无法获取android中的当前地理位置

时间:2011-05-09 11:47:13

标签: android

我必须制作一个Android应用程序,在其中我需要找到用户的当前位置,然后从数据库中获取一堆lat和long。在此之后,我需要找到用户当前位置和lat之间的距离,longs ....对于所有那些小于200km的距离,我需要将它们添加到列表中。

问题:  我无法找到当前的用户位置,我总是获得0,0。我通过使用DDMS传递坐标在仿真器上测试了相同的内容。我也在实际设备上尝试了同样的功能,但仍然收到0,0作为lat,long。计算的距离也是不正确的。

这是我的代码:

    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) {
    }
}

我无法理解出了什么问题?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我已经在Eclipse中加载了你的代码,虽然我还没有运行它,因为我显然没有你的HandleDatabase类代码,也没有你的CustomAdapter。然而,令我印象深刻的是,你似乎正在重复使用lat和lon作为Activity的对象成员,或者你没有将它们定义为它们所使用的方法中的变量。我建议将你的代码分解为自包含的单元,这里的人们可以更轻松地为你调试。