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.
Related
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.
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.)
I want to get the current location latitude and longitude without using the map, is it possible to get like that, I am not getting that from searching the internet, can any one help me to find that.
I tried this with using the core location but even I am not got any thing.
please tell me how to find the latitude and longitude, thank you.
//remember to stop before you are done, either here or in view disappearance.
- (void) dealloc
{ [locationManager stopUpdatingLocation]; }
in .h file:
CLLocationManager *locationMgr;
in .m file:on load
locationMgr =[[CLLocationManager alloc] init];
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
locationMgr.delegate = self;
if ([locationMgr respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[locationMgr requestWhenInUseAuthorization];
}
[locationMgr startUpdatingLocation];
add following delegates:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Handle location updates
CLLocationCoordinate2D location=newLocation.coordinate;
float altitude = newLocation.altitude;
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// Handle error
NSLog(#"error: %#",[error localizedDescription]);
}
may you are not authorising the location from user. thats why you are not getting anything
thanks
In Xcode 4.5 apple introduced apple new maps. My application heavliy requires map services. And I have noticed in my application it shows the wrong current location until you delete the app and reopen it shows the right current location (Sometimes it doesn't). Just to mention I was connected to 4G when it show the wrong current location. Is there a way to fix this bug because my application heavily needs the right location. Thanks in advance. If you could provide code it would help a lot...Edit: Same Issue in Apple Map App
My Code:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
{
}
if (!oldLocation)
totalDistance = 0.0;
else
totalDistance += [newLocation distanceFromLocation:oldLocation];
}
The old approach from apple docs seems still working in iOS6 (didn't notice this in my active app (it tracks user's route via gps))
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (newLocation.horizontalAccuracy < 0) return;
// proceed with coords here
}
UPDATE from the discussion:
Calculating total and current distance could be done like this (excluding some minor stuff):
// somewhere at the top
CLLocation* lastUsedLocation = nil; // last location used in calculation
CLLocation* pointA = nil; // start of the track
double totalDistance = 0; // total distance of track
double currentDistance = 0; // distance between startTrack point and current location
...
// when you start updating location:
- (void) startTracking {
lastUsedLocation = nil;
pointA = nil;
totalDistance = 0;
currentDistance = 0;
[locationManager startUpdatingLocation];
}
...
// location update callback
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return; // filter cached
if (newLocation.horizontalAccuracy < 0) return; // filter invalid
if(!pointA) pointA = [newLocation retain];
if(lastUsedLocation)
{
totalDistance += [newLocation distanceFromLocation:lastUsedLocation];
}
currentDistance = [pointA distanceFromLocation:newLocation];
[lastUsedLocation release];
lastUsedLocation = [newLocation retain];
}
If you need the option to turn off background location on purpose you disable it manually like:
- (void)applicationDidEnterBackground:(UIApplication *)application {
if(backgroundLocationDisabled)
{
[locationManager stopUpdatingLocation];
// additional stuff
}
}
you should check the timestamp .. if i understand your app logic correctly, you could do something like -
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSDate* time = newLocation.timestamp;
NSTimeInterval timePeriod = [time timeIntervalSinceNow];
if(timePeriod < SOME_MINIMUM_TIME_PERIOD ) { //for eg: timePeriod < 1.0
totalDistance += [newLocation distanceFromLocation:oldLocation];
} else {
// skip.. or do what you do on a 'location-not-updated' event
}
}
I have noticed the same problem with Xcode 4.4+. The problem only occurs (randomly, or so it seems) within Xcode: if you upload the app to the App Store, this is not a problem anymore. In the meantime, please file a bug.
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.