我需要找到当前位置和另一个以字符串形式存在的位置之间的距离。我正在使用以下代码

public class NearestGarageActivity extends ListActivity implements
    LocationListener {

private ProgressDialog pd;
private ArrayList<Garage> ag;
private ArrayList<NearGarage> ang;
private ArrayList<Double> al;
private Location loc;
private NearAdapter ca;
private HashMap<Double, Garage> hm;
final private Handler handler = new Handler();
private Runnable finishedLoadingListTask = new Runnable() {
    public void run() {
        finishedLoadingList();
    }
};
private double[] af;
private Button mapbut;
private LocationManager locationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.nearestgarage);
    pd = ProgressDialog.show(NearestGarageActivity.this,
            getString(R.string.app_name),
            getString(R.string.loading_list_of_garages));
    setLocation();
    Thread t = new Thread() {
        public void run() {
            loadGarageList();
            handler.post(finishedLoadingListTask);
        }
    };

    t.start();
    setupview();
}

private void setLocation() {
    // TODO Auto-generated method stub
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            0, 5, this);
}

protected void finishedLoadingList() {
    // TODO Auto-generated method stub
    setListAdapter(ca);
    pd.dismiss();
}

protected void loadGarageList() {
    // TODO Auto-generated method stub
    String str = new String(
            "http://www.internfair.internshala.com/internFiles/AppDesign/GarageList.xml");
    Parse p = new Parse(str);
    ag = p.getAg();
    calDistance();
    createNearGarage();
    ca = new NearAdapter(ang, this);
}

private void createNearGarage() {
    // TODO Auto-generated method stub

    int i = 0;try{
    for (Double f : al) {
        af[i] = f;
        i++;
    }
    Arrays.sort(af);
    for (double f : af) {
        NearGarage ng = new NearGarage();
        Garage g = new Garage();
        g = hm.get(f);
        ng.setG(g);
        ng.setD(f);
        ang.add(ng);
    }}catch(Exception e){
        e.printStackTrace();
        System.out.println("exception");
    }
}

private void calDistance() {
    // TODO Auto-generated method stub
    for (Garage g : ag) {
        String add = g.getA().getStreet() + " " + g.getA().getCity() + " "
                + g.getA().getState();
        Geocoder geo = new Geocoder(this);
        List<Address> addresses;
        try {
            addresses = geo.getFromLocationName(add, 1);
            if(addresses.size()==0){
                System.out.println(" 0");
                continue;
            }else{
            Address a = addresses.get(0);
            System.out.println(a.getCountryName());
            int MILLION = 1000000;
            int EARTH_RADIUS_KM = 6371;
            double lat1 = loc.getLatitude() / MILLION;// latitude of
                                                        // location 1
            double lon1 = loc.getLongitude() / MILLION; // longitude of
                                                        // location 1
            double lat2 = a.getLatitude() / MILLION; // latitude of location
                                                        // 2
            double lon2 = a.getLongitude() / MILLION;// longitude of
                                                        // location 2

            double lat1Rad = Math.toRadians(lat1);
            double lat2Rad = Math.toRadians(lat2);
            double deltaLonRad = Math.toRadians(lon2 - lon1);

            double dist = Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad)
                    + Math.cos(lat1Rad) * Math.cos(lat2Rad)
                    * Math.cos(deltaLonRad))
                    * EARTH_RADIUS_KM;
            al.add(dist);
            hm.put(dist, g);}
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    loc = location;
}


我在double lat1 = loc.getLatitude();处出现空指针异常。解析代码中的Done已成功执行,如何解决?

最佳答案

只需通过以下方式计算距离:

Double distance = (double)loc.distanceTo(a);

10-07 19:15
查看更多