我需要找到當前位置,另一個位置,我有一個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();
。解析執行代碼已成功執行。它可以如何修復?
顯然'loc'是onLocationChanged從來沒有所謂的空意思... – 2012-02-15 18:56:16
空指針可能意味着「loc」爲空。 – 2012-02-15 18:58:43
除了@SergeyBenner所說的內容之外,您還需要確保您的應用程序對位置服務使用適當的權限。 – 2012-02-15 19:00:03