目前我正在一个项目中工作,其要求是使用wifi网络或蜂窝网络在每200m间隔内获取当前位置信息的特定经度和纬度值不使用GPS,因为它消耗更多的电池寿命。如何获取当前使用wifi或蜂窝电话的位置信息,而无需在ios中使用GPS
这是可能的ios最新版本。
如果任何人有任何想法,请与我分享, 谢谢。
目前我正在一个项目中工作,其要求是使用wifi网络或蜂窝网络在每200m间隔内获取当前位置信息的特定经度和纬度值不使用GPS,因为它消耗更多的电池寿命。如何获取当前使用wifi或蜂窝电话的位置信息,而无需在ios中使用GPS
这是可能的ios最新版本。
如果任何人有任何想法,请与我分享, 谢谢。
位置管理器协议参考
1.In的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
{
}
注意:如果你想调试模拟器 首先设置当前位置在调试--->位置--->自定义位置。
查看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];
}
它只有这样,才能在每一个200米是CLLocationManager的startUpdatingLocation您的位置信息。但是它耗费了大量的电池。
但是有一种不同的方式来获取您的位置,当它改变。
CLLocationManager的startMonitoringSignificantLocationChanges。
这里是一个Link
其要求是使用WiFi网络或蜂窝网络,以获得当前的位置信息在每200米间隔专门的纬度和经度值,而不使用GPS,因为它会消耗更多的电池寿命
为CLLocationManager
的文档已经这样说的距离和GPS硬件:
...将定位事件的理想精度设置为1公里,可以让位置管理员灵活地关闭GPS硬件并仅依靠WiFi或小区无线电。
对于不到200米,您可能需要在此处推出自己的解决方案。
- (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];
}
您必须使用核心定位方法startMonitoringSignificantLocationChanges
它仅使用WiFi或蜂窝网络!
这不会提供所需的准确性:文档说不要期望更新,除非用户移动了500米。 –
这是仅使用蜂窝网络的唯一方法。 – CainaSouza
我不确定您是否可以在不使用gps的情况下使用蜂窝网络获取用户位置 – Will