Kik*_*ksy 4 iphone xcode objective-c cllocation
有一个应用程序可以成功找到您的GPS位置,但我需要能够将该GPS与GPS位置列表进行比较,如果两者相同,那么您将获得奖励.
我以为我有它工作,但似乎没有.
我有'newLocation'作为你所在的位置,我认为问题是我需要能够分离newLocation的long和lat数据.
到目前为止,我试过这个:
NSString *latitudeVar = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.latitude];
NSString *longitudeVar = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.longitude];
Run Code Online (Sandbox Code Playgroud)
GPS位置列表的示例:
location:(CLLocation*)newLocation;
CLLocationCoordinate2D bonusOne;
bonusOne.latitude = 37.331689;
bonusOne.longitude = -122.030731;
Run Code Online (Sandbox Code Playgroud)
然后
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];
}
Run Code Online (Sandbox Code Playgroud)
这会产生一个错误'无效操作数到二进制==有strut NSstring和CLlocationDegrees'
有什么想法吗?
Cla*_*och 19
通常,您应该小心直接比较浮点数.由于它们的定义方式,内部值可能与初始化它们不完全相同,这意味着它们很少相同.相反,您应该检查它们之间的差异是否低于某个阈值
if(fabs(latitude1 - latitude2) <= 0.000001)
...
Run Code Online (Sandbox Code Playgroud)
另一种选择可以是通过计算距离来检查人与所需位置的距离.这也可以考虑到GPS的坐标不完全正确的事实,但即使在良好的条件下也可能相差10米:
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
double distance = [loc1 getDistanceFrom:position2];
if(distance <= 10)
...
Run Code Online (Sandbox Code Playgroud)
克劳斯