Frage

Als Proof of Concept, mag ich eine Anwendung erstellen, die die aktuellen Koordinaten abrufen, berechnen die Richtung zu einem anderen Punkt und Kompass, drehe einen Pfeil Bild zu diesem Punkt im Raum zu lenken.

Ich weiß, wie Strom abzurufen zu koordinieren und das Bild durch CGAffineTransformMakeRotation zu drehen, aber ich habe finde keine Formel, um den richtigen Winkel zu berechnen.

Für Hinweise?

War es hilfreich?

Lösung

Sie müssen zunächst ein Lager berechnen. Diese Seite gibt eine saubere Formel dafür, dass:

http://www.movable-type.co.uk/scripts/ latlong.html

Dann können Sie einige einfache arithmetische tun, um die Differenz zwischen diesem Lager zu finden und die Position der iPhone wird darauf hin. Drehen Sie Ihr Bild von dieser Differenz.

Andere Tipps

Lager ist:

double bearingUsingStartCoordinate(CLLocation *start, CLLocation *end)
{
    double tc1;
    tc1 = 0.0;

    //dlat = lat2 - lat1
    //CLLocationDegrees dlat = end.coordinate.latitude - start.coordinate.latitude; 

    //dlon = lon2 - lon1
    CLLocationDegrees dlon = end.coordinate.longitude - start.coordinate.longitude;

    //y = sin(lon2-lon1)*cos(lat2)
    double y = sin(d2r(dlon)) * cos(d2r(end.coordinate.latitude));

    //x = cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)
    double x = cos(d2r(start.coordinate.latitude))*sin(d2r(end.coordinate.latitude)) - sin(d2r(start.coordinate.latitude))*cos(d2r(end.coordinate.latitude))*cos(d2r(dlon)); 

    if (y > 0)
    {
        if (x > 0)
            tc1 = r2d(atan(y/x));

        if (x < 0)
            tc1 = 180 - r2d(atan(-y/x));

        if (x == 0)
            tc1 = 90;

    } else if (y < 0)
    {
        if (x > 0)
            tc1 = r2d(-atan(-y/x));

        if (x < 0)
            tc1 = r2d(atan(y/x)) - 180;

        if (x == 0)
            tc1 = 270;

    } else if (y == 0)
    {
        if (x > 0)
            tc1 = 0;

        if (x < 0)
            tc1 = 180;

        if (x == 0)
            tc1 = nan(0);
    }
    if (tc1 < 0)
        tc1 +=360.0;
        return tc1;
}

Und für diejenigen, die für den Abstand zwischen zwei Punkten:

double haversine_km(double lat1, double long1, double lat2, double long2)
{
    double dlong = d2r(long2 - long1);
    double dlat = d2r(lat2 - lat1);
    double a = pow(sin(dlat/2.0), 2) + cos(d2r(lat1)) * cos(d2r(lat2)) * pow(sin(dlong/2.0), 2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    double d = 6367 * c;

    return d;
}

double haversine_mi(double lat1, double long1, double lat2, double long2)
{
    double dlong = d2r(long2 - long1);
    double dlat = d2r(lat2 - lat1);
    double a = pow(sin(dlat/2.0), 2) + cos(d2r(lat1)) * cos(d2r(lat2)) * pow(sin(dlong/2.0), 2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    double d = 3956 * c; 

    return d;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top