This is my first time posting here so I would greatly appreciate the help. I have searched Google as well as this site for help and have not found anything quite like what I am looking for. I am trying to take the altitude
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
altitudeField.text = [NSString stringWithFormat: #"%.2f ft", newLocation.altitude * 3.2808399];
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
altitudeField.text = #"0.00 ft";
}
Which gives me something like "500.00 ft" for instance. I have another textfield that I would like to fill in based on the altitude value in altitudeField.text. Where if the altitude is <1000 ft it equals 1.08 in textField6, from 1000 to 2000 ft it equals 1.04 in textField6, and so on in 1000 ft increments...
The original altitude is in meters, but I just multiply it to get it into feet, so must I look at the original value in meters? Or can I look at the actual value in the altitudeField?
I have been trying to manipulate some standard if-else statements I have found but I get nothing but errors. Any help or direction would be greatly appreciated.
Why not just do this:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
double altitudeFt = newLocation.altitude * 3.2808399;
altitudeField.text = [NSString stringWithFormat: #"%.2f ft", altitudeFt];
double otherValue = 1.08 - 0.4 * floor(altitudeFt / 1000);
otherField.text = [NSString stringWithFormat:#"%.2f", otherValue];
}
It sounds like you need to create a variable to store the numeric value of your altitude. You can then make what ever adjustments/conversions/formatting necessary when you set a particular field's text.
Related
I am interested in grabbing my users current Latitude and Longitude coordinates, and displaying them literally as a NSSString in a UILabel on the view.
I don't need any MKMapView or to show anything graphically, just to display the coordinates in a UILabel. Is this possible?
Could anyone provide a starting block for me?
Thanks
Ya, its possible. Just import #import <CoreLocation/CoreLocation.h> and declare the delegate <CLLocationManagerDelegate>. then you can get the values in following delegate mathod.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
CLLocationCoordinate2D location=newLocation.coordinate;
NSString *s = [NSString stringWithFormat:#"%f,%f",location.latitude,location.longitude];
}
There is the CoreLocation framework which does this job. You can get the user's current location by implementing this delegate.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
Follow the steps:
Add MapKit.framework to your project
add to .h #import "CoreLocation/CoreLocation.h" and #import "MapKit/MapKit.h"
Use delegates as, #interface yourInterface : UIViewController < MKMapViewDelegate, CLLocationManagerDelegate >
Now Add following method to your .m file
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self setMapCenter:newLocation.coordinate];
[self._mapView selectAnnotation:[[self._mapView annotations] lastObject] animated:YES];
lblLong.text = [nsstring stringWithFormat:#"%f", newLocation.coordinate.longitude];
lblLat = [nsstring stringWithFormat:#"%f", newLocation.coordinate.latitude];
[self.locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"ERROR");
}
Here mention CLLocationManager *locationManager;. good luck.
i am new to iphone
by using CLLocation i can find latitude and longitude but it is not working in iphonesimulater4.0 working in 3.2.1,please any one tell me how to calculate latitude & longitude in iphone4.0.
Thanks in advance
CLLocationCoordinate2D location =
[[[mapview userLocation] location] coordinate];
//NSLog(#"Location found from Map: %f %f",
location.latitude,location.longitude);
this concept use in directly in ur app delegte and check it ur latitude and long.
#pragma mark locationManager delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session
if(abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 120)
{
//CLLocation *test = [[CLLocation alloc] initWithLatitude:-33.857034 longitude:151.035929];
currentLocation = newLocation;
coordinates = newLocation.coordinate;
latitude = currentLocation.coordinate.latitude;
longitude = currentLocation.coordinate.longitude;
[locationManager stopUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"%#", [error description]);
}
Location only shows on a real device. You may find the values in the CLLocationManager will work in the Simulator (possibly from your exchange - although it wont be very accurate) but the blue pin on the map will always be in CA!
trying add annotation on current location when ever i want Any number of annotation at the but it work can any help me out
i try this code
- (CLLocationCoordinate2D)coordinate;
{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude =MKUserLocation.latitude; //37.786996;// Here we have to change the with current location
theCoordinate.longitude = MKUserLocation.longitude;
return theCoordinate;
}
Make use of CLLocationManagerDelegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
[map setShowsUserLocation:TRUE];
map is the object of MKMapView.
Use this delegate method
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if (userLocation_) {
[userLocation_ release];
}
userLocation_ = [newLocation retain];
}
Can I get the magnetic heading value on the following delegate? If so, how can I get it into a UILabel?
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
}
Yup:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
myLabel.text = [NSString stringWithFormat:#"%f", [newHeading magneticHeading]];
}
In More iPhone Programming book, the author does:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if ([newLocation.timestamp timeIntervalSince1970] < [NSDate timeIntervalSinceReferenceDate] - 60)
locationCoordinate = newLocation.coordinate;
return;
...
To make sure the data was taken in the last minute. Two questions:
1) What is the if statement doing. It seems like on the left hand side, you are getting the time difference in seconds between when this method fires and the 1970 date. Then on the right hand side, you get the difference in seconds between the 2001 date and now minus 60 seconds. So to me, the if statement would never be valid since the data on the left is always going to be a greater amount of seconds. Or am I understanding it wrong?
2) What does return in a void function do? Is that considered good coding here? Thx.
I don't know what is going on here, its confusing statement, I have use something like this
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if( abs(howRecent) > 1.0 )
return;
////process your event here
}
It is right to put return in void method, as I don't want to execute the statement next for some conditions..the same code can be written as
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if( abs(howRecent) < 1.0 ){
///process your event
}
}
it just depends upon your need.
for the if block :
the value return by timeIntervalSinceReferenceDate could be negative. see the description .
If the receiver is earlier than the
reference date, the value is negative.
So if condition could be false.
For the return statement.
if you want your function to return the control to the calling function before reaching the function closed bracket on some condition. See the eg.
-(void) SomeFunction
{
if(Condition1)
{
return;
}
}