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
| public class CalculDistance 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 = null;
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);
int indexDistancemin = calculmin();
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);
}
});
}
public int calculmin(){
float[] tab1 = new float[100];
int j = 0;
int k;
for (int i = 0 ; i< listF.size() ; i++){
Location location1 = null;
location1.setLatitude(listF.get(i).getLatitude());
location1.setLongitude(listF.get(i).getLongitude());
locationUser.distanceTo(location1);
tab1[j] = locationUser.distanceTo(location1);
j++;
k = j;
Log.i(getClass().getName(), "tableau : " + k);
}
float dmin = tab1[0];
int indexmin = 0;
for (int i = 0; i<j ; i++){
if (tab1[i] < dmin){
dmin = tab1[1];
indexmin = i;
}
}
return indexmin;
}
@Override
public void onLocationChanged(Location location) {
latitudeUser = location.getLatitude();
longitudeUser = location.getLongitude();
locationUser = location;
}
} |
Partager