1 pièce(s) jointe(s)
Récupérer valeur fonction localisation d'un service dans une Activity
Bonjour à tous,
J'ai une question concernant la récupération de donnée sous Android particulière.
J'avais crée à un moment une classe service "GPS_Service" qui sert à crée ou retourner la Latitude et la Longitude du GPS de l'utilisateur.
Sauf que maintenant je voudrais récupérer ses coordonnées dans une Activity (une Map Activity) mais je ne sais pas trop comment m'y prendre pour que cela fonctionne correctement.
Voilà le code de mon Service:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
//import com.google.android.gms.location.LocationListener;
import android.location.LocationListener;
public class GPS_Service extends Service {
private LocationListener listener;
private LocationManager locationManager;
//private LocationListener locationListener;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//function qui renvoie la latitude et la longitude de notre position sous forme de text
@Override
public void onLocationChanged(Location location) {
Intent localisation = new Intent("location_update");
localisation.putExtra("coordinates", location.getLongitude() + " " + location.getLatitude());
//On envoie notre intent grâce à la fonction "sendBroadcast"
sendBroadcast(localisation);
}
//@Override
public void onStatusChanged(String s, int i, Bundle extras) {
}
//@Override
public void onProviderEnabled(String s) {
}
//@Override
public void onProviderDisabled(String s) {
Intent localisation = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
localisation.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(localisation);
}
/**
* //On envoie nos coordonnées Longitude et Latitude à notre map
Intent IntentLocalisation = new Intent("location_update");
IntentLocalisation.putExtra("coordonnées :", listener.onLocationChanged().getLongitude() + " " + listener.onLocationChanged().getLatitude());
//On envoie notre intent grâce à la fonction "sendBroadcast"
sendBroadcast(IntentLocalisation);**/
};
/**
//On envoie nos coordonnées Longitude et Latitude à notre map
Intent IntentLocalisation = new Intent("location_update");
IntentLocalisation.putExtra("coordonnées :", listener.onLocationChanged().getLongitude() + " " + listener.onLocationChanged().getLatitude());
//On envoie notre intent grâce à la fonction "sendBroadcast"
sendBroadcast(IntentLocalisation);**/
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,listener);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null){
//noinspection MissingPermission
locationManager.removeUpdates(listener);
}
}
} |
J'ai une petite erreur au niveau de mes paranthèses également, donc faute syntaxique probablement
Pièce jointe 253108
J'avais essayé avec un Intent mais ce n'est peut-être pas la solution.
Je veux retourner la position de mon utilisateur dans mon code ici:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//mMap.append()
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
//Afficher la position de l'utilisateur
float UserLatitude = GPS_Service.listener.onLocationChanged();
float UserLongitude = ;
LatLng maPosition = new LatLng(GPS_Service.listener.location.getLongitude(), GPS_Service.listener.location.getLatitude());
mMap.addMarker(new MarkerOptions().position(sydney).title("Votre position"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
} |
Après que ma map soit crée.
Si quelqu'un à une idée qui pourrait m'éclairer merci d'avance.