2011-05-09 96 views
0

我必須製作一個android應用程序,我需要找到用戶當前位置,然後從數據庫中獲取一堆經緯度。在此之後,我需要找到用戶當前位置和經緯度之間的距離,多頭......並且對於小於200km的所有距離,我需要將它們添加到列表中。無法獲取當前的地理位置android

問題: 我無法找到當前的用戶位置,我總是獲得0,0。我通過使用DDMS傳遞座標來在模擬器上測試相同的結果。我也嘗試過在實際的設備上仍然收到0,0作爲經緯度。此外正在計算的距離不正確。

這裏是我的代碼:

LocationManager lm; 
LocationListener ll; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.nearactivity); 
    mHandler= new Handler(); 
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    ll = new mylocationlistener(); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
    showProgress(); 
} 

private void showProgress() { 
    pd = new ProgressDialog(NearActivity.this); 
    pd.setTitle("Nautic-Dates"); 
    pd.setMessage("Loading"); 
    pd.show(); 
    GetLocations gl = new GetLocations(); 
    gl.execute(); 
    try { 
     setListAdapter(new CustomeAdapter(gl.get())); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    pd.dismiss(); 
} 

public class GetLocations extends AsyncTask<Void, Void, ArrayList<String>> { 

    @Override 
    protected ArrayList<String> doInBackground(Void... arg0) { 
     HandleDatabase hb = new HandleDatabase(); 
     String locations[] = hb.getLocationsFromDatabase(); 
     ArrayList<String> nearlocations = new ArrayList<String>(); 
     CustomGeoCoder cg = new CustomGeoCoder(); 
     for (int i = 0; i < locations.length; i++) { 
      String details[] = locations[i].split("--"); 
      if (details[0].trim().equals("") 
        || details[1].trim().equals("")||details[0].trim().equals("null") 
        || details[1].trim().equals("null")) { 
       continue; 
      } else { 
       float distance = cg.checkLocation(details[0], details[1]); 
       if (distance<RADIUS) { 
        nearlocations.add(locations[i]+"--"+distance); 
       } 
      } 
     } 
     return nearlocations; 
    } 

} 

public class CustomGeoCoder { 


    public float checkLocation(String string, String string2) { 
     Location locationA = new Location("pointA"); 

     Log.i("current loc", "lat " + lat + " lng " + lng); 
     Log.i("checklocation loc", "lat " + string + " lng " + string2); 
     locationA.setLatitude(lat); 
     locationA.setLongitude(lng); 

     Location locationB = new Location("pointB"); 

     try { 
      locationB.setLatitude(Double.parseDouble(string)); 
     } catch (NumberFormatException e) { 
      try { 
       locationB.setLatitude(Float.parseFloat(string)); 
      } catch (NumberFormatException e1) { 
       try { 
        locationB.setLatitude(Integer.parseInt(string)); 
       } catch (NumberFormatException e2) { 
        e2.printStackTrace(); 
        return RADIUS+1; 
       } 

      } 
     } 
     try { 
      locationB.setLongitude(Double.parseDouble(string2)); 
     } catch (NumberFormatException e) { 
      try { 
       locationB.setLongitude(Float.parseFloat(string2)); 
      } catch (NumberFormatException e1) { 
       try { 
        locationB.setLongitude(Integer.parseInt(string2)); 
       } catch (NumberFormatException e2) { 
        e2.printStackTrace(); 
        return RADIUS+1; 
       } 
      } 
     } 

     float distance = locationA.distanceTo(locationB); 

//this code is returning wrong values. 
     return distance/1000; 
//   Log.i("distance", "" + distance); 
//   if (distance/1000 > RADIUS) 
//    return false; 
//   else 
//    return true; 
    } 

} 

private class mylocationlistener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      Log.d("LOCATION CHANGED", location.getLatitude() + ""); 
      Log.d("LOCATION CHANGED", location.getLongitude() + ""); 
      lat = location.getLatitude(); 
      lng = location.getLongitude(); 
      Toast.makeText(NearActivity.this,"lng="+lng+" lat="+lat, Toast.LENGTH_SHORT); 
      showProgress(); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
} 

我不能夠明白是怎麼回事了?

非常感謝您的幫助。

+0

您的android Manifest.xml是否包含適當的授權? (http://developer.android.com/reference/android/Manifest.permission.html),你看到日誌中的任何東西? – 2011-05-09 11:58:15

+0

是的。我看到以下內容:05-09 17:48:59.877:INFO /當前loc(1573):lat 0.0 lng 0.0 05-09 17:48:59.877:INFO/checklocation loc(1573):lat 41.2834563 lng -70.0994605 – user590849 2011-05-09 12:20:10

+0

爲什麼我從distanceTo方法獲得距離0? – user590849 2011-05-09 12:24:43

回答

0

我已經在Eclipse中加載了你的代碼,儘管我沒有運行它,因爲我顯然沒有你的HandleDatabase類代碼,也沒有你的CustomAdapter。然而,讓我感到震驚的是,你似乎正在使用lat和lon作爲Activity的對象成員,或者你沒有在它們使用的方法中將它們定義爲變量。我建議把你的代碼分解爲自包含的單元,這裏的人們可以更容易地爲你調試。