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 に答える