views:

370

answers:

1

I'm trying to pass some lat/long values from a JSON doc to iPhone's Map Kit to then plot the points on the map. The values are coming from an NSArray:

CGFloat goLat = [valfields objectForKey: @"geo_lat"];
CGFloat golong = [valfields objectForKey: @"geo_long"];


CLLocationCoordinate2D newCoord = {golat, golong}; etc...

But I'm getting an "incompatible types" error.

My guess is I need to cast the string values from my array to CGFloat? But I'm just sure.

Any thoughts?

Thanks,

g

+1  A: 

'Casting' isn't what you need to do, but if your array (actually from your code it looks like a dictionary) contains strings, then yes, you need to convert them to a numeric format. Give this a try:

CGFloat goLat = [[valfields objectForKey: @"geo_lat"] floatValue];

From the NSString documentation:

floatValue

Returns the floating-point value of the receiver’s text as a float.

- (float)floatValue

Return Value
The floating-point value of the receiver’s text as a float, skipping whitespace at the beginning of the string. Returns HUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. Also returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.

In addition, NSString has a -doubleValue method. There is also an NSNumber class, which might be a better representation than strings for your data.

Carl Norum
Brilliant. Thank you.
givp