Frage

Ich möchte Azimut (Kurs) des entfernten Objekts (auf der Karte) kennen. Ich habe Koordinaten dieses entfernten Objekts. Ich habe versucht, dies zu tun:

CLLocation *distLoc = [CLLocation alloc] initWithLatitude:40.725405 
                                                longitude:-74.003906]; // NY city

NSLog(@"loc: %f", distLoc.course);

Die Ausgabe ist: loc: -1.0...

aber wieso?

Ich möchte diesen Kurs aus meiner aktuellen Richtung kennen. Ich habe auch Standort -Update mit startUpdatingLocation Methode- und Delegierteraktualisierung.

Was ich falsch mache?

Warum kann ich nicht einfach entferntes Objekt aus meiner aktuellen Position bekommen? Vielen Dank.

War es hilfreich?

Lösung

-Course gibt die Richtung eines sich bewegenden Objekts zurück. Sie rufen also den aktuellen Standort auf dem aktuellen Standort an. Es gibt Ihnen keine Richtung von Ihrem Standort zu einem anderen Ort. Wenn sich NYC nicht bewegt, wird es immer -1 zurückkehren

Wenn Sie möchten, dass die Kompass -Überschrift Sie bewegen, können Sie dies tun:

#define RAD_TO_DEG(r) ((r) * (180 / M_PI))

...    

CLLocationCoordinate2D coord1 = currentLocation.coordinate;
CLLocationCoordinate2D coord2 = distLoc.coordinate;

CLLocationDegrees deltaLong = coord2.longitude - coord1.longitude;
CLLocationDegrees yComponent = sin(deltaLong) * cos(coord2.latitude);
CLLocationDegrees xComponent = (cos(coord1.latitude) * sin(coord2.latitude)) - (sin(coord1.latitude) * cos(coord2.latitude) * cos(deltaLong));

CLLocationDegrees radians = atan2(yComponent, xComponent);
CLLocationDegrees degrees = RAD_TO_DEG(radians) + 360;

CLLocationDirection heading = fmod(degrees, 360);

Andere Tipps

Verwenden Sie den folgenden Code, um den Kurs zu erhalten

CLLocation *distLoc =  [[CLLocation  alloc] initWithCoordinate:loc altitude:location.altitude horizontalAccuracy:location.horizontalAccuracy verticalAccuracy:location.verticalAccuracy  course:location.course speed:location.speed timestamp:location.timestamp];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top