4

有一个应用程序可以成功找到您的 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];
}

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

有什么想法吗?

4

2 回答 2

20

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

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

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

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

克劳斯

于 2010-03-19T21:57:55.323 回答
0

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

此外,鉴于 gps 单元往往会跳动一点,您可能想要

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

b:将纬度和经度四舍五入到一定数量的数字,以便在 100 英尺之内有效。

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

于 2010-03-19T21:50:04.470 回答