【问题标题】:Objective C / iPhone comparing 2 CLLocations /GPS coordinatesObjective C / iPhone比较2 CLLocations /GPS坐标
【发布时间】:2010-03-19 19:32:51
【问题描述】:

有一个应用程序可以成功找到您的 GPS 位置,但我需要能够将该 GPS 与 GPS 位置列表进行比较,如果两者相同,那么您将获得奖励。

我以为我已经成功了,但似乎没有。

我将“newLocation”作为您所在的位置,我认为问题是我需要能够分离 newLocation 的经纬度数据。

到目前为止我试过这个:

NSString *latitudeVar = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.latitude];

NSString *longitudeVar = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.longitude];

GPS 位置列表示例:

location:(CLLocation*)newLocation;

CLLocationCoordinate2D bonusOne;    

bonusOne.latitude = 37.331689;
bonusOne.longitude = -122.030731;

然后

if (latitudeVar == bonusOne.latitude && longitudeVar == bonusOne.longitude) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"infinite loop firday" message:@"infloop" delegate:nil cancelButtonTitle:@"Stinky" otherButtonTitles:nil ];    

    [alert show];
    [alert release];
}

这会出现错误“二进制的无效操作数 == 具有 strut NSstring 和 CLlocationDegrees”

有什么想法吗?

【问题讨论】:

    标签: iphone objective-c xcode cllocation


    【解决方案1】:

    通常您应该小心直接比较浮点数。由于它们的定义方式,当您初始化它们时,内部值可能不完全,这意味着它们很少会相同。相反,您应该检查它们之间的差异是否低于某个阈值,例如

    if(fabs(latitude1 - latitude2) <= 0.000001)
    ...
    

    另一种选择是通过计算距离来检查人员与所需位置的距离。这也可能考虑到 GPS 的坐标并不完全正确,即使在良好条件下也可能相差 10 米:

    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
    double distance = [loc1 getDistanceFrom:position2];
    if(distance <= 10)
    ...
    

    克劳斯

    【讨论】:

    • 使用这个:CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:bonusOne.latitude longitude:bonusOne.longitude];双距离 = [loc1 getDistanceFrom:newLocation]; if(distance
    • 您查看过每个坐标的值吗?听起来它们可能与您想象的不同。
    • 原来是我自己的错误。我不小心复制了一个 long 和 lat。谢谢,现在可以正常使用了!
    【解决方案2】:

    为什么不直接比较 bonusOne.latitude 和 newLocation.coordinate.latitude?您正在将浮点数转换为字符串,然后将其与浮点数进行比较,这就是您收到该错误的原因。

    另外,鉴于 gps 装置往往会跳动一点,您可能想要要么

    a:测量 bonusOne 和 newLocation.coordinate 之间的距离(使用勾股定理来计算三角形的斜边,对我们来说,没有理由比在这个小尺度上更准确。如果你觉得挑剔,请使用地图套件测距功能)并指定其小于一定数量。

    b:将纬度和经度四舍五入到一定数量的数字,这样在 100 英尺之内就可以了。

    其中任何一个都比依赖两个浮点数相等对您来说效果更好,这在软件中通常是有问题的,特别是当您测量的设备具有高噪音水平时会出现问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-19
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      相关资源
      最近更新 更多