0

我试图通过比较几个目标位置(用户位置-> A 目标与用户位置-> B 目标)之间的距离来找到距用户位置最近的位置,但我无法制作正确的代码。有人可以帮助我吗?这是我的代码:

……

public Location geoLat(GeoPoint userGeopoint)
{
    Location location = new Location("");
    location.setLatitude(userGeopoint.getLatitudeE6()/1E6);
    return location;
}

public Location geoLng(GeoPoint userGeopoint)
{
    Location location = new Location("");
    location.setLatitude(userGeopoint.getLongitudeE6()/1E6);
    return location;
}

public GeoPoint userGeopoint(Location location)
{
    Double userLat = location.getLatitude()*1E6;
    Double userLng = location.getLongitude()*1E6;
    GeoPoint point = new GeoPoint(userLat.intValue(), userLng.intValue());
    return point;
}

private void nearRSU(Location location)
{
    Location userlocation = new Location("point A");
    Location objectlocation = new Location("point B");
    userlocation.setLatitude(geoLat(userGeopoint(location())).getLatitude());
    userlocation.setLongitude(geoLng(userGeopoint(location())).getLongitude());

    double rangeconv = 0, ctrange = 0, sumrange = 0, alltimes = 0;
    String rgtype = "", tmtype = "";
    int a = 0;
    String objectaddress = null;
    GeoPoint pointB = null;

    for (int i = 0 ; i < listLocRSU.size(); i++)
    {
        GeoPoint pointA = new GeoPoint((int) (listLocRSU.get(i).lat * 1E6), 
                                      (int) (listLocRSU.get(i).lng * 1E6));

        objectlocation.setLatitude(pointA.getLatitudeE6()/1E6);
        objectlocation.setLongitude(pointA.getLongitudeE6()/1E6);

        double range = userlocation.distanceTo(objectlocation);

        if (range >= 1000)
        {
            rangeconv = range / 1000;
            rgtype = " km";
        }
        else
        {
            rangeconv = range;
            rgtype = " m";
        }

        double times = rangeconv / 40;
        if (times >= 1)
        {
            alltimes = times;
            tmtype = " h";
        }
        else
        {
            alltimes = times * 60;
            tmtype = " min";
        }

        if (ctrange > rangeconv)
        {
            ctrange = rangeconv;
            sumrange = ctrange;
            a = i;
            pointB = new GeoPoint ((int) (listLocRSU.get(a).lat * 1E6), 
                                   (int) (listLocRSU.get(a).lng * 1E6));
        }
    else if (ctrange < rangeconv)
        {
            sumrange = ctrange;
            a = a;
            pointB = new GeoPoint ((int) (listLocRSU.get(a).lat * 1E6), 
                                   (int) (listLocRSU.get(a).lng * 1E6));
        }

        double objLat = listLocRSU.get(a).lat;
        double objLng = listLocRSU.get(a).lng;

        Geocoder objectgc = new Geocoder(this, Locale.getDefault());
        try 
        {
            List<Address> addresses = objectgc.getFromLocation(objLat, objLng, 1);
            StringBuilder objaddress = new StringBuilder();
            if (addresses.size() > 0) 
            {
                Address address = addresses.get(0);
                address.getMaxAddressLineIndex();
                objaddress.append(address.getAddressLine(0)).append(", ");
                objaddress.append(address.getLocality()).append(", ");
                objaddress.append(address.getCountryName()).append(", ");
                objaddress.append(address.getPostalCode());
            }
            objectaddress = objaddress.toString();
        } 
        catch (IOException e){}

        List<Overlay> overlays = map.getOverlays();
        Drawable marker = this.getResources().getDrawable(R.drawable.marker);
        MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(marker, this);            

        OverlayItem overlayitem = new OverlayItem(pointB, listLocRSU.get(a).locname, 
                "Address:\n" + objectaddress + 
                "\n\nLongitude:\n" + listLocRSU.get(a).lng + 
                "\n\nLatitude:\n" + listLocRSU.get(a).lat + 
                "\n\nDistance:\n" + sumrange + rgtype + 
                "\n\nTime Calculation (40 km/h):\n" + alltimes + tmtype);

        itemizedOverlay.addItem(overlayitem);
        overlays.add(itemizedOverlay);
    }
}

……

4

2 回答 2

3

Location类有一个方法distanceBetween,你不能用吗?

于 2012-07-31T09:58:37.250 回答
0

尝试提高你的代码可读性

Location origin;
Location closest;
List<Location> list;
Double minDist = null;
Double curDist = 0d;
for(Location l : list) {
    if(minDist == null || (curDist = origin.distanceBetween(l)) < minDist) {
        closest = l;
        minDist = curDist;
    }
}  
于 2012-07-31T10:04:41.993 回答