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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class JouerActivity extends Activity implements View.OnClickListener, LocationListener {
private LocationManager lManager;
private LocationListener ll;
private Location location;
private Button radarButton, positionAdresse, positionCarte;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jouer);
lManager = (LocationManager)getSystemService(JouerActivity.this.LOCATION_SERVICE);
positionAdresse = (Button)findViewById(R.id.positionAdresse);
positionAdresse.setOnClickListener(this);
positionCarte = (Button)findViewById(R.id.positionCarte);
positionCarte.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.positionCarte:
position(v);
break;
case R.id.positionAdresse:
afficherAdresse();
break;
default:
break;
}
}
private void afficherAdresse() { // Affiche l'adresse postale dans un TextView
Geocoder geo = new Geocoder(JouerActivity.this);
location = this.lManager.getLastKnownLocation(
LocationManager.PASSIVE_PROVIDER);
if (Build.VERSION.SDK_INT >= 23) { // Il faut ajouter ce "if" pour que ça marche sur une version 7.0
if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Et bien vérifier ces permissions (il faut mettre ces permissions aussi dans ton Manifest.xml)
location = this.lManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
lManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 60000, 0, this);
}
}
try {
//Ici on récupère la premiere adresse trouvée gràce à la position que l'on a récupérée
List<Address> adresses = geo.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
if (adresses != null && adresses.size() == 1) {
Address adresse = adresses.get(0);
// Si le geocoder a trouvé une adresse, alors on l'affiche
((TextView) findViewById(R.id.adresse)).setText(String.format("%s, %s %s",
adresse.getAddressLine(0),
adresse.getPostalCode(),
adresse.getLocality()));
} else {
// Sinon on affiche un message d'erreur
((TextView) findViewById(R.id.adresse)).setText("L'adresse n'a pu être déterminée");
}
} catch (IOException e) {
e.printStackTrace();
((TextView) findViewById(R.id.adresse)).setText("L'adresse n'a pu être déterminée");
} */
}
public void position(final View v) { // Cette méthode ouvre Google Map et affiche la position géographique de l'utilisateur sur la carte, un toast te préviens si ça a marché ou pas
location = this.lManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if(Build.VERSION.SDK_INT >= 23) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
location = this.lManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
}
if(null == location || (System.currentTimeMillis()-location.getTime() > 3600e3)) {
Toast.makeText(this, "Position inconnue" + location, Toast.LENGTH_LONG).show();
v.setEnabled(false);
this.lManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, ll, null);
} else {
Toast.makeText(this, "Dernière position connue " + (((TextView)this.findViewById(R.id.adresse))).getText(), Toast.LENGTH_LONG).show();
this.analyseLocation(location);
}
}
private void analyseLocation(final Location l) { // Cette méthode est utilisée dans la méthode précédente, c'est pour pouvoir ouvrir l'application Google Map (donc il y a un Intent)
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:" + l.getLatitude() + "," + l.getLongitude()));
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0);
if(activities.size() > 0) {
this.startActivity(i);
}
}
public void onLocationChanged(Location location) { // Les 4 fonctions qui suivent sont obligatoires dans la documentation Android, même si certaines n'ont rien dedans il faut les mettre
analyseLocation(location);
//... on sauvegarde la position
this.location = location;
//... et on spécifie au service que l'on ne souhaite plus avoir de mise à jour
lManager.removeUpdates(this);
}
public void onProviderDisabled(String provider) {
//... et on spécifie au service que l'on ne souhaite plus avoir de mise à jour
lManager.removeUpdates(this);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
} |