CLLocationManager tracks wrong Location (Track Me) - iphone

I am implementing Track me option in my code.CLLocationManager is not working as expected.when I start app remain at the same position ,CLLocationManager changes around 20-30 meters with-in 1 min minutes..then I remain Constant.
And If i change my position to track same thing happen in starting 1 min CLLocationManager moves 20-30 min extra then moves with my speed..
Why this happening..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 0.0001;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
-(void)start {
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation {
[self processLocationChange:newLocation fromLocation:oldLocation];
}
-(void)processLocationChange:(CLLocation*)newLocation fromLocation:oldLocation {
if (newLocation != oldLocation) {
NSLog(#"Moved from %# to %#", oldLocation, newLocation);
CLLocation* lastKnownLocation = NULL;
if ([self.locationPoints count] > 0) {
lastKnownLocation = [self.locationPoints objectAtIndex:[self.locationPoints count] - 1];
}
else {
lastKnownLocation = newLocation;
self.bottomLeft = newLocation.coordinate;
self.topRight = newLocation.coordinate;
}
// Check for new boundaries
CLLocationCoordinate2D coords = newLocation.coordinate;
if (coords.latitude < bottomLeft.latitude || coords.longitude < bottomLeft.longitude) {
self.bottomLeft = coords;
NSLog(#"Changed bottom left corner");
}
if (coords.latitude > topRight.latitude || coords.longitude > topRight.longitude) {
self.topRight = coords;
NSLog(#"Changed top right corner");
}
double speed = fabs(newLocation.speed);
double deltaDist = fabs([newLocation distanceFromLocation:lastKnownLocation]);
double newAvgSpeed = (self.totalDistance + deltaDist) / ((double)[self getElapsedTimeInMilliseconds] / 1000.0);
double accuracy = newLocation.horizontalAccuracy;
double alt = newLocation.altitude;
NSLog(#"Change in position: %f", deltaDist);
NSLog(#"Accuracy: %f", accuracy);
NSLog(#"Speed: %f", speed);
NSLog(#"Avg speed: %f", newAvgSpeed);
self.totalDistance += deltaDist;
self.currentSpeed = speed;
self.avgSpeed = newAvgSpeed;
self.altitude = alt;
NSLog(#"Delta distance = %f", deltaDist);
NSLog(#"New distance = %f", self.totalDistance);
// Add new location to path
[self.locationPoints addObject:newLocation];
// Update stats display
[self.first.start1 updateRunDisplay];
// Update map view
[self updateMap:lastKnownLocation newLocation:newLocation];
}
}

I faced the same problem in my current Pedometer app. I stretched, banged my head for couple of days. Then I found out that CLLocationManager is not able to track <5 meter distance and location generate updates. I kept self.locationManager.distanceFilter =2.0; and it gave me location updates even device was stationary. So I just changed distancefilter to 5.0 meter and it started working great. Try taking 5 meters it should work, I tested and all my wrong notifications issues vanished:
self.locationManager.distanceFilter =5.0;
You are taking self.locationManager.distancefilter=0.0001 which is I suppose out of capacity for CLLocationManager to track such a minor movement. Also you need to filter out old locations i.e. cached location updates as mentioned in Location Awareness Guide by Apple. I have used this condition in my code to filter all events which are older than 5 seconds.
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation=[locations lastObject];
NSDate* eventDate = currentLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if(abs(howRecent)<5.0 && self.currentLocation.horizontalAccuracy<=10 && self.currentLocation.horizontalAccuracy>0)
{
//you have got fresh location event here.
}
}

I think give the distance filter effective with this
self.locationManager.distanceFilter = kCLDistanceFilterNone;
and you can start updating location method but also try this method these both methods are required to get exact location
[locationManager startMonitoringSignificantLocationChanges];

Related

Re routing user in iphone navigation app

I am working on a navigation app that will give turn by turn navigation.
I am using google directions API to plot best route between points. Apart from source and destination user can add up to 8 waypoints. I have stored all the coordinates in an array and in didUpdateLocations method i am checking the distance between current location and coordinates stored in array. If the distance is less than 5 meters then the user is going in the right direction
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *myLocation = [locations lastObject];
BOOL goingWrongWay = YES;
//Searching Current Location in Coordinates Array
for(int i=0;i<self.decodedArray.count;i++)
{
CLLocation *tempLoc = [self.decodedArray objectAtIndex:i];
//NSLog(#"distance=%f", [myLocation distanceFromLocation:tempLoc]);
if([myLocation distanceFromLocation:tempLoc]<=5)
{
goingWrongWay = NO;
break;
}
}
if(goingWrongWay)
self.title = #"Wrong Way";
else
self.title = #"On Track";
NSLog(#"YES");
if(isShowingRoute)
{
NSLog(#"YES");
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
longitude:myLocation.coordinate.longitude
zoom:19.0];
[mapView animateToCameraPosition:camera];
}
}
Is there any other way to detect whether the user is going in the right direction or not as i have to Re-route user. BTW i am using google maps SDK for ios

path tracing on map in ios

Some one help me out please I am stuck on it from last few days...
My task is here.
In my app i need to integrate the map, as it open it should shows the current user position, as i pressed start button while walking it starts tracing path from the position where i pressed start button till the position i pressed stop.the distance covered in this interval should be traced turn by turn navigation and also wann to collect start and stop position coordinates to calculate distance.
i have added map kit framework, core location frame work, also added map view,and also implemented method to show current user position . now
here is my code
-(void)startSignificantChangeUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == self.locatioManager)
{
self.locatioManager = [[CLLocationManager alloc] init];
self.locatioManager.delegate = self;
[self.locatioManager startMonitoringSignificantLocationChanges];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// If it's a relatively recent event, turn off updates to save power
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
// If the event is recent, do something with it.
NSLog(#"latitude %+.6f, longitude %+.6f\n",location.coordinate.latitude,location.coordinate.longitude);
}
MKCoordinateRegion ref=MKCoordinateRegionMakeWithDistance([location coordinate], 250,250);
[self.myMapView setRegion:ref animated:YES];
}
Please guide me from here to trace path, and can collect coordinates also .

Can't get location from Wifi connection on iPhone

I am trying to write an app that will get a users location for a mass transit app which works great when I am above ground. When I am underground the location doesn't update even if I have a wifi and/or a cell signal. Below is the code that I am using. From what I understood the iPhone is able to get a location from wifi signal only, is this incorrect? Any help would be greatly appreciated, thank you in advance!
- (void)viewDidLoad
{
[super viewDidLoad];
//********************** Add map ******************************************
//setup location manager
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//setup map view
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 160.0f)];
mapView.showsUserLocation = YES;
mapView.userTrackingMode = MKUserTrackingModeFollow;
//run loop in background
loopTimer = [[NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:#selector(tick:) userInfo:nil repeats:YES]retain];
}
// Search for n seconds to get the best location during that time
- (void) tick: (NSTimer *) timer
{
// Check for valid coordinate
CLLocationCoordinate2D coord = mapView.userLocation.location.coordinate;
if (!coord.latitude && !coord.longitude) return;
//get coordinates to update map
[mapView setRegion:MKCoordinateRegionMake(coord, MKCoordinateSpanMake(0.005f, 0.005f)) animated:NO];
//update current location in view
currentLatView.text = [NSString stringWithFormat:#"%.2f", coord.latitude];
currentLonView.text = [NSString stringWithFormat:#"%.1f", coord.longitude];
}
The locationManager you setup in viewDidLoad is your instance of the CL Location Manager whereas MapKit uses its own instance when you set showsUserLocation to true.
So your settings for the distance filter and desired accuracy are not being used by MapKit, and in any case, you haven't started your location manager with startUpdatingLocation.
So, try starting your location manager instance and then use the delegate method
locationManager:(CLLocationManager *)manager didUpdateToLocation:
to get what your location manager says.
Here is what I came up with, seems to work well needs more testing.
- (void)viewDidLoad
{
[super viewDidLoad];
//********************** Add map ******************************************
// Create the manager object
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
// This is the most important property to set for the manager. It ultimately determines how the manager will
// attempt to acquire location and thus, the amount of power that will be consumed.
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
// When "tracking" the user, the distance filter can be used to control the frequency with which location measurements
// are delivered by the manager. If the change in distance is less than the filter, a location will not be delivered.
locationManager.distanceFilter = kCLLocationAccuracyBest;
// Once configured, the location manager must be "started".
[locationManager startUpdatingLocation];
//initialize newCoord
currentCoord = [[CLLocation alloc] initWithLatitude:0 longitude:0];
//setup map view
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 160.0f)];
mapView.showsUserLocation = YES;
mapView.userTrackingMode = MKUserTrackingModeFollow;
//create map view
[self.view addSubview:mapView];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
// test the age of the location measurement to determine if the measurement is cached
// in most cases you will not want to rely on cached measurements
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
// store all of the measurements, just so we can see what kind of data we might receive
currentCoord = newLocation;
[self tick];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// The location "unknown" error simply means the manager is currently unable to get the location.
if ([error code] != kCLErrorLocationUnknown) {
[self stopUpdatingLocation:NSLocalizedString(#"Error", #"Error")];
}
}
- (void)stopUpdatingLocation:(NSString *)state
{
[locationManager stopUpdatingLocation];
locationManager.delegate = nil;
}
- (void) tick
{
//do stuff here
}

iOS CoreLocation not working over 3G in USA only

My app is unable to determine lat/long coordinates over 3G in North America. It works perfectly when connected to Wi-Fi. This has been confirmed with both AT&T and Verizon with iOS 5. In the UK it works as expected on both 3G and Wi-Fi with o2.
I'm really stumped as to what the cause may be. I think my code is quite forgiving with regards to accuracy, but as I'm on the other side of the pond, I could be way off?
Can anyone sport any glaring mistakes in my code? (I've removed some unrelated methods).
static NSTimeInterval MaxLocationAge = 60.0; // Seconds.
static CLLocationAccuracy DesiredHorizontalAccuracy = 200.0; // Meters.
static NSTimeInterval UpdateTimeout = 30.0; // Seconds.
#implementation AFLocation
#synthesize locationManager = _locationManager, delegate = _delegate, bestEffortAtLocation = _bestEffortAtLocation, updateStartedAt = _updateStartedAt;
- (id)init
{
self = [super init];
if (self) {
CLLocationManager *manager = [[CLLocationManager alloc] init];
self.locationManager = manager;
[manager release];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
}
return self;
}
- (void)update
{
self.updateStartedAt = [NSDate date];
isUpdating = YES;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if ([[NSDate date] timeIntervalSinceDate:self.updateStartedAt] > UpdateTimeout) {
[self.locationManager stopUpdatingLocation];
if (isUpdating) {
[self updateDidTimeout];
}
isUpdating = NO;
}
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > MaxLocationAge) {
return;
}
if (newLocation.horizontalAccuracy < 0) {
return;
}
if (self.bestEffortAtLocation == nil || self.bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
self.bestEffortAtLocation = newLocation;
if (newLocation.horizontalAccuracy <= DesiredHorizontalAccuracy) {
self.bestEffortAtLocation = nil;
isUpdating = NO;
[self.locationManager stopUpdatingLocation];
[self didUpdateWithDesiredAccuracyToLocation:newLocation];
}
}
}
#end
WiFi and 3G are only used to speed up the process of getting a high accuracy (<300m) location. Without internet connectivity it can take 3-10 minutes to get a fix using GPS only. (They are used for low accuracy fixes with cell tower or wifi triangulation and also to download the satellite ephemeris data to speed up GPS signal acquisition).
The initial fix with WiFi or 3G could be around 800m accuracy that's expected, but if the device has 3G (or WiFi or Edge) connectivity and can get a strong enough signals from the GPS satellites it should be able to get a GPS position within 20-90 seconds.
Perhaps you're app isn't waiting long enough, or the user has weak GPS signal, or has a device without GPS (ie: iPhone1 or iPod Touch or non-3G iPads). In that case they won't get enough the 200m accuracy required.
You've set the CLLocationManager to use a desired accuracy of kCLLocationAccuracyBestForNavigation, but you're comparing the location's accuracy with an arbitrary accuracy of 200 meters. With kCLLocationAccuracyBestForNavigation being an opaque type, how do you know that the manager is even striving for your 200 meter accuracy? When I receive location updates, I check to see if the accuracy is less than or equal to the desired accuracy set on my location manager.

MapKit - didUpdateToLocation called but userLocation not updated

I have a MKMapView configured like so:
mapView = [[MKMapView alloc] init];
[mapView setMapType:MKMapTypeStandard];
[mapView setShowsUserLocation:YES];
[mapView setDelegate:self];
I then initialize a CLLocationManager and call startUpdatingLocation.
I am using iSimulate to send the GPS data from my phone, to the simulator, which seems to be working since the CLLocationManager delegate method is invoked with my correct GPS coordinates. However the MKMapView never moves the blue dot away from Cupertino.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"Did Update Location = %f / %f", [newLocation coordinate].latitude, [newLocation coordinate].longitude);
NSLog(#"Current User Location = %f / %f", [[mapView userLocation] coordinate].latitude, [[mapView userLocation] coordinate].longitude);
}
The above method outputs the following:
>>> Did Update Location = 40.740100 / -73.989900 # Correct
>>> Current User Location = 37.331693 / -122.030457 # Cupertino... Incorrect
Even if I manually update the userLocation's coordinate using:
[[mapView userLocation] setCoordinate:[newLocation coordinate]];
The dot still just sits on Cupertino. Am I missing something?
The problem with CLLocation manger is that is caches the old location and some time it returns the old location. To get a new location just check the time stamp of the CLLocation object if it is older than the time limit then ignore this location
-(void) locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*) oldLocation
{
NSDate* time = newLocation.timestamp;
NSTimeInterval timePeriod = [time timeIntervalSinceNow];
if(timePeriod < 2.0 ) { //usually it take less than 0.5 sec to get a new location but you can use any value greater than 0.5 but i recommend 1.0 or 2.0
[manager stopUpdatingLocation];
// process the location
} else {
// skip the location
}
}
The dot still just sits on Cupertino. Am I missing something?
Are you testing this on the simulator? Note that in the simulator, the location dot always remains in Cupertino. Try it on a device - maybe you don't have a bug at all!