2012-02-15 72 views
2

我需要找到當前位置,另一個位置,我有一個string.I形式之間的距離形式之間的距離正在使用下面的代碼如何找到兩個位置其中之一是在一個字符串

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();。解析執行代碼已成功執行。它可以如何修復?

+1

顯然'loc'是onLocationChanged從來沒有所謂的空意思... – 2012-02-15 18:56:16

+0

空指針可能意味着「loc」爲空。 – 2012-02-15 18:58:43

+1

除了@SergeyBenner所說的內容之外,您還需要確保您的應用程序對位置服務使用適當的權限。 – 2012-02-15 19:00:03

回答

0

只需要通過計算距離:

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

提供者在這個方法中實際上意味着什麼?我可以在位置(字符串) – Arihant 2012-02-15 18:54:21

+0

中傳遞一個字符串地址什麼樣的字符串地址? – goodm 2012-02-15 18:56:39

+0

正常地址由街道,城鎮和國家組成 – Arihant 2012-02-15 18:58:44

0

你可以使用勾股定理計算這個距離:

int x_point1 = 0; 
int x_point2 = 100; 
int y_point1 = 0; 
int y_point2 = 200; 
int distance = 0; 
// Pythagorean theorem: a^2 + b^2 = c^2 
// (y_point2 - y_point1) basically calculates the distance in the coordinate grid 
// Applied with this: (y_point2 - y_point1)^2 + (x_point2 - x_point1)^2 = distance^2 
//       40000 + 10000     = distance^2 
distance = sqrt ((y_point2 - y_point1)^2 + (x_point2 - x_point1)^2); 
// distance^2 = 50000 
//distance = sqrt(50000) 
//distance = 100 sqrt(5) or approx. 223.607 
相關問題