2

目前我在一个项目中工作,它的要求是使用wifi网络或蜂窝网络在不使用gps的情况下每200m间隔获取当前位置信息,特别是纬度和经度值,因为它会消耗更多的电池寿命。

这在 ios 最新版本中是否可行。

如果哪位有什么想法,请分享给我,谢谢。

4

6 回答 6

1

看看CLLocationManager,那将能够告诉您用户所在的位置。

。H

#import <CoreLocation/CoreLocation.h>
@property(nonatomic,retain) CLLocationManager *locationManager;

.m

- (void)viewDidLoad
{
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;

        //let the user know the purpose
        locationManager.purpose = @"Enable location services";
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
        NSLog(@"User latitude: %f",locationManager.location.coordinate.latitude);
        NSLog(@"User longitude: %f",locationManager.location.coordinate.longitude);

        [locationManager startUpdatingLocation];
}
于 2013-02-05T10:29:48.720 回答
1

获取每 200m 位置信息的唯一方法是 CLLocationManager 的 startUpdatingLocation。但它消耗大量电池。

但是,当位置发生变化时,有一些不同的方法可以获取您的位置。

CLLocationManager 的 startMonitoringSignificantLocationChanges。

这是一个链接

于 2013-02-05T10:31:49.623 回答
1

位置管理器协议参考

https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html

1.在Appdelegate中

#import <CoreLocation/CoreLocation.h>

在@interface 文件中

CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocationManager *locationManager;

并添加协议CLLocationManagerDelegate协议。

2.在.m中实现这些功能。

@synthesize locationManager;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];  
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 1.0;
    [self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
  // Show an alert or otherwise notify the user
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
}

注意:如果要调试首先在模拟器中设置当前位置在调试--->位置--->自定义位置。

于 2013-02-05T10:26:44.013 回答
0

您必须startMonitoringSignificantLocationChanges使用仅使用 wifi 或蜂窝网络的核心定位方法!

于 2013-02-05T11:17:23.670 回答
0
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {


[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];    
[self performSelector:@selector(stopUpadateLocation)];


CLLocation *location = [locationManager location];
CLLocationCoordinate2D coord;
coord=[location coordinate];
NSLog(@"coord %f %f", coord.latitude, coord.longitude);
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=json", newLocation.coordinate.latitude, newLocation.coordinate.longitude];

NSURL *url = [NSURL URLWithString:urlString];
NSString *locationString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

NSDictionary *dic=[locationString JSONValue];
NSLog(@"locationString:%@",locationString );
[strAddr setString:[AppUtility removeNull:[NSString stringWithFormat:@"%@",[[[dic valueForKey:@"Placemark"] objectAtIndex:0] valueForKey:@"address"]]]];
[txtNear setText:strAddr];

}

- (void)startUpdateLocation{
[locationManager startUpdatingLocation];

}

- (void)stopUpadateLocation{
[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];

}

于 2013-02-05T10:53:54.760 回答
0

它的要求是在不使用gps的情况下,使用wifi网络或蜂窝网络以每200m间隔获取当前位置信息,特别是纬度和经度值,因为它会消耗更多的电池寿命

的文档CLLocationManager有关于距离和 GPS 硬件的说明:

... 将位置事件的所需精度设置为一公里,使位置管理器能够灵活地关闭 GPS 硬件并仅依赖 WiFi 或蜂窝无线电。

对于不到 200 米的距离,您可能需要在这里推出自己的解决方案。

于 2013-02-05T10:35:27.583 回答