我正在开发一款能够随时跟踪用户位置的iPhone应用程序。
位置可以是米的精度(最高400-500)。并且应该至少每小时更新一次(即使用户的位置没有变化,我也想跟踪它)
我想知道最好的方法是什么。
如果我使用CLLocation,我想在一小时内使用一次startupdatinglocation,然后使用startmonitoringsignificantlocationchanges。你认为这是最好的方法吗?导致最小的电池消耗
另一种选择是使用地理位置api,如geoloqi,但我认为使用实时跟踪模式一直会导致电池耗尽,而被动模式的准确度不够好(据我所知,它提供的精确度就像在城市一样你呢)
如果有人知道我可以使用推荐的api或使用CLLocation来解决我的问题的有效算法,我会很高兴。
谢谢!
答案 0 :(得分:0)
您可以通过维护一个从GPS软件读取位置的变量来跟踪位置,并使用计时器编写算法,该计时器每秒读取一次位置或您想要的任何精度
答案 1 :(得分:0)
试试这段代码我觉得这对你有用。 在.h文件中添加此代码
#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
在.m文件中添加此代码。
- (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;
}