Objective C / iPhone 比较 2 个 CLLocations /GPS 坐标
有一个应用程序可以成功找到您的 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”
有什么想法吗?
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.
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.
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:
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'
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,直接比较浮点数时应该小心。由于它们的定义方式,当您初始化它们时,内部值可能完全,这意味着它们很少会相同。相反,您应该检查它们之间的差异是否低于某个阈值,例如,
另一种选择可能是通过计算距离来检查该人距所需位置有多远。这还可以考虑到 GPS 坐标并不完全正确的事实,但即使在良好的条件下也可能相差 10 米:
Claus
Generally you should be careful with comparing floating point numbers directly. Because of the way they are defined, the internal value may not be exactly as 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
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:
Claus
为什么你不直接比较bonusOne.latitude和newLocation.coordinate.latitude?您将浮点数转换为字符串,然后将其与浮点数进行比较,这就是您收到该错误的原因。
另外,考虑到 gps 单位往往会稍微跳动,您可能想要
:测量 BonusOne 和 newLocation.coordinate 之间的距离(使用三角形斜边的毕达哥拉斯定理,没有理由比这更准确)当在这么小的比例上时,如果您觉得挑剔,请使用地图套件距离测量功能)并指定其小于一定数量。
b:将纬度和经度四舍五入到一定位数,以便在 100 英尺之内即可。
其中任何一个都比依赖两个浮点数相等更适合您,这在软件中通常都是有问题的,特别是当您测量的设备具有高噪声水平时。
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.
Also, given that the gps unit tends to jump around a bit, you probably want to either
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.
b: round the latitude and longitude off to a certain number of digits so that being, within, say 100 feet would work.
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.