CLLocationManager Coordinates - ios

I have been working to implement route trace map for walking,biking and driving.
However, as you see in the following screenshot, my coordinate jumps all of a sudden from time to time even though I did not walk/bike or drive that location. The circle has been drawn on the image to point out the problem. My question is why all of a sudden coordinates jumps ?
Here is my implementation snapshot:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CoordinateModel *coord = [[CoordinateModel alloc] init];
coord.latitude = newLocation.coordinate.latitude;
coord.longitude = newLocation.coordinate.longitude;
ActivityType currentActivityType = [DataManager sharedInstance].activityType;
if (currentActivityType == 0) {
// walking
[appDelegate.walkingCoordinates addObject:coord];
}
else if(currentActivityType == 1) {
[appDelegate.bikingCoordinates addObject:coord];
}
else if(currentActivityType == 2) {
// driving
[appDelegate.drivingCoordinates addObject:coord];
}
self.coordinate = newLocation.coordinate;
}

I suggest you not to use the delegate method locationManager:didUpdateToLocation:fromLocation: anymore and it has been deprecated.
You should use locationManager:didUpdateLocations instead.
About your question, the location "jumping" like you mention is due to the GPS that is unable to determine the accuracy of your location during a certain time. If you record down the coordinate and also the accuracy for all the time including when you are indoor, you will realize that the accuracy when you are staying indoor is not good, you might see the accuracy 1414 when you are connected to Wifi. GPS does not work well when you are indoor. So, your code has to be smart enough to only draw a path or send the coordinate to the server when only the coordinate is good enough.
The below code are some of the criteria that I use to filter out the bad coordinates.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 30.0)
continue;
//Select only valid location and also location with good accuracy
if(newLocation!=nil&&theAccuracy>0
&&theAccuracy<2000
&&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){
self.myLastLocation = theLocation;
self.myLastLocationAccuracy= theAccuracy;
NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
[dict setObject:[NSNumber numberWithFloat:theLocation.latitude] forKey:#"latitude"];
[dict setObject:[NSNumber numberWithFloat:theLocation.longitude] forKey:#"longitude"];
[dict setObject:[NSNumber numberWithFloat:theAccuracy] forKey:#"theAccuracy"];
//Add the valid location with good accuracy into an array
//Every 1 minute, I will select the best location based on accuracy and send to server
[self.shareModel.myLocationArray addObject:dict];
}
}
}
After a certain period (eg: 3 minutes), I will again choose the best coordinate from self.shareModel.myLocationArray before drawing the coordinate on map and send the coordinate to the server.
You may see the full solution and sample project from here: Background Location Services not working in iOS 7
Don't forget to upvote if my answer is good enough. ;)

Same Problem has been still in the code.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
iNEAT_o_GamesAppDelegate *appDelegate = (iNEAT_o_GamesAppDelegate *)[[UIApplication sharedApplication] delegate];
CoordinateModel *coord = [[CoordinateModel alloc] init];
ActivityType currentActivityType = [DataManager sharedInstance].activityType;
for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 30.0)
continue;
//Select only valid location and also location with good accuracy
if(newLocation!=nil&&theAccuracy>0
&&theAccuracy<2000
&&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){
coord.latitude = theLocation.latitude;
coord.longitude = theLocation.longitude;
if (currentActivityType == 0) {
// walking
[appDelegate.walkingCoordinates addObject:coord];
}
else if(currentActivityType == 1) {
[appDelegate.bikingCoordinates addObject:coord];
}
else if(currentActivityType == 2) {
// driving
[appDelegate.drivingCoordinates addObject:coord];
}
}
}
}

Related

Find the distance between my location and a point in ios

