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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
| package symplybus.fr;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class Geolocalisation extends Activity implements Runnable {
/** Called when the activity is first created. */
private ListView maListViewPerso;
private double position[];
private String[][] mArret;
private ProgressDialog progressDialog;
final Handler uiThreadCallback = new Handler();
private HashMap<String, String> map ;
Runnable runInUIThread;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(this); //On crée un objet ProgressDialog
progressDialog.setMessage("Géolocalisation en cours...");//On détermine le message indiquant à l'utilisateur la synchronisation en cours
progressDialog.show();//On l'affiche
Thread a= new Thread(this);
a.start();
}
public List<NameValuePair> post()
{
String a=String.valueOf(position[1]);
String b=String.valueOf(position[0]);
mArret= new String[3][4];
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); //On crée la liste qui contiendra tous nos paramètres
nameValuePairs.add(new BasicNameValuePair("latitude",a));
nameValuePairs.add(new BasicNameValuePair("longitude",b));
return nameValuePairs;
}
public void affichage()
{
setContentView(R.layout.geolocalisation);
maListViewPerso = (ListView) findViewById(R.id.listviewperso);
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
//On déclare la HashMap qui contiendra les informations pour un item
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("titre",mArret[0][0]);
map.put("description", "Distance : " + Integer.parseInt(mArret[0][1]) + " mètres");
map.put("img", String.valueOf(R.drawable.fleche));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre",mArret[1][0]);
map.put("description", "Distance : " + Integer.parseInt(mArret[1][1]) + " mètres" );
map.put("img", String.valueOf(R.drawable.fleche));
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre",mArret[2][0]);
map.put("description", "Distance : " + Integer.parseInt(mArret[2][1]) + " mètres");
map.put("img", String.valueOf(R.drawable.fleche));
listItem.add(map);
//Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue affichageitem
SimpleAdapter mSchedule = new SimpleAdapter(getBaseContext(), listItem, R.layout.affichageitem,
new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(mSchedule);
}
public void ecouter()
{
maListViewPerso.setOnItemClickListener(new OnItemClickListener() {
@Override
@SuppressWarnings("unchecked")
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//on récupère la HashMap contenant les infos de notre item (titre, description, img)
map = (HashMap<String, String>) maListViewPerso.getItemAtPosition(position);
affichage_choix();
}
public void affichage_plan()
{
for(int i=0;i<3;i++)
{
if(map.get("titre").equals(mArret[i][0]))
{
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?saddr="+ position[1] +","+ position[0] +"&daddr="+mArret[i][2]+","+mArret[i][3]+""));
startActivity(intent);
}
}
}
public void affichage_choix()
{
//on créer une boite de dialogue
AlertDialog.Builder adb;
adb = new AlertDialog.Builder(Geolocalisation.this);
//on attribut un titre à notre boite de dialogue
adb.setTitle("Sélection de l'Arrêt");
//on insère un message à notre boite de dialogue, et ici on affiche le titre de l'item cliqué
adb.setMessage("Votre choix : "+map.get("titre"));
//on indique que l'on veut le bouton ok à notre boite de dialogue
adb.setNegativeButton("Plan",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
affichage_plan();
} });
adb.setNeutralButton("Calcul Itineraire",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
Bundle bundle = new Bundle();
bundle.putString("arrets",map.get("titre"));
Intent newIntent = new Intent(Geolocalisation.this, Avance.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
} });
//on affiche la boite de dialogue
adb.show();
}
});
}
public void affichage_erreurService()
{
progressDialog.dismiss(); //On fait disparaitre l'affichage de progressDialog
AlertDialog.Builder builder = new AlertDialog.Builder(Geolocalisation.this); //On configure la boîte de dialogue
builder.setMessage("Récupération des arrêts les plus proches impossible.") //On lui donne un message
.setCancelable(false)//On peut pas fermer celle-ci sans que l'on appuie sur un bouton
.setTitle("Service inaccessible")//On lui donne un titre
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { //On crée un bouton "ok"
public void onClick(DialogInterface dialog, int id) {
Geolocalisation.this.finish(); //Si on clique sur le bouton, alors on ferme l'application
}
});
AlertDialog alert = builder.create(); //On crée la boîte de dialogue
alert.show(); //On l'affiche
}
public void affichage_erreurGps()
{
AlertDialog.Builder builder = new AlertDialog.Builder(Geolocalisation.this); //On configure la boîte de dialogue
builder.setMessage("Veuillez vérifier que votre GPS est bien activé.") //On lui donne un message
.setCancelable(false)//On peut pas fermer celle-ci sans que l'on appuie sur un bouton
.setTitle("Récupération de votre position impossible")//On lui donne un titre
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { //On crée un bouton "ok"
public void onClick(DialogInterface dialog, int id) {
Geolocalisation.this.finish(); //Si on clique sur le bouton, alors on ferme l'application
}
});
AlertDialog alert = builder.create(); //On crée la boîte de dialogue
alert.show(); //On l'affiche
}
@Override
public void run() {
// TODO Auto-generated method stub
position = new double[2];
Geolocaliser geo = new Geolocaliser(Geolocalisation.this);
if(geo.geolocaliser(position)==true)
{
if(Webservice.recuperationArretsProches(post(),mArret)==true)
{
runInUIThread = new Runnable() { //Création de la fonction a éxécuter lorsque la synchronisation sera terminée
public void run() {
affichage();
ecouter();
}
};
}
else
{
runInUIThread = new Runnable() { //Création de la fonction a éxécuter lorsque la synchronisation sera terminée
public void run() {
affichage_erreurService();
}
};
}
}
else
{
runInUIThread = new Runnable() { //Création de la fonction a éxécuter lorsque la synchronisation sera terminée
public void run() {
affichage_erreurGps();
}
};
}
progressDialog.dismiss();
uiThreadCallback.post(runInUIThread);
}
} |
Partager