2011-05-23 4 views
1

Pouvez-vous me aider .. l'application a commencé à se bloquer lorsque j'ai ajouté le géocodage pour obtenir le nom de la ville en utilisant la latitude et la longitude ..Gps Android - obtenir Latitude et Longitude

double latitude, longitude; 
    private TextView lala; 

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    longitude = location.getLongitude(); 
    latitude = location.getLatitude(); 
    final LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
     } 

     public void onProviderDisabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onProviderEnabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      // TODO Auto-generated method stub 

     } 
    }; 

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
    lala = (TextView) findViewById(R.id.textView1); 

    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault()); 
    List<Address> myList = null; 
    try { 
     myList = myLocation.getFromLocation(latitude, longitude, 1); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 


    lala.setText((CharSequence) myList); 
+0

stacktrace s'il vous plaît? – matsjoe

Répondre

8

lm.getLastKnownLocation peut return null, puis essayer de getLongitude() tou vous obtiendrez NullPointerException

également lala.setText((CharSequence) myList); peut faire la même chose (Exception jet) cause » myList peut être nul ici (si Geocoder est échoué).

EDIT Pour faire face à l'exception de géocodage considèrent la liste suivante

  1. Assurez-vous que votre latitude et la longitude ne sont pas nulles
  2. Assurez-vous que -90 < = latitude < = 90 et -180 Assurez-vous que vous disposez d'une connexion réseau disponible
= longitude < = 180

EDIT2 Code amélioré

private TextView lala; 

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

if(location != null) { 
    showMyAddress(location); 
} 

final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     showMyAddress(location); 
    } 

    public void onProviderDisabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 

    public void onProviderEnabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     // TODO Auto-generated method stub 

    } 
}; 

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
lala = (TextView) findViewById(R.id.textView1); 



// Also declare a private method 

private void showMyAddress(Location location) { 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault()); 
    List<Address> myList; 
    try { 
     myList = myLocation.getFromLocation(latitude, longitude, 1); 
     if(myList.size() == 1) { 
      lala.setText(myList.get(0).toString());    
     } 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
} 
+0

Vous avez raison .. mais pouvez-vous me dire quoi faire pour que Geocoder ne pas échouer .. Désolé, je suis débutant dans android et java .. – Sergio

+0

J'ai édité ma réponse – Olegas

+0

okey .. Maintenant j'ai Latitude: 59.412 .. et Longitude: 24.689 .. Et ma connexion réseau fonctionne bien .. Peut-être qu'il y a quelque chose de mal avec mon code – Sergio

2
Criteria criteria = new Criteria(); 
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
String provider = locationManager.getBestProvider(criteria, false); 
Location location = locationManager.getLastKnownLocation(provider); 
int la = (int) (location.getLatitude() * 1E6); 
int lo = (int) (location.getLongitude() * 1E6); 
Questions connexes