views:

839

answers:

2

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?

A: 

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.

corprew
+2  A: 

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

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:

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

Claus

Claus Broch
Using this: CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:bonusOne.latitude longitude:bonusOne.longitude]; double distance = [loc1 getDistanceFrom:newLocation]; if(distance <= 10000000)Is the only way I seem to be able to get it to think that the 2 coordinates are the same. Any number less than 10000000 and it goes to the 'else'. Any idea why this is ?
Kiksy
Have you looked at the values for each of the coordinates? It sounds like they might be somewhere else than what you think they are.
Claus Broch
turns out it was my own mistake. I had accidently duplicated a long and lat. Thanks, it works perfectly now!
Kiksy