I am using a method to get my location:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation *currentLocation = newLocation;
NSLog(#"%#", currentLocation);}
//new method to find the distance
-(void) zoomToNearestSpot: (NSArray *) spot location: (CLLocation *)myLocation
{
myLocation = currentLocation;
spot = chSpot;
for (int i=0; i< spot.count; i++)
{
ChargingSpots *mySpot = [spot objectAtIndex:i];
CLLocation *thePoint = [[CLLocation alloc]initWithLatitude:[mySpot.LocationLat doubleValue] longitude:[mySpot.LocationLong doubleValue]];
CLLocationDistance dist = [myLocation distanceFromLocation:thePoint];
NSLog(#"%f", dist);
you get here **distance** in meter so in assumption **20 miles = 32187 meter**
put condition such like
if(distance <= 32187)
{
// under 20 miles you can add the zoom level
}
else
{
// out of 20 miles
}
}
}
First, I need to show all of the spots on the map by zooming out according to their number then find the nearest spots and zoom in the camera on them in.
you have more the two coordinate
1) current (user Location)
2) another coordinate ..
Here distanceFromLocation: method is use for get distance between two location.
CLLocationDistance distance = [currentLOcation distanceFromLocation:anotherLocation];
for example where you need add the following lines
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
for (int i=0; i<chSpot.count; i++)
{
ChargingSpots *spot = [chSpot objectAtIndex:i];
NSString *locLat = spot.LocationLat;
NSString *locLong = spot.LocationLong;
CLLocation *LocationAtual = [[CLLocation alloc] initWithLatitude: locLat longitude: locLong];
CLLocationDistance distanceBetween = [currentLocation
distanceFromLocation: LocationAtual];
NSString *tripString = [[NSString alloc]
initWithFormat:#"%f",
distanceBetween];
NSLog(#"DIST: %f", tripString);
}
}
you can use this code
1.) CLLocation *loc=[[CLLocation alloc]initWithLatitude:spot.LocationLat longitude:spot.LocationLong];
2.) CLLocationDistance distanceInMeters=[currentLocation distanceFromLocation:loc];
This will help.ThankYou

How to generate my traveled path using CLLocationManager on google map in iOS?

User has to create his own path on google map wherever he move step by step the route is generated.
please have look on my code snippet:
- (void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray )locations
{
CLLocation* location1 = [locations lastObject];
if (self.endPointArray == nil)
{
self.endPointArray = [[NSMutableArray alloc]init];
}
NSString *pointString=[NSString stringWithFormat:#"%f,%f",location1.coordinate.latitude,location1.coordinate.longitude];
[self.endPointArray addObject:pointString];
NSLog(#"end point array :%#",self.endPointArray);
GMSMutablePath *path = [GMSMutablePath path];
for (int i=0; i<self.endPointArray.count; i++)
{
NSArray *latlongArray = [[self.endPointArray objectAtIndex:i]componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#","]];
[path addLatitude:[[latlongArray objectAtIndex:0] doubleValue] longitude:[[latlongArray objectAtIndex:1] doubleValue]];
}
if (self.endPointArray.count>2)
{
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor redColor];
polyline.strokeWidth = 3.0f;
polyline.map = _mapView;
}
}
I have create route with help of above code.but sometimes we get unexpected coordinates from CLLocation. If we are on road (route) it's gives perfect but if we are not on road like in home it gives wrong coordinates
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
NSTimeInterval age = -[location.timestamp timeIntervalSinceNow];
if (age > 120) return; // ignore old (cached) updates
if (location.horizontalAccuracy < 0) return; // ignore invalid updates
if(self.oldLocation == nil)
{
self.oldLocation = location;
return
}
CLLocationDistance distance = [location distanceFromLocation: self.oldLocation];
if (distance >= 10 && location.horizontalAccuracy <=20) //you can change 10 to 20 if you want more frequent updates
{
// add location to path
self.oldLocation = location; // save newLocation for next time
}
}
Try using this code. Its from my project and it works just fine. Use it according to your need. Hope it helps you.

How to Remove Bad GPS Coordinate

As shown in the image below, there is a sudden jump in GPS coordinate while I keep track of user activity (walking, biking or driving).
I do not know how to fix this problem even though I have already attempted multiple ways to solve, but as a reference I also posted my code below.
In this image screenshot, I parked my car and walked to campus and came to my office, but it shows me that I went to Harvest Ln which is not right.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
iNEAT_o_GamesAppDelegate *appDelegate = (iNEAT_o_GamesAppDelegate *)[[UIApplication sharedApplication] delegate];
CoordinateModel *coord = [[CoordinateModel alloc] init];
ActivityType currentActivityType = [DataManager sharedInstance].activityType;
for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 30.0)
continue;
//Select only valid location and also location with good accuracy
if(newLocation!=nil&&theAccuracy>0
&&theAccuracy<2000
&&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){
coord.latitude = theLocation.latitude;
coord.longitude = theLocation.longitude;
if (currentActivityType == 0) {
// walking
[appDelegate.walkingCoordinates addObject:coord];
}
else if(currentActivityType == 1) {
[appDelegate.bikingCoordinates addObject:coord];
}
else if(currentActivityType == 2) {
// driving
[appDelegate.drivingCoordinates addObject:coord];
}
}
}
}
I have added another condition for walking activity and limited only accuracy between 0-60 meters. and work perfectly fine. Thanks Allessandro and Skladek feedback above.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
iNEAT_o_GamesAppDelegate *appDelegate = (iNEAT_o_GamesAppDelegate *)[[UIApplication sharedApplication] delegate];
CoordinateModel *coord = [[CoordinateModel alloc] init];
ActivityType currentActivityType = [DataManager sharedInstance].activityType;
for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 30.0)
continue;
//Select only valid location and also location with good accuracy
if(newLocation!=nil&&theAccuracy>0
&&theAccuracy<2000
&&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){
coord.latitude = theLocation.latitude;
coord.longitude = theLocation.longitude;
if (currentActivityType == 0 && theAccuracy<60) {
// walking
[appDelegate.walkingCoordinates addObject:coord];
}
else if(currentActivityType == 1) {
[appDelegate.bikingCoordinates addObject:coord];
}
else if(currentActivityType == 2) {
// driving
[appDelegate.drivingCoordinates addObject:coord];
}
}
}
}

