Sur Android, quelle est la methode qui cherche la distance en kilometere entre p1 et p2?
Sachant que p1 et p2 sont des objets de type GeoPoint.
Sur Android, quelle est la methode qui cherche la distance en kilometere entre p1 et p2?
Sachant que p1 et p2 sont des objets de type GeoPoint.
Salut anwer
tu peut utiliser ce class:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import com.google.android.maps.GeoPoint; public class DistanceCalculator { private double Radius; // R = earths radius (mean radius = 6,371km) // Constructor DistanceCalculator(double R) { Radius = R; } public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) { double lat1 = StartP.getLatitudeE6()/1E6; double lat2 = EndP.getLatitudeE6()/1E6; double lon1 = StartP.getLongitudeE6()/1E6; double lon2 = EndP.getLongitudeE6()/1E6; double dLat = Math.toRadians(lat2-lat1); double dLon = Math.toRadians(lon2-lon1); double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); return Radius * c; } }
Tu veux la distance "à vol d'oiseaux" ou la distance réelle en passant par des routes ?
Bonjour,
Je découvre aussi ANDROID dans ses aspects liés à l'exploitation du GPS et je crois rêver ... tant il me semble avoir dû faire les choses au marteau et au burin ... avant ANDROID.
Il faut aller voir de près la classe Location et toutes ses méthodes qui permettent de tout faire très facilement ... en oubliant les coordonnées sphériques !
c'est, par exemple, ici :
http://developer.android.com/referen.../Location.html
Voir en particulier :
-> bearingTo(Location dest)
Returns the approximate initial bearing in degrees East of true North when traveling along the shortest path between this location and the given location.
-> distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.
-> distanceTo(Location dest)
Returns the approximate distance in meters between this location and the given location.
Partager