C / iPhone lens comparing 2 coordinates of CLLocations / GPS

you have an application that successfully finds your GPS location, but I need to compare this GPS with a list of GPS locations, if both of them are the same, then you get a bonus.

I thought it worked for me, but it seems not.

I have "newLocation" as the place where you are, I think the problem is that I need to be able to share 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 a 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 results in the error 'invalid operands for binary files' have rack NSstring and CLlocationDegrees'

Any thoughts?

+4
source share
2 answers

Generally, you should be careful when comparing floating point numbers directly. Due to how they are defined, the internal meaning may not be the same as you initialize them, which means that they will very rarely be identical. Instead, you should check if the difference between them is below a certain threshold, for example

 if(fabs(latitude1 - latitude2) <= 0.000001) ... 

Another option would be to check how far the person is in the right place, calculating the distance. This can also take into account the fact that the GPS coordinate is not quite correct, but can be different up to 10 meters even in good conditions:

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

Klaus

+19
source

Why don't you compare bonusOne.latitude and newLocation.coordinate.latitude directly? You match a floating point number with a string and then compare it with a floating point number, so you get this error.

Also, given that the gps unit tends to move 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, there is no reason for us to be more accurate than on this small scale. If you feel picky, use the distance measurement function in the map set) and specify it less than a certain value.

b: Round the latitude and longitude to a certain number of digits to be within, say, 100 feet.

Any of them will work better for you than relying on two floats to be equal, which is generally problematic in software and, in particular, a problem when the device you are measuring has a high noise level.

0
source

Source: https://habr.com/ru/post/1304604/


All Articles