开发者

iPhone - Google map coordinates and double precision

Using http://maps.google.com/maps/geo?q=.... return latitude and longitude like those : 48.8616441,2.3448885

in a return string like this one : 200,8,48.8616441,2.3448885

When parsed a into a function that returns a CLLocationCoordinate2D :

    NSArray* listItems = [locationString componentsSeparatedByString:@","];

    CLLocationDegrees latitude = kInexistantLatitude;
    CLLocationDegrees longitude = kInexistantLongitude;

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
        latitude = [[listItems objectAtIndex:2] doubleValue];
        longitude = [[listItems objectAtIndex:3] doubleValue];
    }

    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;

    return location;

it gives into the debugger :

latitude = 48.861644099999999 longitude = 2.3448885000000002

I store many items like this one into an array, and at the end, I parse the array to write them into a plist file as dictionary items ( tag) (I use a NSString appendFormat that gives, like a NSLog) :

NSLog(@"%f %f", location.latitude, location.longitude);

48.861644 2.344889

At some moment, I will load that dictionary, and开发者_如何学编程 I will store that read stuff into a CLLocationCoordinate2D to display it on a MKMapView.

So I have 2 problems :

1) How may I store the returned value given by the http request without loosing a digit, and keeping my function ?

2) If I acheiev to do so, how may I read the stuff and store it into a CLLocationCoordinate2D without loosing again a digit ?


You are not losing a digit of precision. When you use

NSLog(@"%f %f", location.latitude, location.longitude);

If you do not specify a precision, %f automatically truncates your number to 6 decimal places as per the ANSI C standard.

Try

NSLog(@"%.8f %.8f", location.latitude, location.longitude);

This should return the same number as the return string.


If you've happen to have a problem with the precision of the primitive double type.

In computing, a exactly precise floating point number doesn't exist (you can't have infinite precision within a defined number of bytes), but it will generally have the appearance of precision (like you saw with the debugger output). You generally don't have to worry about this, since the lost in precision is negligible.

You can learn more about this in the Wikipedia entry.

However, when you NSLog or appendFormat:, you can specify the exact number of digits you want to print. In this case if you want 7 digits, you'll write :

NSLog(@"%.7f %.7f", location.latitude, location.longitude);


The same problem i had.The condition i had was,i will get lat long from JSON and upon clicking on map i ll have to send the lat long back to server.The server responded with "wrong location" message.Assigning to cllocationcoordinate restricted the precision.I solved it by , having the lat long data from JSON as NSString. Create an object class to hold the NSString equivalent of your lat and long."Dont store the lat long obtained from JSON into cllocationcoordinate, you may have to loose precision.

NSString * lattodeletetest;
NSString * longtodeletetest;
lattodeletetest=[dict objectForKey:@"Lat"];
longtodeletetest=[dict objectForKey:@"Lon"];
NSArray *array2=[[NSArray alloc] initWithObjects:lattodeletetest,longtodeletetest, nil];
NSString * coordinateString2=[array2 componentsJoinedByString:@","];
safe.strLatLong=coordinateString2;
NSLog(@"%@",safe.strLatLong); //prints 12.758493847753464,71.488293492438438

By this way it is possible to preserve the lat and long obtained from JSON without edits.

To use it as CLLOcationCoordinate2d , use

 NSString *str=strLatLong;
 NSArray *array=[str componentsSeparatedByString:@","];
 lattodeletetest=[array objectAtIndex:0];
 longtodeletetest=[array objectAtIndex:1];
 CLLocationCoordinate2D location=CLLocationCoordinate2DMake([lattodeletetest doubleValue],[longtodeletetest doubleValue]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