GoogleMap not showing user's current location properly when drawing path overlay

I am using GMSPolyline on google map to show shortest path between two points. This map does not show user's current location properly.
Here is my code. Let me know about the correction that I can make in my code. Thanks in advance.
- (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) < 0.5)
{
/////// Update your marker on your map using location.coordinate.latitude
//and location.coordinate.longitude);
NSLog(#"event time=%#",eventDate);
#pragma mark start Point of Polyline Drow
///////////////Logitude 23.03 apvathe darek point tyathe start thay etle polyline Drow thay/////
if (location.coordinate.longitude==23.03)
{
[locationManager stopUpdatingLocation];
}
[mypath addObject:[NSString stringWithFormat:#"%f,%f",location.coordinate.latitude,location.coordinate.longitude]];
GMSMutablePath *path1 = [GMSMutablePath path];
for (int i=0; i<mypath.count; i++)
{
//NSString *str1=mypath[i];
NSArray* foo = [mypath[i] componentsSeparatedByString: #","];
NSString* lat = [foo objectAtIndex: 0];
NSString* longi = [foo objectAtIndex: 1];
[path1 addCoordinate:CLLocationCoordinate2DMake([lat floatValue], [longi floatValue])];
}
GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path1];
rectangle.map = mapView_;
marker.position = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
marker.title = #"Gurukul Road,Ahmedabad";
marker.snippet = #"Gujarat";
marker.map = mapView_;
}
}
you have to be more accurate in case of latitude and longitude you are using there %f and that is not accurate as per values of latitude and longitude use this code...
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
strForCurrentLongitude= [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
strForCurrentLatitude = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
Take Double value instead of float in this line.
[path1 addCoordinate:CLLocationCoordinate2DMake([lat floatValue], [longi floatValue])];

ios: How to obtain CLLocationCoordinate2D with <CLLocationManagerDelegate>

What is the propery way to obtain CLLocationCoordinate2D?
In .h:
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
In .m
//Called when the location can be obtained
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//Get the latitude and longitude location
CLLocation *lastLocation = [locations lastObject];
_latitude = [[NSString alloc] initWithFormat:#"%f", lastLocation.coordinate.latitude];
_longitude = [[NSString alloc] initWithFormat:#"%f", lastLocation.coordinate.longitude];
}
Is it this:
_coordinate = lastLocation.coordinate;
or this:
_coordinate = manager.location.coordinate;
First read the documentation as already stated in the comments, because this is a must know for location programming.
Second pay attention that is not the first data you get from delegate, because it could be a cached data (you need to check the timestamp) or an invalid data (with horizontal accuracy less that zero).
You could use that code:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation * newLocation = [locations lastObject];
if (newLocation.horizontalAccuracy < 0) {
return;
}
NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
if (abs(interval)>5) {
return;
}
//YOUR CODE HERE
}
Hope this helps, Andrea

Resources