Objective-C store longitude and latitude in variable - ios

I have this code here that gets the latitude and longitude:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
Long.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
Lat.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
my question is when I move my phone, these values change, I am looking for away to store them into a variable so they very change. Is this possible ?

Hrrrm...
Yes, it's possible. Trivial even. Simply move your currentLocation out of your function and into the #interface of your class:
#interface MyClass
{
//other instance variables here
CLLocation *currentLocation;
}
//properties and function definitions go here
#end
Then change your function:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *) newLocation
fromLocation:(CLLocation *)oldLocation
{
//Now saves to an instance variable instead of a local var.
if (currentLocation == nil)
currentLocation = newLocation;
if (currentLocation != nil) {
Long.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
Lat.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
However, it isn't clear what you're asking. That didUpdateToLocation method is a delegate method of the location manager, intended to report CHANGES in the user's location. What specific location do you want to store, and under what conditions? You want to save the very first location update you get once you ask the location manager to start updating locations? And then what do you want to do with it?
EDIT:
Note that when you first ask for location updates from the location manager the first location you get often has really horrible accuracy. (It's not uncommon for it to be off by a kilometer or more.) You need to keep checking the horizontal accuracy of the location and wait for the GPS to settle down and give reasonable readings.

Related

Location manager accuracy not very accurate

I am having an issue with sending location to other users. I using Parse.com as my backend and I use this code to get a location:
-(void)sendLocation{
if ([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation]; //kill it NOW or we have duplicates
if(!self.haveLocation) {
self.haveLocation=YES;
NSLog(#"didUpdateToLocation: %#", newLocation);
self.currentLocation = newLocation;
self.currentLocationGeoPoint= [PFGeoPoint geoPointWithLocation:self.currentLocation];
}
If I send myself a message from my current location and then open the message to view it, by comparing the current location circle with the pin dropped I can see they aren't in the same place and a fair distance apart.
I have the BOOL var haveLocation to make sure it is only refreshed once. Do I need to refresh it more times or something? Any pointers on how to make this more accurate would be really appreciated! Thanks
I tried following the apple example LocateMe:
I now have:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
// test the measurement to see if it is more accurate than the previous measurement
if (self.bestEffortAtLocation == nil || self.bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
// store the location as the "best effort"
self.bestEffortAtLocation = newLocation;
if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
[locationManager stopUpdatingLocation];
//this code after getting the location
NSLog(#"didUpdateToLocation: %#", newLocation);
}
}
}
However when I tap send location it just runs forever and doesn't stop updating.. I have locationManager.desiredAccuracy = kCLLocationAccuracyBest; set too.
The location manager may send you several updates as it improves its accuracy. This is a feature designed to give you information it as as quickly as possible, while eventually getting the best information to you (at least up to the accuracy you requested). You are actively avoiding those updates, so you are likely getting the least accurate information.
You should check the horizontalAccuracy to determine if it is accurate enough for your use.
Note that you cannot compare horizontalAccuracy to kCLLocationAccuracyBest. "Best" is a constant -1. You need to compare it to what you need (in meters), and probably set a timeout to give up if it takes too long to get there (the system may not be able to provide you an arbitrarily accurate value.)

Core Location can't get data in viewDidLoad

I wrote a simple iOS application that retrieves location information and then uses the location to request Yahoo Weather.
The problem is that even when I call the Core Location in the viewDidLoad, it won't give me the result immediately.
So why can't I get the location information?
How can I get the location information in viewDidLoad?
The pseudocode currently is something like:
- (void)viewDidLoad
{
[super viewDidLoad];
self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;
self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locManager.distanceFilter = 100;
[self.locManager startUpdatingLocation];
//won't get the current location right now, so the output will be null
NSLog(#"Current Location Longitude: %#", self.longitudeString);
NSLog(#"Current Location Latitude: %#", self.latitudeString);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
self.longitudeString = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
self.latitudeString = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
Location updates are not provided as instantly as you are expecting, you need to wait few seconds (2-3 or may be more) to get precise location update. If you want to have location data in viewDidLoad then you should init your location manager (and call startUpdatingLocation) before invoking the ViewController (since then it is not guaranteed that you will have location-data in viewDidLoad).

Use location variables in multiple actions?

I'm using CLLocationManager to input the device's speed from GPS data. Under the method "didUpdateToLocation", speed is defined as "currentLocation.speed" (code shown below). However, if I want to use the value of "currentLocation.speed" in a button press action later in the code, it cannot find the variable.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_speedOutput.text = [NSString stringWithFormat:#"%.3f", currentLocation.speed];
_longitudeOutput.text = [NSString stringWithFormat:#"%.3f", currentLocation.coordinate.longitude];
_latitudeOutput.text = [NSString stringWithFormat:#"%.3f", currentLocation.coordinate.latitude];
}
}
How do I, in essence, "publicize a variable" so I can use it in multiple actions?
Thanks so much.
If the actions are in the same class, just create a property and store the speed in it. If they are in a different class, you could create a global variable and use that to store the speed.

Core Location does't update the significant location changes

In my app(Deployment Target 5.1), i am using CoreLocation to setup a reminder feature, it will basically search the nearby items(have location attached) as device update its current location.
This feature is not working very stable at this stage, sometimes it just doesn't works at all regardless of whether device is in active, suspended and terminated states. i realized that the delegate method locationManager:didUpdateToLocation:fromLocation: didn't get call.
Is there anybody can point me a direction to make this thing work properly?
Here is my setup for the CLLocationManager
sharedLocationManager = [[CLLocationManager alloc]init];
[ESLocationManager defaultManager].delegate = someDelegateClassInstance;
[[ESLocationManager defaultManager] setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[[ESLocationManager defaultManager] startMonitoringSignificantLocationChanges];
And the implementation of the delegate callback
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// If it's a relatively recent event, turn off updates to save power
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 20.0) {
// If the event is recent,find nearby items.
NSLog(#"latitude %+.6f, longitude %+.6f\n",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude);
//reload nearby list
NSArray *nearbyItems = [self nearbyItemsForLocation:newLocation];
if (nearbyItems && nearbyItems.count ) {
UIApplicationState appState = [[UIApplication sharedApplication] applicationState];
if (appState != UIApplicationStateActive) {
[[UIApplication sharedApplication] presentLocalNotificationNow:[self localNotificationForItems:nearbyItems]];
}
}
}
}
Similar reason to this forum with region monitoring
https://devforums.apple.com/message/251046#251046
Hope this helps

CLLocationManager - Calculate real time speed on iPhone

How can I calculate real time speed on device? I've googled a lot but all I got is to calculate distance and speed after completion of journey. Can I calculate speed at runtime?
Suggestions will be much appreciated.
Thanks in advance.
here CLLocationManager class provide different field of location like, Latitude, Longitude,accuracy and speed.
i use CoreLocationController so for location update i call this bellow method
you can get current speed in - (void)locationUpdate:(CLLocation *)location method like bellow
- (void)locationUpdate:(CLLocation *)location
{
NSString *currentspeed = [NSString stringWithFormat:#"SPEED: %f", [location speed]];
}
or otherwise bellow is delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"in update to location");
NSString *currentspeed = [NSString stringWithFormat:#"SPEED: %f", [newLocation speed]];
}
also you can get example from this link...
http://www.vellios.com/2010/08/16/core-location-gps-tutorial/
i hope this help you...
:)
There is a delegate CLLocationManagerDelegate add it to your controller header file.
Initialize Location Manager and set the delegate where you implement the delegate method for location Manager like this
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // send loc updates to current class
and you can write method in your class
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"Location Speed: %f", newLocation.speed);
}
That's it you will get your location updates in the above method with the speed also, and it will trigger as frequent as possible.

Resources