MKMapView centerCoordinate not returning proper values - iphone

In my app, I am saving coordinates from an MKMapView into a property list. After the user hits "save" I set the center coordinate of the selection view to that on the main view, and then save the mapView.centerCoodinate.latitude and longitude into a pList. However, this gives me a value like "1078114215" which the map says is not a vail coordinate. What am I doing wrong?

Saving a pointer instead of the two floats in the coordinate? Not saving as a float?

Sounds like you're accidentally mis-typing your double variable. When you add it to your dictionary to be stored as a plist be sure to transform it from a double to an NSNumber like this:
[myDictionary addObject:[NSNumber numberWithDouble:latitude] forKey:#"latitude"];
and when you retrieve it, transform it from an NSNumber to a double:
double latitude = [[myDictionary objectForKey:#"latitude"] doubleValue];

Related

Syntax for Extracting CGFloat out of NSDictionary

Just wondering what the syntax would be to extract a CGFloat out of an NSDictionary like follows:
slider.minimumValue = [filterAttributes valueForKey:kCIAttributeSliderMin];
An NSDictionary only holds objects. What kind of object would wrap a primitive like CGFloat? NSNumber would make sense. Now, since CGFloat is either a float or a double, you'll probably want to get the double value to preserve precision/range.
Therefore:
slider.minimumValue = [[filterAttributes valueForKey:kCIAttributeSliderMin] doubleValue];
You can only put OBJECTS into an NSDictionary (or NSARRAY). CGFloat is a literal (just maps to a float), so you can't put it into or retrieve it from the dictionary.
Instead, wrap it as an NSNumber (when you add it to the dictionary), which is an object.
NSNumber *sliderMin = [NSNumber numberWithFloat:kCIAttributeSliderMin]
Or using the new syntax, you can just say #kCIAttributeSliderMin or #(kCIAttributeSliderMin) to autobox as an NSNumber.
To get the value back out, you'll retrieve the object as an NSNumber then say, [myNumber floatVal] to get the NSFloat.
Finally, you probably want to say "objectForKey" not "valueForKey".
update - sorry, in your example you're treating kCIAttributeSliderMin as a key, and I'm using it as the 'value'; but I think you get the point. Store an NSNumber object; retrieve an NSNumber object. Sorry for any confusion swapping that may have caused.

read latitude and longitude from sqlite iphone

I have been struggling with this for long. I read many resources but still not able to find a clear way around it.
I have a Sqlite table with rows of latitudes and longitudes. My task is to fetch those latitudes and longitudes and put them in an array and then use later for displaying on map with all the pins. I do these "reading from database" in my AppDelegate (is this advisable or is it better to do in view controller which has the map?)
I fetch the lat and long as double values as shown below
while (sqlite3_step(selectStmt)==SQLITE_ROW){
double latitude= sqlite3_column_double(selectStmt, 1);
double longitude= sqlite3_column_double(selectStmt, 2);
CLLocationCoordinate2D coord=CLLocationCoordinate2DMake(latitude,longitude);
// I want to add this to an array, so that i can use later for annotations
}
However when i try to add "Incompatible type for argument 1 off addObject".
Is this the right way of fetching multiple coordinates from sqlite to display on maps?
Help would be appreciated
You need to use CLLocation class to store your location data.
http://developer.apple.com/library/ios/#DOCUMENTATION/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/doc/uid/TP40007126
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[array addObject:location];
[location release];
CLLocationCoordinate2D is a struct not an object and you need objects for addObject methods.
You can use the CLLocation as Evgeniy suggest or create your own object to store/retrieve those values.

Accessing methods from another class

I am new to the C objective and I'm having lots of difficulties. Hope you goys would be able to help me out.
Alright, I have a view controller class that displays the data from the external sensor plugged to an iphone. I have another database class that is supposed to grab that data and store it in an array which can be used to plot a graph.
I'm having difficulties in finding a way to capture the data captured by the view controller class method variables and using it to store in database class.
The code below is from View Controller class which captures analog signal and displays in UILabel.
(void) forceCalculationKg{
NSNumber *number = [controller. analogInValues objectAtIndex:0];
[controller enableDigitalInputs:YES];
double value = [number doubleValue];
double force;
force = 0.2908 *pow(2.718,(1.2089 * value));
double forcekg;
forcekg = force/2.2;
forceoutput.text = [NSString stringWithFormat:#" %0.1f", forcekg];
}
You didn't submit code for this question that actually compiles. But in any case, I assume your forceCalculationKg method is the function on your controller that you are calling to retrieve the sensor data? You are then applying a calculation to the data and displaying it, right?. To keep this data around, just add a NSMutableArray property to your controller and save each transformed data point to it.
-(void) forceCalculationKg {
NSNumber *number = [controller.analogInValues objectAtIndex:0];
[controller enableDigitalInputs:YES];
double value = [number doubleValue];
double force = 0.2908 *pow(2.718,(1.2089 * value));
double forcekg = force/2.2;
[self.datapoints addObject: [NSNumber numberWithDouble: forcekg]];
forceoutput.text = [NSString stringWithFormat:#" %0.1f", forcekg];
}
Where datapoints is a NSMutableArray property you add to your view controller via the #property and #synthesize keywords. You can then pass the datapoints array around your program to wherever you need it.

Casting floats and UILabel

HI -
I have a value in a UILabel and I want to pass the number to a value that is of float type.
rate = float
hourlyRate = label
Below is my code and im getting errors. I know its bad form to go from object to primitive values but there has to be a way.
Any help would be appreciated!
rate = NSNumber numberWithFloat:[hourlyRate.text rate];
NSString has a convenient floatValue method:
rate = [hourlyRate.text floatValue];
rate = NSNumber numberWithFloat:[hourlyRate.text rate];
That’s invalid because if you’re trying to send the message numberWithFloat: to NSNumber it has to be enclosed in brackets, like so:
rate = [NSNumber numberWithFloat:[hourlyRate.text rate]];
And what’s more hourlyRate.text returns an NSString. You can’t send an NSString the method rate unless you subclassed it and added that method.
This is the right way to get the float value of UILabel, try this:
rate = [hourlyRate.text floatValue];
And do you mean:
float rate;

How do you pass a float to ProgressView from another class?

I'm trying to pass ProgressView a float from a calculation made in another class. I've tried passing it by converting it to a NSDecimalNumber but I can't get it back to a float again when it reaches the destination. There's got to be a better way than this.
myUIProgressView.value = someFloat; // where someFloat is just type float between 0.0..1.0
Should work. If you're using an NSNumber to hold the value, you can use [myNSNumber floatValue]; to get its float representation.