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

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.

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;

MKMapview showing current location wrong

I am using MKMapView and it is showing my current location. in india it is showing well and exact what in google map but in US current location is showing wrong.
i am using
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
any help please ?
If you are using an MKMapView you can use its delegate method mapView:didUpdateUserLocation:
You need to set showsUserLocation property of your map view to YES.
Then the map view uses CoreLocation framework to determine current location.
It tracks userLocation by itself whenever it receives location updates and updates it periodically.

MapView snaps back to original location - help!

I have a mapView with custom annotations and just ran into a problem on a beta tester's iPhone. The mapView won't let the user move to any location.. as soon as you try to move, it snaps right back to the original coordinates.
Any idea why? It doesn't happen in the simulator, and I notice it a little bit on my own device... but it is a consistent problem on another device.
Thanks so much!
static BOOL haveAlreadyReceivedCoordinates = NO;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if(haveAlreadyReceivedCoordinates) {
return;
}
haveAlreadyReceivedCoordinates = YES;
CLLocationCoordinate2D loc = [newLocation coordinate];
[mapView setCenterCoordinate:loc];
}
Icky put you on the right track. You shouldn't be constantly resetting the map's center to whatever you get back from the location manager. The location manager will send you all sorts of updates.
You may wish to only use the update once and then ignore future updates (by setting some kind of flag), or you may wish to center the map on the user's location only when the user presses a button. In the latter case the only thing you need to do in didUpdateToLocation: is store the new location into some member variable.
The reason you don't see this in the simulator is that the location hardware simulator isn't constantly updating the way the real one is.

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.

iPhone SDK: Track users location using GPS

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