【问题标题】:Tracking user location all the time algorithm for an iphone appiPhone应用程序的始终跟踪用户位置算法
【发布时间】:2012-05-05 07:51:20
【问题描述】:

我正在开发一个一直跟踪用户位置的 iPhone 应用程序。

该位置的精度可以达到米的手数(最高 400-500)。并且应该至少每小时更新一次(即使用户的位置没有变化,我也想跟踪它)

我想知道最好的方法是什么。

如果我使用 CLLocation,我想每小时使用一次 startupdatinglocation,然后使用 startmonitoringsignificantlocationchanges。您认为这是减少电池消耗的最佳方法吗

另一种选择是使用 geoloqi 之类的地理定位 api,但我认为一直使用实时跟踪模式会导致电池耗尽,而被动模式的准确性不够好(据我所知,它提供的准确性就像在哪个城市你是)

如果有人对我可以使用的推荐 api 或使用 CLLocation 解决我的问题的有效算法有任何想法,我会很高兴。

谢谢!

【问题讨论】:

    标签: iphone geolocation


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      试试这个代码,我认为这对你有用。 在 .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;
         }
      

      【讨论】:

      • 这将显示用户当前位置,如果它工作通知我们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多