Store iPhone Location as NSString? - iphone

I am fairly new to Objective-c & currently building my first App. I am trying to output an iPhones location to a string to be used in a JSON request.
I have the request built but I am unsure how to get the iPhones location, let alone into a string & the apple Documentation i am finding difficult to follow.
How can I do this?
Edit: I've seen how to implement location like so:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CLLocationDegrees latitude = newLocation.coordinate.latitude;
CLLocationDegrees longitude = newLocation.coordinate.longitude;
}
However i am unsure where to put this, this isn't an object is it?
I don't need to initialize it?
I tried editing this to return the newLocation as a stirng instead of void, but how do I call it?
What input do I need into (CLLocationManager *)?

The CLLocationManager class allow you to track your current location.
If you want use it to find your location, that's what you need to do :
// Create an instance of CLLocationManager class :
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
// Set the delegate of your instance :
locationManager.delegate = self; // Set your controller as a <CLLocationManagerDelegate>.
// Now update your location :
[locationManager startUpdatingLocation];
Now, everytime a CLLocationManager instance did update, the delegate method is called :
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
Which means everytime your locationManager update its location, this method is called.
Now you can overwrite the method by adding in your .m file :
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// Do whatever you want here
}
For example, if you want to store your current coordinates you can do :
Declare in the .h :
float latitude;
float longitude;
Then complete the delegate method (write this in .m) :
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
latitude = [manager location].coordinate.latitude;
longitude = [manager location].coordinate.longitude;
}
Now you can store your values in NSString like #amar answered.
I have edited this answer like 6 times so I hope this will answer your question and help you :D.

All you need is [NSString stringWithFormat:#"%f",<your float>];

Related

How Can I Get Current Location on iOS? [duplicate]

This question already has answers here:
how to get the current location position in map on iphone
(2 answers)
Closed 9 years ago.
I can't get current location. When I launch my app in different place, App can get last location. But I don't want to last location. If you close the app and restart it, now app can get current location. How can I get current location even if it is first launch of application?
- (void)viewDidLoad
{
[super viewDidLoad];
[locationManager startUpdatingLocation];
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
locationManager.delegate=self;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
location = [locationManager location];
CLLocationCoordinate2D coord;
coord.longitude = location.coordinate.longitude;
coord.latitude = location.coordinate.latitude;
lat = coord.latitude;
longt = coord.longitude;
}
You are doing [locationManager startUpdatingLocation]; before setting its delegate
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
And implement its delegate method
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
}
You should read the documentation provided in the Location Awareness Programming Guide.
Specifically, when you ask for the current location, the system returns the last known location right away so you can do something useful with it. If you don't care about past locations, you can discard it and only use more recent location information by looking at the timestamp property of the CLLocation returned to determine how recent it is.
You should really read the CLLocationManager documentation.
Wat you are doing will not work, since it will take some time determine the device location.
Therefor you will need to wait until the CLLocationManager notifies you that a location has been determent.
You will need to implement the CLLocationManagerDelegate which will tell you if a location is determent or if the location determination failed.
Also you should also check if location can be determent with:
if ([CCLocationManager locationServicesEnabled]) {
// The location services are available.
}
You should also check wether you are authorize to use the location services with [CCLocationManager authorizationStatus].

I can't get the gps coordinates for a persistent period of time

