今天写到定位的功能,一切正常,发现在开始定位的时候,提示定位的信息 一闪而过, 没有时间给客户点击的时间,找了半天 才发现这个不起眼的问题
我之前的代码是
CLLocationManager * manager=[[CLLocationManager alloc] init];
manager.delegate=self;
manager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
manager.distanceFilter=1.0f;
if ([CLLocationManager locationServicesEnabled]) {
[manager startUpdatingLocation];
}
else {
NSLog(@"请开启定位功能!");
}
CLLocationManager <pre name="code" class="objc">
提示授权信息, 调用完成 跳出方法体后,<pre name="code" class="objc">manager 销毁了 所以 提示信息消失了 导致信息一闪而过!
what a fucking day!!!
// 刚刚遇到一个问题
didUpdateLocations 这个代理是不停的调用的,我该怎么获取当前经纬度呢, 这里是我拿的苹果Demo里面的一个 我拿了一部分 (虽然不知道什么意思。。。)多次调用取最优的那个, 他是实时的更新位置信息的。我实时给给一个 类值赋值,当用了这个值(例如:已经向服务器发送了,这时就可以 停止更新)
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *currlocation=[locations lastObject];
NSTimeInterval locationAge=-[currlocation.timestamp timeIntervalSinceNow];
if (locationAge>5.0) {
return;
}
if (currlocation.horizontalAccuracy<0) {
return;
}
if (bestEffortAtLocation==nil ||bestEffortAtLocation.horizontalAccuracy>currlocation.horizontalAccuracy) {
bestEffortAtLocation=currlocation;
if (currlocation.horizontalAccuracy<=manager.desiredAccuracy) {
[self stopUpdatingLocation:manager];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:manager];
}
}
CLLocation *Nowlocal=[[CLLocation alloc] initWithLatitude:bestEffortAtLocation.coordinate.latitude longitude:bestEffortAtLocation.coordinate.longitude];
currlocation=Nowlocal.locationMarsFromEarth;
CLLocation *GCLocal=[[CLLocation alloc] initWithLatitude:[_model.XMInfo_jzwlx1 doubleValue] longitude:[_model.XMInfo_jzwlx2 doubleValue]];
kilometers=[currlocation distanceFromLocation:GCLocal];
NSLog(@"upate纬度:%f",currlocation.coordinate.latitude);
NSLog(@"upate经度:%f",currlocation.coordinate.longitude);
// 解码地理位置信息
CLGeocoder *geocoder=[[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currlocation completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count]>0) {
CLPlacemark *placemark=placemarks[0];
NSDictionary *addressDic=placemark.addressDictionary;
_Addresslabel.text= _Addresslabel.text==nil?@"":[addressDic objectForKey:@"Name"];
_jingdu=[NSString stringWithFormat:@"%3.5f",currlocation.coordinate.latitude];
_weidu=[NSString stringWithFormat:@"%3.5f",currlocation.coordinate.longitude];
}
}];
}