2017-02-14 1 views
-2

Voici mon code source pour obtenir la longitude et la latitude, mais comment puis-je implémenter l'instruction dans le Location Listener, je suis nouveau dans ce domaine, j'ai vraiment besoin d'aide, s'il vous plaît veuillez offrir votre aide, merci.Comment implémenter le LocationListener?

public class Location extends AppCompatActivity{ 

    LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_location); 
     isLocationEnabled(); 

    } 

    LocationListener locationListenerGPS=new LocationListener() { 
     @Override 
     public void onLocationChanged(android.location.Location location) { 
      double latitude=location.getLatitude(); 
      double longitude=location.getLongitude(); 
      String msg="New Latitude: "+latitude + "New Longitude: "+longitude; 
      Toast.makeText(getBaseContext(),msg,Toast.LENGTH_LONG).show(); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 


    protected void onResume(){ 
     super.onResume(); 
     isLocationEnabled(); 
    } 

    private void isLocationEnabled() { 

     if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      AlertDialog.Builder alertDialog=new AlertDialog.Builder(this); 
      alertDialog.setTitle("Enable Location"); 
      alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu."); 
      alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int which){ 
        Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(intent); 
       } 
      }); 
      alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int which){ 
        dialog.cancel(); 
       } 
      }); 
      AlertDialog alert=alertDialog.create(); 
      alert.show(); 
     } 
     else{ 
      AlertDialog.Builder alertDialog=new AlertDialog.Builder(this); 
      alertDialog.setTitle("Confirm Location"); 
      alertDialog.setMessage("Your Location is enabled, please enjoy"); 
      alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int which){ 
        dialog.cancel(); 
       } 
      }); 
      AlertDialog alert=alertDialog.create(); 
      alert.show(); 
     } 
    } 

} 
+2

Pouvez-vous préciser ce que vous cherchez? Vous semblez avoir déjà implémenté du code dans un LocationListener. Quel est le problème? – degs

+1

Quel type de déclaration implémente-t-on dans l'écouteur de localisation? Pouvez-vous clarifier? –

+0

Je veux dire, comment dois-je appeler cela dans onCreate() 'LocationListener locationListenerGPS = new LocationListener() { @Override public void OnLocationChanged (emplacement android.location.Location) { à double latitude = location.getLatitude(); double longitude = location.getLongitude(); Chaîne msg = "Nouvelle latitude:" + latitude + "Nouvelle longitude:" + longitude; ' –

Répondre

1

Il suffit d'utiliser ce code:

public class Location extends AppCompatActivity { 
    LocationManager locationManager; 
    Context mContext; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_location); 
     mContext=this; 
     locationManager=(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       2000, 
       10, locationListenerGPS); 
     isLocationEnabled(); 

    } 

    LocationListener locationListenerGPS=new LocationListener() { 
     @Override 
     public void onLocationChanged(android.location.Location location) { 
      double latitude=location.getLatitude(); 
      double longitude=location.getLongitude(); 
      String msg="New Latitude: "+latitude + "New Longitude: "+longitude; 
      Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show(); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 


    protected void onResume(){ 
     super.onResume(); 
     isLocationEnabled(); 
    } 

    private void isLocationEnabled() { 

     if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext); 
      alertDialog.setTitle("Enable Location"); 
      alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu."); 
      alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int which){ 
        Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(intent); 
       } 
      }); 
      alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int which){ 
        dialog.cancel(); 
       } 
      }); 
      AlertDialog alert=alertDialog.create(); 
      alert.show(); 
     } 
     else{ 
      AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext); 
      alertDialog.setTitle("Confirm Location"); 
      alertDialog.setMessage("Your Location is enabled, please enjoy"); 
      alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int which){ 
        dialog.cancel(); 
       } 
      }); 
      AlertDialog alert=alertDialog.create(); 
      alert.show(); 
     } 
    } 
} 


The parameters of requestLocationUpdates methods are as follows: 

provider: the name of the provider with which we would like to register. 
minTime: minimum time interval between location updates (in milliseconds). 
minDistance: minimum distance between location updates (in meters). 
listener: a LocationListener whose onLocationChanged(Location) method will be called for each location update. 


Permissions: 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

Add above permissions to manifest file for the version lower than lollipop and for marshmallow and higher version use runtime permission. 
+0

à la ligne ' locationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 2000,10, locationListenerGPS); ' il montre l'autorisation d'appel qui rejeté par l'utilisateur –

+0

Merci, ça marche. Vraiment merci beaucoup –