2011-11-11 6 views
2

Le code suivant calculer la distance entre deux points géographiques et retourner le réseau à distanceerreur sur le calcul de distance entre deux points géographiques

public static double[] getDistance() { 
    double[] distance=new double[20]; 
    Intent intent=new Intent(); 
    double latitude[]=intent.getDoubleArrayExtra("latitude"); 
    double longitude[]=intent.getDoubleArrayExtra("longitude"); 
    Log.v(TAG, "got latitude array"); 
    double getlat=intent.getDoubleExtra("geoLat", 0.0); 

    double getlng=intent.getDoubleExtra("geoLng", 0.0); 
      Log.v(TAG, "got location"); 
      for(int i=0;i<20;i++) { 
    distance[i]=gps2m((float)getlat, (float)getlng, (float)latitude[i], (float)longitude[i]); 
} 
return distance; 
} 

et gps2m est

private static double gps2m(float lat_a, float lng_a, float lat_b, float lng_b) { 
    float pk = (float) (180/3.14169); 

    float a1 = lat_a/pk; 
    float a2 = lng_a/pk; 
    float b1 = lat_b/pk; 
    float b2 = lng_b/pk; 

    float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2); 
    float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2); 
    float t3 = FloatMath.sin(a1)*FloatMath.sin(b1); 
    double tt = Math.acos(t1 + t2 + t3); 
    Log.v(TAG, "returning distance"); 
    return 6366000*tt; 
} 
} 

et l'activité d'afficher la distance est comme suit

public class CalculateDistance extends Activity{ 
double Latitude; 
private String tag="CalculateDistance"; 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
setContentView(R.layout.distance_phone_no); 
Intent intent= getIntent(); 
int value= intent.getIntExtra("clickedid",0);//item id when clicked on listview 
Log.v(tag, ""+value); 
double latitude[]=GetLatAndLng.getDistance(); 
Log.v(tag, "got distanc array"); 
for(int i=0;i<20;i++) 
    if(i==value) 
     Latitude=latitude[i]; 
TextView tv=(TextView)findViewById(R.id.text11); 
tv.setText(""+Latitude); 
} 
} 

il donne l'exception NullPointer La ligne qui causin g c'est la boucle for de la méthode getDistance pour calculer la distance.

Aidez-moi où ça ne va pas? Ou toute autre méthode pour calculer la distance ... J'ai besoin d'aide !!! Merci à l'avance !!!

+0

comment peut '' si le travail intent.getDoubleArrayExtra' intention = new Intent() '?? –

+0

Vous pouvez également jeter un oeil à la statique ['Location.distanceBetween'] (http://developer.android.com/reference/android/location/Location.html#distanceBetween%28double,%20double,%20double, % 20double,% 20float% 5b% 5d% 29). Pas besoin de réinventer la roue. :) –

+0

ohh !! bon point @Sherif !! le changer en getIntent(); –

Répondre

2

vient ici un extrait personnalisé, espérons que cette aide

import static java.lang.Math.acos; 
import static java.lang.Math.cos; 
import static java.lang.Math.sin; 
import static java.lang.Math.toRadians; 

public static long getDistanceMeters(double lat1, double lng1, double lat2, double lng2) { 
    double l1 = toRadians(lat1); 
    double l2 = toRadians(lat2); 
    double g1 = toRadians(lng1); 
    double g2 = toRadians(lng2); 

    double dist = acos(sin(l1) * sin(l2) + cos(l1) * cos(l2) * cos(g1 - g2)); 
    if(dist < 0) { 
     dist = dist + Math.PI; 
    } 
    return Math.round(dist * 6378100); 
} 
Questions connexes