现象:
使用 iPhone 或者 iPad 经常定位到香港(22.284681,114.158177)去。重启设备后又可以了。但是隔一会儿,又到香港去了。
//
// 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];
}
}
//iOS 8
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
//用户还没有选择是否授权
if ([self.lm respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.lm requestAlwaysAuthorization];
}
break;
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:
//有定位权限,开始定位,并计时
[self.delegate gpsStartLocating];
break;
default:
//没有定位权限,提示
[self.delegate gpsLocatedFailed];
break;
}
}
//iOS6.0以上推荐使用方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
[self saveLocation:currentLocation];
[self.lm stopUpdatingLocation];
}
//iOS6.0以下
- (void) locationManager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation{
[self saveLocation:newLocation];
[self.lm stopUpdatingLocation];
}
- (void)saveLocation:(CLLocation *)newLocation
{
//纬度
NSString *latitude = [[NSString alloc] initWithFormat:@"%f",newLocation.coordinate.latitude];
//经度
NSString *longitude = [[NSString alloc] initWithFormat:@"%f",newLocation.coordinate.longitude];
//2015-1-30 过滤定位香港的
if ([latitude isEqualToString:@"22.284681"] && [longitude isEqualToString:@"114.158177"]) {
//定位不正确,请重启定位服务
[self.delegate gpsInHongKong];
//保存定位信息
NSString *timeNow = [ZYDateUtil timeNow:@"yyyy-MM-dd HH:mm:ss"];
[[ZYMyLog sharedMyLog] appendString:[NSString stringWithFormat:@"更新位置:(%@,%@), 定位在香港,不允许通过",latitude,longitude]];
self.longitude = @"";
self.latitude = @"";
self.time = timeNow;
}else{
//保存定位信息
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];
[self.delegate gpsLocatedFailed];
}
@end
一开始认为这个是 iOS 本身的bug,因为定位就是调一个接口而已,我能干涉的并不多。而且,出现定位香港之后,我使用高德地图、苹果地图,都是定位到香港。因此我百分之百认定,就是系统bug。
直到跟老公一直争论,老公坚持认为是我的app的问题,于是用他的 iPhone(未安装我的app)搞来搞去,就是没有出现定位香港。
后来在网上搜索,也没有人遇到跟我一样的情况,我开始怀疑了。
再用测试机测试,当我的app中出现定位香港之后,使用高德地图定位,依然是香港。 这时候,我把我的app退出,再用高德地图去定位的时候,神奇的事情发生了,定位对了!果然,出问题的是我的app,不是系统bug。
在网上搜索了定位的这个坐标(22.284681,114.158177), 是一个苹果店。 于是猜想,会不会是定位没成功的时候,先返回这个值,然后继续定位的呢?
但是去掉时间限制之后,还是一直定位香港,目前无解。
======== 2015-2-3 更新 ============
解决办法:
偶然看到一篇文章:xcode4.2 模拟器定位
原来xcode里面可以模拟一个位置,检查了下自己模拟器中的设置:
果然是在香港的。难怪只要退出我的app,就定位正确了。
于是改成Don't Simulate Location,再运行我的app,果然没有问题了。
再改成别的位置,确实定位到对应的地方了。
至此,奇葩终于解开!