xcode Objective C/iPhone 比较 2 个 CLLocations/GPS 坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2480081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective C / iPhone comparing 2 CLLocations /GPS coordinates
提问by Kiksy
have an app that finds your GPS location successfully, but I need to be able to compare that GPS with a list of GPS locations, if both are the same , then you get a bonus.
有一个应用程序可以成功找到您的 GPS 位置,但我需要能够将该 GPS 与 GPS 位置列表进行比较,如果两者相同,那么您将获得奖励。
I thought I had it working, but it seems not.
我以为我有它的工作,但似乎没有。
I have 'newLocation' as the location where you are, I think the problem is that I need to be able to seperate the long and lat data of newLocation.
我有'newLocation' 作为你所在的位置,我认为问题是我需要能够分离newLocation 的long 和lat 数据。
So far ive tried this:
到目前为止我试过这个:
NSString *latitudeVar = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.latitude];
NSString *longitudeVar = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.longitude];
An example of the list of GPS locations:
GPS 位置列表示例:
location:(CLLocation*)newLocation;
CLLocationCoordinate2D bonusOne;
bonusOne.latitude = 37.331689;
bonusOne.longitude = -122.030731;
and then
进而
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];
}
this comes up with an error 'invalid operands to binary == have strut NSstring and CLlocationDegrees'
这会出现错误“二进制的无效操作数 == 具有支柱 NSstring 和 CLlocationDegrees”
Any thoughts?
有什么想法吗?
回答by Claus Broch
Generally you should be careful with comparing floating point numbers directly. Because of the way they are defined, the internal value may not be exactlyas you initialize them meaning that they will very seldom be identical. Instead you should check to see if the difference between them are below a certain threshold, for instance
通常,您应该小心直接比较浮点数。由于它们的定义方式,内部值可能与您初始化它们时不完全相同,这意味着它们很少相同。相反,您应该检查它们之间的差异是否低于某个阈值,例如
if(fabs(latitude1 - latitude2) <= 0.000001)
...
Another option could be to check how far the person is from the desired location by calculating the distance. This could also take into account the fact that the coordinate from the GPS is not exactly correct, but might differ up to like 10 meters even under good conditions:
另一种选择是通过计算距离来检查此人离所需位置的距离。这也可以考虑到 GPS 的坐标不完全正确的事实,但即使在良好的条件下也可能相差 10 米:
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
double distance = [loc1 getDistanceFrom:position2];
if(distance <= 10)
...
Claus
克劳斯
回答by corprew
Why are you not comparing bonusOne.latitude and newLocation.coordinate.latitude directly? You are conevrting a floating point number to a string and then comparing it to a floating point number which is why you are getting that error.
为什么不直接比较 bonusOne.latitude 和 newLocation.coordinate.latitude 呢?您正在将浮点数转换为字符串,然后将其与浮点数进行比较,这就是您收到该错误的原因。
Also, given that the gps unit tends to jump around a bit, you probably want to either
此外,鉴于 gps 单元往往会跳动一下,您可能想要
a: measure the distance between bonusOne and newLocation.coordinate (using the pythagorean theorem for the hypotenuse of triangles, no reason to us something more accurate than that when on this small a scale. if you're feeling picky, use the map kit distance measuring function) and specify its less than a certain amount.
a:测量bonusOne和newLocation.coordinate之间的距离(使用勾股定理来计算三角形的斜边,我们没有理由比这个小比例尺更精确。如果你觉得挑剔,请使用地图套件距离测量功能)并指定其小于一定数量。
b: round the latitude and longitude off to a certain number of digits so that being, within, say 100 feet would work.
b:将纬度和经度四舍五入到一定数量的数字,以便在 100 英尺之内可以工作。
Either of those will work better for you than relying on the two floats to be equal, which is both generally problematic in software and specifically an issue when the device you're measuring is has a high noise level.
这两种方法中的任何一个都比依赖两个浮点数相等更适合您,这在软件中通常是有问题的,尤其是当您测量的设备具有高噪声水平时的问题。