2012-05-21 3 views
2

Je développe l'emplacement du projet en fonction où je suis en utilisant le code suivantgetLastknownLocation() renvoie la valeur NULL en lien

J'utilise google api 8 pour le projet

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    currloc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    TextView t = (TextView)findViewById(R.id.textView1); 
    try{ 
     t.setText("Your current location is - "+currloc.getLatitude()+","+currloc.getLongitude()); 
    }catch (Exception e) { 
     // TODO: handle exception 
     t.setText("cant find current location "); 
    } 

ce code fonctionne très bien sur mon mais galaxie tab même sur htc

mais quand j'utilise nexus il renvoie la valeur nulle pour l'emplacement. dois-je changer mon niveau api ou est-il une exigence spécifique pour Galaxy nexus merci d'avance :)

+0

lm.requestLocationUpdates (LocationManager.GPS_PROVIDER , 0, 0, ceci); ici vous passez le dernier param (contexte de même classe), ici vous devriez passer l'instance de classe d'écouteur. –

+0

suis mon code. j'ai collé. –

+0

Salut Vishesh problème n'est pas avec le code que j'ai mis en place locationListener et les méthodes, il est seulement pour perticular nexus périphérique – Mahesh

Répondre

3

S'il vous plaît suivre la ligne de code ..

Step1: into your oncreate 

LocationListener locationListener = new LocalLocationListener(); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

Step2: into class body 

/**Listener on location change*/ 
private class LocalLocationListener implements LocationListener 
{ 

    public void onLocationChanged(Location location) 
    { 
     String text = "My current Location is: "+location.getLatitude()+", "+location.getLongitude(); 
     GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()* 1E6), (int)(location.getLatitude() * 1E6)); 
     mapController_.animateTo(geoPoint); 
     Toast.makeText(LocalMap.this, text, Toast.LENGTH_SHORT).show(); 
     Log.i("onLocationChanged", text); 

    } 

    public void onProviderDisabled(String provider) 
    { 
     // TODO Auto-generated method stub 
     Toast.makeText(LocalMap.this, "GPS Disable", Toast.LENGTH_SHORT).show(); 
     Log.i("onProviderDisabled", "GPS Disable"); 
    } 

    public void onProviderEnabled(String provider) 
    { 
     // TODO Auto-generated method stub 
     Toast.makeText(LocalMap.this, "GPS Enable", Toast.LENGTH_SHORT).show(); 
     Log.i("onProviderEnabled", "GPS Enable"); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
     // TODO Auto-generated method stub 

    } 
+0

Ne fonctionne pas .. si j'utilise le 'bestProvider' il retourne toujours' null', mais si j'utilise pas ... –

Questions connexes