iPhone SDK: Track users location using GPS - iphone

I have a few questions about CoreLocation and GPS.
First, what method in core location is used to continually get the users current coordinates? And at what interval should these be retrieved?
Second, should these coordinates be pushed into a NSMutableArray each time they are received, so that the array of coordinates will represent the users path?
Thanks, just wanting to get started getting me mind around this.

A very abbreviated version:
First, adopt the <CLLocationManagerDelegate> protocol in your .h, and #import <CoreLocation/CoreLocation.h>.
Then in .m go:
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(#"%f %f ", here.latitude, here.longitude);
}
Your -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation method will get pinged every time Core Location has something to say to you, which should happen every few seconds. those CLLocation objects contain info about accuracy, so you can screen for good points in that method.
Be sure to call [locationManager stopUpdatingLocation] and then [locationManager release] at some point!
Good luck finding yourself!

The best way to is read the CLLocationManager Class Reference, which links to several example projects. The short version:
Set the delegate property to a class that will receive location updates.
Implement the CLLocationManagerDelegate protocol in the delegate.
Call the appopriate methods to start updating location and/or heading.

You are able to define what range is acceptable for accuracy as well as how often you wish to receive automatic updates (based on a distance from last point mechanism). Also you can just turn off the location manager and turn it back on at will thru some use of a timer.
As for saving the locations to build a path, its not that simple. You will continually get GPS locations at first until the desired accuracy is achieved, and for any points in the future you may get more than one that is inaccurate before you get a good location. So building a list of these points will basically just be a list of their path, along with a lot of extra points.
You could solve this by saving only those points that have the accuracy you desire, but its an imperfect world in this respect.
Best case I would suggest you keep two lists, one is the path and the other is a running list of locations where you are comparing until you get a highly accurate location, then putting that on your path list.
Some of the example projects do things along these lines, do check them out.

You will have to do the following:
If device cannot access internet
Get co-ordinates from GPS device
Send these co-ordinates via SMS
Receive and decode SMS message at the SMS gateway you have to configure to receive info from device.
Update the info on the application database or any other store you are using
Update the position on map with latest info
If device can access internet
Get co-ordinates from GPS device
Connect to application server (may be some service) and upload information
Update the info on the application database or any other store you are using
Update the position on map with latest info

Related

Does a MKMapView have its own location Manager?

There is a CLLocationManager i have created, and receiving the gps updates from that , but my doubt is whether the mapview has its own locationmanager, in that case there will be two location managers right? Whether this will affect my application? in case its having its own location manager
MKMapView has its own location manager.That's why you can directly show user location on map like this myMapView.showsUserLocation=YES;
Why are you using separate location manager im MKMapView? You can capture current user location by this way.
CLLocationCoordinate2D location = [[myMapView userLocation] location].coordinate;
double currentLat = location.latitude;
double currentLong = location.longitude;
Also I dont think using separate location manager in MKMapView cause any problems.
Yes, it does. And it shouldn't effect your application as long as you choose one and stick with it. Although, I will point out that having two (or one empty) is terrible coding practice. Use the following function to access your MKMapView's locationManager:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

is iphone Simulator capable of showing heading and latitude , longitude?

I am using iphone simulator 4.2 and try to display or NSLog Heading and other Location services attributes e.g. Latitude, Longitude, altitude, horizontalAccuracy, VerticalAccuracy, speed. but its not showing the correct parameters and Incase of Heading its actually not firing the event.
as its execute CLLocation code
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.delegate locationUpdate:newLocation];
}
and not executing CLHeading code
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
[self.delegate headingUpdate:newHeading];
}
and when I put breakpoint on these both codes it never touch CLHeading code. I am updating location and heading in init.
- (id) init{
if (self!=nil) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
[self.locationManager startUpdatingHeading];
}
return self;
}
The problem is that I do not know that is due to simulator or there is any problem in code?
please help.
CLLocationManager needs additional hardware and hence wont work on simulator. However you can test that using the method described in this other SO question.
From the documentation:
Some location services require the presence of specific hardware on the given device. For example, heading information is available only for devices that contain a hardware compass. This class defines several methods that you can use to determine which services are currently available.
This answer can be updated for anyone using Xcode 4.2. It is still in beta status, but if you are a paid developer you will have access to it. Also, if you are a paid developer, there are some good videos from WWDC 2011 that explain how to use the simulator for location simulation.
WWDC 2011
See What's New in Core Location and Testing Your Location-Aware App Without Leaving Your Chair
**Note: Please note again that only paid developers have access to these videos
It looks like the behaviour is by default
It fires Location but not heading.
I have not tested my application in actual hard ware to confirm my though...
Anthony Desa

