現象:
使用CLLocationManager定位。
//
// GPSViewController.m
// Fly Survey
//
// Created by dinghongyan on 13-7-5.
//
//
#import "GPSViewController.h"
#import "ZYFileUtil.h"
#import "ZYDateUtil.h"
#import <CoreLocation/CoreLocation.h>
#import "ZYMyLog.h"
@interface GPSViewController ()<CLLocationManagerDelegate>
@end
@implementation GPSViewController
-(id)init{
if (self = [super init]) {
if ([CLLocationManager locationServicesEnabled]) {
self.lm = [[CLLocationManager alloc] init];
self.lm.delegate = self;
self.lm.desiredAccuracy = kCLLocationAccuracyBest;
}
}
return self;
}
-(void)startGPS{
if (self.lm) {
[self.lm startUpdatingLocation];
}
}
-(void)stopGPS{
if (self.lm) {
[self.lm stopUpdatingLocation];
}
}
//iOS6.0以上推薦使用方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此處locations存儲了持續更新的位置座標值,取最後一個值爲最新位置,如果不想讓其持續更新位置,則在此方法中獲取到一個值之後讓locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
[self saveLocation:currentLocation];
}
//iOS6.0以下
- (void) locationManager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation{
[self saveLocation:newLocation];
}
- (void)saveLocation:(CLLocation *)newLocation
{
//緯度
NSString *latitude = [[NSString alloc] initWithFormat:@"%f",newLocation.coordinate.latitude];
//經度
NSString *longitude = [[NSString alloc] initWithFormat:@"%f",newLocation.coordinate.longitude];
//保存定位信息
NSString *timeNow = [ZYDateUtil timeNow:@"yyyy-MM-dd HH:mm:ss"];
[[ZYMyLog sharedMyLog] appendString:[NSString stringWithFormat:@"更新位置:(%@,%@)",latitude,longitude]];
self.longitude = longitude;
self.latitude = latitude;
self.time = timeNow;
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSString *timeNow = [ZYDateUtil timeNow:@"yyyy-MM-dd HH:mm:ss"];
[[ZYMyLog sharedMyLog] appendString:[NSString stringWithFormat:@"定位出錯:%@",error]];
self.time = timeNow;
self.longitude = @"";
self.latitude = @"";
[self.lm stopUpdatingLocation];
}
@end
iPhone有手機卡,開啓GPS和WIFI,可以很快定位。
iPhone沒有手機卡,開啓GPS和WIFI,1分鐘內無法定位。
iPad開啓GPS和WIFI,1分鐘內無法定位。(室外)
暫時無解。
2015-1-28更新:
其實跟手機什麼狀態沒有關係。恰好有手機卡的iPhone是7.1的,而沒有手機卡的iPhone和iPad都是8.0以上的,因此看起來奇怪。
實際的原因是,iOS 8中定位有了變化。導致iOS 7可用,iOS 8定位失敗。
iOS 8的適配見:http://blog.csdn.net/hzxpyjq/article/details/43231435