문제

먼 물체 (지도에서)의 방위각 (코스)을 알고 싶습니다. 이 먼 대상의 좌표가 있습니다. 나는 이것을 시도했다 :

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

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

출력은 다음과 같습니다. loc: -1.0...

하지만 왜?

이 과정을 현재 방향에서 알고 싶습니다. 또한 위치 업데이트가 있습니다 startUpdatingLocation 방법 및 대표 업데이트.

내가 뭘 잘못 했어?

왜 현재 위치에서 먼 물체를 얻을 수 없습니까? 감사.

도움이 되었습니까?

해결책

-코스는 움직이는 물체의 방향을 반환합니다. 따라서 사용자의 현재 위치에서 -Course를 호출합니다. 위치에서 다른 위치로의 방향을 제공하지 않습니다. 따라서 NYC가 움직이지 않으면 항상 -1을 반환합니다.

나침반 제목을 찾으려면 움직여야합니다.

#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);

다른 팁

아래 코드를 사용하여 코스를 얻으십시오

CLLocation *distLoc =  [[CLLocation  alloc] initWithCoordinate:loc altitude:location.altitude horizontalAccuracy:location.horizontalAccuracy verticalAccuracy:location.verticalAccuracy  course:location.course speed:location.speed timestamp:location.timestamp];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top