2016-08-01 32 views
-2

Ich versuche, mit der Funktion zu kommen, die GPS-Koordinaten zwischen zwei Punkten jede Sekunde ausfüllen könnte. Es gibt wenige Posts hierüber, aber ich konnte nichts komplett finden. Die nächste Antwort, die ich fand, war:
Interpolate between 2 GPS locations based on walking speed
Ich modifizierte eine der Antwort mit dem Lager. Es scheint jedoch immer noch nicht zu funktionieren. Besonders denke ich, dass die Entfernungsberechnung falsch ist. Könnte jemand den folgenden Code sehen und ändern?
Danke!Java interpolieren zwischen zwei GPS-Punkten mit einer Geschwindigkeit

import java.util.ArrayList; 

public class Test { 

    double radius = 6371; 

    public Test() { 
     Location start = new Location(lat, lon); 
     Location end = new Location(lat, lon); 
     double speed = 1.39; 
     double distance = CalculateDistanceBetweenLocations(start, end); 
     double duration = distance/speed; 
     System.out.println(distance + ", " + speed + ", " + duration); 
     ArrayList<Location> locations = new ArrayList<Location>(); 
     for (double i = 0; i < duration; i += 1.0) { 
      double bearing = CalculateBearing(start, end); 
      double distanceInKm = speed/1000; 
      Location intermediaryLocation = CalculateDestinationLocation(start, bearing, distanceInKm); 
      locations.add(intermediaryLocation); 
      System.out.println(intermediaryLocation.latitude + ", " + intermediaryLocation.longitude); 
      start = intermediaryLocation; 
     } 
    } 

    double DegToRad(double deg) { 
     return (deg * Math.PI/180); 
    } 

    double RadToDeg(double rad) { 
     return (rad * 180/Math.PI); 
    } 

    double CalculateBearing(Location startPoint, Location endPoint) { 
     double lat1 = DegToRad(startPoint.latitude); 
     double lat2 = DegToRad(endPoint.latitude); 
     double deltaLon = DegToRad(endPoint.longitude - startPoint.longitude); 
     double y = Math.sin(deltaLon) * Math.cos(lat2); 
     double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(deltaLon); 
     double bearing = Math.atan2(y, x); 
     return (RadToDeg(bearing) + 360) % 360; 
    } 

    Location CalculateDestinationLocation(Location point, double bearing, double distance) { 
     distance = distance/radius; 
     bearing = DegToRad(bearing); 
     double lat1 = DegToRad(point.latitude); 
     double lon1 = DegToRad(point.longitude); 
     double lat2 = Math 
       .asin(Math.sin(lat1) * Math.cos(distance) + Math.cos(lat1) * Math.sin(distance) * Math.cos(bearing)); 
     double lon2 = lon1 + Math.atan2(Math.sin(bearing) * Math.sin(distance) * Math.cos(lat1), 
       Math.cos(distance) - Math.sin(lat1) * Math.sin(lat2)); 
     lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI; 
     return new Location(RadToDeg(lat2), RadToDeg(lon2)); 
    } 

    double CalculateDistanceBetweenLocations(Location startPoint, Location endPoint) { 
     double lat1 = DegToRad(startPoint.latitude); 
     double lon1 = DegToRad(startPoint.longitude); 
     double lat2 = DegToRad(endPoint.latitude); 
     double lon2 = DegToRad(endPoint.longitude); 
     double deltaLat = lat2 - lat1; 
     double deltaLon = lon2 - lon1; 
     double a = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) 
       + Math.cos(lat1) * Math.cos(lat2) * Math.sin(deltaLon/2) * Math.sin(deltaLon/2); 
     double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1)); 
     return (radius * c); 
    } 

    public static void main(String[] args) { 
     new Test(); 
    } 

    class Location { 
     public double latitude, longitude; 

     public Location(double lat, double lon) { 
      latitude = lat; 
      longitude = lon; 
     } 
    } 

} 
+0

Was die erwarteten Ergebnisse und was haben Sie eigentlich bekommen sind sein sollte? Was bedeutet "falsch"? – Hulk

+0

Ich vermute, dass ein Teil Ihrer Verwirrung von der Tatsache herrührt, dass ein Kurs mit konstanter Peilung keine gerade Linie ist. – Hulk

+0

Ich versuchte mit zwei Punkten auf einer geraden Linie, und es ist etwa 250 Meter, aber es sagt über 20.000 Meter, und interpolierte GPS-Punkte waren komplett aus. – jl303

Antwort

0

Sie haben einen Tippfehler in Zeile

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1)); 

Es

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
0

Ihre Methode CalculateDistanceBetweenLocations enthält eine folgende Zeile:

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1)); 

die

zu

double c = 2 * Math.atan2(Math.sqrt(a), 0.0); 
äquivalent ist, was bedeutet, dass das Ergebnis der Math.atan2 immer pi, unabhängig von dem Wert von a ist solange a positiv ist.

Daher gibt CalculateDistanceBetweenLocations immer 20015.086796020572 unabhängig von den eingegebenen Koordinaten zurück.