2012-07-31 1 views
0

몇 개의 대상 위치 (사용자 위치 -> 대상 vs 사용자 위치 -> 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); 
    } 
} 

....

답변

0

`distanceTo()`http://developer.android.com/reference/android/location/Location.html#distanceTo%28android.location.Location%29를 코드의 가독성을 향상

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; 
    } 
}