I have implemented the standard method of retrieving the coordinates from the gps using - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation.
The problem is that this function is called only on initialization and not throughout the life of the program. Is this normal?
In android you would implement a listener and you would get data instantly.
Is this not the wright way how i'm doing it? If it is, what could be the problem? (btw i've checked, i don't stopUpdatingLocation)
I have a CLLocationManager inherited class named testing and initialize it
testing* cllm = [[testing alloc] init];
cllm.delegate = self;
i later start the updating
[cllm startUpdatingLocation];
self.locationManagerDelegate = delegate;
and later is called
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
but after that it isn't called anymore. I need it to be called frequently so that i may calculate the distance to a certain point X from where i am.
Agreed with #Matt, without more code the best solution I can offer is this to tell it to update every time the device is moved with:
[self.locationManager setDistanceFiler:kCLDistanceFilterNone]
Update
I went through past projects and found the code I believe you are looking for assuming your location manager subclass is working properly
- (void)viewDidLoad {
[super viewDidLoad];
//Location
// create new location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// start location manager
[self.locationManager startUpdatingLocation];
}
-(void) distanceBetweenUserandPin {
CLLocation *currentUserLocation = [[CLLocation alloc] initWithLatitude:_currentLocation.latitude longitude:_currentLocation.longitude];
CLLocation *currentPinLocation = [[CLLocation alloc] initWithLatitude:_pinLocation.latitude longitude:_pinLocation.longitude];
CLLocationDistance distanceBetweenUserAndPinMeters = [currentUserLocation distanceFromLocation:currentPinLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//This successfully saves Lat, Long Data to a point location
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSLog(#"%f, %f", location.latitude, location.longitude);
//This assigns the value of location to the ivar _currentLocation
_currentLocation = CLLocationCoordinate2DMake(location.latitude, location.longitude);
NSLog(#"%f, %f", _currentLocation.latitude, _currentLocation.longitude);
}
First, it seems strange to me that you would use a subclass of CLLocationManager, since I'm not sure what benefit that provides you. Assuming that's not the problem, however...
From the CLLocationManager documentation:
This method returns immediately. Calling this method causes the
location manager to obtain an initial location fix (which may take
several seconds) and notify your delegate by calling its
locationManager:didUpdateToLocation:fromLocation: method. After that,
the receiver generates update events primarily when the value in the
distanceFilter property is exceeded. Updates may be delivered in other
situations though. For example, the receiver may send another
notification if the hardware gathers a more accurate location reading.
What's happening is that it is being called once for the initial position fix, but it isn't calling again because other conditions haven't changed. If a user doesn't move anywhere, then new location data won't be provided since it will be the same as last time (with a few exceptions as mentioned in the docs).
When you're testing your app, make sure that you try moving around and changing your location to produce an update. If that doesn't work, try experimenting with the desiredAccuracy and distanceFilter properties:
You start standard location services by calling the
startUpdatingLocation method. This service is most appropriate for
applications that need more fine-grained control over the delivery of
location events. Specifically, it takes into account the values in the
desiredAccuracy and distanceFilter property to determine when to
deliver new events.
Other than that, I'd guess it might have to do with how you're subclassing CLLocationManager. Providing some of that code might help.

MKMapView annotation position update problem

I need to track user current location with realtime refreshrate
I have one function with two solutions for that.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
# ifdef Variant_1
if(m_currentLocation)
[m_Map removeAnnotation:m_currentLocation];
else
m_currentLocation = [MKPlacemark alloc];
[m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
[m_Map addAnnotation:m_currentLocation];
[m_Map setCenterCoordinate:m_currentLocation.coordinate animated:YES];
# else //Variant_2
if(m_currentLocation == nil)
{
m_currentLocation = [MKPlacemark alloc];
[m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
[m_Map addAnnotation:m_currentLocation];
}else
{
[m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
//[m_currentLocation setCoordinate:newLocation.coordinate];
}
[m_Map setCenterCoordinate:m_currentLocation.coordinate animated:YES];
# endif
}
Variant_1 works good but when you move fast the location sing on the map blinks.
Variant_2 does not blink but does not move location sing however moves map.
Where is the problem?
In Variant_1, it probably blinks because you're doing a removeAnnotation and then an addAnnotation instead of just modifying the coordinates of the existing annotation.
In Variant_2, initWithCoordinate returns a new MKPlacemark object with those coordinates. It doesn't update the properties of the object you are calling the method on.
What happens if you run the setCoordinate line instead?
A separate question is why not use the MKMapView's built-in ability to show the current user location? Just do m_Map.showsUserLocation = YES; at the start. You don't need CLLocationManager to get the user's current location if you are using the MKMapView anyway.
I think you'll still need to center the map on the user's current location using one of the map view delegate methods:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}

How to get the current location or GPS coordinates with Core Location?

How do I get the current location or GPS coordinates with the Core Location framework?
first of all you need a class implementing the protocol CLLocationManagerDelegate. So at least you need to implement the method:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
...
}
After that create an instance of CLLocationManager, set the delegate and start updating the location:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self; //SET YOUR DELEGATE HERE
locationManager.desiredAccuracy = kCLLocationAccuracyBest; //SET THIS TO SPECIFY THE ACCURACY
[locationManager startUpdatingLocation];
After calling startUpdatingLocation, your implementation of locationManager: didUpdateToLocation: fromLocation gets called as soon as a location fix occurs. The parameter newLocation contains your actual location. BUT the location manager will fix a location as soon as possible, even if your specifed accuracy is not given. For this case you have to check the accuracy of the new location by your own.
cheers,
anka

CLLocation manager Gives me old position

i used CLLoction manager to get location.but when ever i start my location then it gives me oldlocation which is store in device previously.i want to reset the location for this i used flag on first launch of application i used newlocation and call stopupdateinglocation and startupdatinglocation method.But oldlocation is not changed it show old location.
Every CLLocation has a timestamp property that you can use to filter all location updates that are too old for your usage.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSTimeInterval age = [newLocation.timestamp timeIntervalSinceNow];
if(age < SOME_CONSTANT_IN_SECONDS)
{
//Use location
}
}
You can't clear the old location, but you can check the horizontalAccuracy to know if this is a good location (current) or something old and irrelevant.