简体   繁体   中英

Tracking user location all the time algorithm for an iphone app

I'm developing an iphone app that tracks the user location all the time.

The location can be in accuracy of handerts of meters (up to 400-500). And should be updated at least once an hour (even if there is no change in user's location, I would like to keep track on it)

I would like to know what is the best way to do that.

If I use CLLocation, I would like to use startupdatinglocation once in an hour and then use startmonitoringsignificantlocationchanges. Do you think it's the best way to do that causing minimum battery drain

The other option is using a geolocation api like geoloqi but i assume that using the realtime tracking mode all the time would cause a battery drain and the passive mode is not good enough accuracy (as i understood it gives accuracy like in wich city are you)

I would be glad if someone has any idea for a recommended api i could use or an efficient algorithm to use CLLocation to solve my problem.

Thank you!

您可以通过维护一个变量来跟踪位置,该变量从GPS软件读取位置并使用计时器编写算法,该计时器每秒或您想要的精度读取位置

Try this code I think This will be work for you. In .h file add this code

#import "CoreLocation/CoreLocation.h"
#import "MapKit/MapKit.h"

@interface AddressAnnotation : NSObject<MKAnnotation>
{
 CLLocationCoordinate2D coordinate;

  NSString *mTitle;
  NSString *mSubTitle;
}
 @end
 @interface NearestPropertyMatchViewController : UIViewController<MKMapViewDelegate> {

 CLLocationManager *locationManager;
 IBOutlet MKMapView *searchMap;

 NSMutableArray *searchListArray;
 AddressAnnotation *addAnnotation;

}

-(CLLocationCoordinate2D) addressLocation:(NSString *)str;
@property (nonatomic, retain) CLLocationManager *locationManager;

@end

In .m file add this code.

   - (void)viewDidLoad
   {
     [super viewDidLoad];
     locationManager = [[CLLocationManager alloc] init];
     locationManager.delegate = self;
      locationManager.desiredAccuracy = kCLLocationAccuracyBest;
     locationManager.distanceFilter = kCLDistanceFilterNone;
     [locationManager startUpdatingLocation];

     CLLocation *location = [locationManager location];


     CLLocationCoordinate2D coordinate = [location coordinate];

      NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
      NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

     NSLog(@"dLatitude : %@", latitude);
     NSLog(@"dLongitude : %@",longitude);
     MKCoordinateRegion region;
       MKCoordinateSpan span;
      span.latitudeDelta=100;
     span.longitudeDelta=100;
     region.span=span;


for(int i=0;i<[regestrationArray  count];i++)
{
    CLLocationCoordinate2D location = [self addressLocation:[regestrationArray objectAtIndex:i]];


    region.center=location;
    addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
    [searchMap addAnnotation:addAnnotation];

    [searchMap setRegion:region animated:TRUE];
    [searchMap regionThatFits:region];   
    }

   -(CLLocationCoordinate2D) addressLocation :(NSString *)str
    {
     NSError* error = nil;
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [str  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:@","];

double latitude = 0.0;
double longitude = 0.0;

if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
    latitude = [[listItems objectAtIndex:2] doubleValue];
    longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
    //Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
NSLog(@" lati=%f lon=%f",latitude,longitude);

return location;

   }

    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
   {
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM