代码之家  ›  专栏  ›  技术社区  ›  zebra

比较cllocation课程值的iphone问题

  •  0
  • zebra  · 技术社区  · 16 年前

    我正在写一些代码来获取一些值,包括course

        -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation   *)newLocation fromLocation:(CLLocation *)oldLocation {
        //somecode
        NSString *dirString = [[NSString alloc] initWithFormat:@"%d", newLocation.course];
        int myInt = [dirString intValue];
        if ((myInt >= 0) || (myint  < 90)) {course.text =@ "N";}
        if ((myInt >= 90) || (myint  < 180)) {course.text =@ "E";}
    

    等等,但我总是检索第一个值“n”。

    我的错在哪里?

    谢谢!

    3 回复  |  直到 13 年前
        1
  •  3
  •   Jasarien    16 年前

    您可能需要将逻辑或更改为逻辑和(更改 || && )这将确保该值介于0到90或90到180之间。

    因为逻辑或,在我看来逻辑也有点缺陷,也许有些东西我不理解你的假设,但如果值是,比如200,它将通过第一个 if 因为200大于0。然后它也会通过第二个 如果 因为200大于90。它们通过是因为逻辑或。只有一个语句(>=0或<90)必须为true才能通过。

    这可以通过使用逻辑和来解决。

        2
  •  1
  •   yonel    16 年前

    你不需要通过nsstring来检查课程,但是你的bug的根本原因是课程是一个double,你应该使用 %f 在你的字符串的格式中。

    更短的:

    double theCourse = newLocation.course;
    if ((theCourse >= 0) || (theCourse  < 90)) {course.text =@ "N";}
    if ((theCourse >= 90) || (theCourse  < 180)) {course.text =@ "E";}
    

    但实际上我认为你的算法是错误的。如果航向是0<=航向<45或315<=航向<360,则您将向北行驶。

        3
  •  0
  •   zebra    16 年前

    约内尔

    谢谢,课程是学位:-( 但我不明白你的算法,读了苹果的文档我发现:

    Thus, north is 0 degrees, east is 90degrees, south is180 
    degrees, and so on. Course values may not be available on all 
    devices.
    

    对我来说这意味着

    course between 0 and 44= North;
    course between 45 and 89= NE; 
    course between 90 and 134= East; 
    course between 135 and 179= SouthEast;
    course between 180 and 234= South;
    course between 235 and 269= SouthWest;
    course between 270 and 314= West;
    course between 315 and 360 = NorthWest; - 
    

    我们说的是同一件事吗?D

    推荐文章