目标C / iPhone对比2个CLLocations / GPS坐标
-
21-09-2019 - |
题
有成功找到你的GPS位置的应用程序,但我需要能够比较与GPS位置的列表,全球定位系统,如果两者是相同的,那么你得到的奖金。
我原本以为它的工作,但它似乎没有。
我有“newLocation”的位置,你在哪里,我认为这个问题是我需要能够单独newLocation的长和纬度数据。
到目前为止IVE尝试这样:
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”的误差
任何想法?
解决方案
一般来说,你应该小心直接比较浮点数。因为它们被定义的方式,内部值可以不是准确你初始化它们意味着它们将很少是相同的。相反,你应该检查,看看它们之间的区别是低于某一阈值,例如
if(fabs(latitude1 - latitude2) <= 0.000001)
...
另一个选项可以是,检查人多远是从所希望的位置,通过计算距离。这也可能考虑到的是,从GPS坐标其实是不完全正确的,但是可能有所不同多达喜欢,即使在良好的条件10米乘:
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
double distance = [loc1 getDistanceFrom:position2];
if(distance <= 10)
...
克劳斯
其他提示
你为什么不直接比较和bonusOne.latitude newLocation.coordinate.latitude?您conevrting浮点数到字符串,然后相对于一个浮点数这就是为什么你得到这个错误。
此外,考虑到GPS单元往往会跳了一下周围,你可能想要么
一个:测量(bonusOne和newLocation.coordinate之间的距离,利用勾股定理为三角形的斜边,没有理由我们的东西比这更准确,如果你感觉挑剔这个小规模的时候,用地图试剂盒距离测量功能),并指定其超过一定量以下。
B:圆的纬度和经度关闭到一定的位数,使之中,内,说百英尺将工作
无论那些将更好地为你不是依赖于两个浮体是相等的,这是无论在软件一般问题的,并且具体地一个问题,当你测量是该装置具有高的噪声电平。