Iphone CLLocationManager horizontalAccuracy update. When? and how do I know it?

I need to display horizontalAccuracy of currentLocation on the map in my app. To do that I put a line of code in viewDidLoad();
(_currentLocation is CLLocationManager's location.)
label4Accuracy.text = [NSString stringWithFormat:#"Accuracy: %.fm", _currentLocation.horizontalAccuracy];
And it display's accuracy.
The problem is the accuracy does not change. The horizontalAccuracy changes its accuracy if we wait enough (not always though).
What I want my app to be able to do is that it changes the accuracy and display it on the map in real time.
I expected there might be a method somewhere near CLLocationManager like;
- (CLLocationAccuracy *)horizontalAccuracyUpdated():
but there isn't. So my question is where should I put the line of code above to display the horizontalAccuracy of currentLocation that changes in accordance to the actual device's current accuracy?
It's unclear from your question whether you're using your own CLLocationManager instance, or getting current location from an MKMapView.
If you're just using a MKMapView, your map view is going to make a delegate call to the method - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation every time it updates the user's location, and that userLocation variable it passes will have a .horizontalAccuracy parameter that gives the accuracy in meters.
So set your view controller as the map view's delegate and implement that method and watch there for updates.
If you're actually using a CLLocationManager, the same is true except the delegate method is - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation.

Calculate and display distance between userlocation and known point in Table View

I have a table view with a list of hotel, and i want put in cell.detailTextLabel.text the distance beetween userlocation and hotel. How can obtain the coordinates of userlocation?
I see on web that i need to use CLLocationManager but i don't understand how and where implement in my table view.
Then,to get the distance,i do a "getDistancefrom" between userLocation and the coordinates of the hotel ?
Thanks
That's really quite a big question ;)
This tutorial looks pretty good - I'd go through it and see if it helps. If not, come back to stackoverflow and ask more questions.
Well you have the right approach in mind, just need to clear out the details.
The simplest way is to have your table view controller adhere to the CLLocationManagerDelegate protocol. You then setup a location manager and have it start updating the user's location:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
Then you just implement this method from the delegate:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
In there you will be notified whenever a user's location changes. So then you can grab the newLocation longitude and latitude and compute the distance using all your hotels' longitudes and latitudes.
Make sure you import the core location framework to your project as I don't think it's there by default.
Good luck.

Will location manager find the current location repeatedly after it finds the current location in iPhone?

I am new to iPhone development. I want to know the working process of the CLLocationManager in determining the current location. I have used the below code for the button event to find the current location.
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // Tells the location manager to send updates to this object
[locationManager startUpdatingLocation];
IN
- (void)locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{ }
I have determined the lat and long values and loaded the current location.
It is working fine. If after updating to the new location, will it be once again called to get the current location? If iPhone is not able to find the current location, will then once again search for current location?
I want to know for both cases
If iPhone determines the current location.
If iPhone doesn't able to determine the current location.
According to the CoreLocationManager reference
The location service returns an
initial location as quickly as
possible, returning cached information
when available. After delivery of the
initial event notification, the
CLLocationManager object may deliver
additional events if the minimum
threshold distance (as specified by
the distanceFilter property) is
exceeded or a more accurate location
value is determined.
That seems to indicate that (1) you may received cached location information while it's still trying to determine your current location, (2) you may receive multiple updates as it determines the location more accurately.