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
|
public class LocalisationActivity extends MapActivity implements LocationListener {
double latitudeUser;
double longitudeUser;
Location locationUser;
double latitudemin;
double longitudemin;
FormationBDD formationBdd;
Formation formation;
Location localtion;
int indexmin;
MapView mapView = null;
private LocationManager lm = null;
MapController mc = null;
List<Formation> listF = new ArrayList<Formation>();
private MyLocationOverlay myLocation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.localisation);
mapView = (MapView) this.findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0,
this);
mc = mapView.getController();
mc.setZoom(15);
formationBdd = new FormationBDD(this);
formationBdd.open();
// récupération liste centres de formations
listF = formationBdd.getAllFormations();
Log.i(getClass().getName(), "liste : " + listF);
// calcul distance min
int indexDistancemin = calculDistanceMin();
Log.i(getClass().getName(), "index : " + indexDistancemin);
double latitudemin = listF.get(indexDistancemin).getLatitude();
double longitudemin = listF.get(indexDistancemin).getLongitude();
// récupérer l'index du centre le plus proche
// création du géopoint correspondant au centre plus proche
GeoPoint point = new GeoPoint(microdegrees(latitudemin),microdegrees(longitudemin));
ItemizedOverlayPerso pinOverlay = new ItemizedOverlayPerso(getResources().getDrawable(R.drawable.marker));
pinOverlay.addPoint(point);
mapView.getOverlays().add(pinOverlay);
myLocation = new MyLocationOverlay(getApplicationContext(), mapView);
//Ajouter votre location dans la map
mapView.getOverlays().add(myLocation);
//Activer l'affichage de la localisation
myLocation.enableMyLocation();
myLocation.runOnFirstFix(new Runnable(){
public void run() {
mc.animateTo(myLocation.getMyLocation());
mc.setZoom(17);
}
});
}
// méthode qui calcule la distance entre chaque point et la localisation actuelle et retourne l'index min
public int calculDistanceMin(){
float[] results = new float[2];
int indexmin = 0;
for (int i = 0 ; i< listF.size() ; i++){
double latitudeElmt = listF.get(i).getLatitude();
double longitudeElmt = listF.get(i).getLongitude();
GeoPoint point = new GeoPoint(microdegrees(latitudeElmt),microdegrees(longitudeElmt));
myLocation = new MyLocationOverlay(getApplicationContext(), mapView);
GeoPoint locationUser = myLocation.getMyLocation();
Location.distanceBetween(locationUser.getLatitudeE6(), locationUser.getLongitudeE6(), microdegrees(latitudeElmt), microdegrees(longitudeElmt), results);
}
float dmin = results[0];
for (int i = 0; i<listF.size() ; i++){
if (results[i] < dmin){
dmin = results[1];
indexmin = i;
}
}
return indexmin;
}
} |
Partager