您的位置:首页 > 其它

[汇文教育]定位系统的实现

2015-03-30 22:16 176 查看
xcode模拟器中不能实现自动定位,需要到真机上运行。

IOS8获取当前经纬度、地理位置

/*集成说明:

1、在plist添加

NSLocationAlwaysUsageDescription = 将根据您的地理位置信息,提供精准服务

NSLocationWhenInUseUsageDescription = 若不允许,您将无法使用地图定位等相关的功能


2、导入CCLocationManager.h头文件,使用CLLocationManager
代理方法;

- (void) getLocationCoordinate:(LocationBlock) locaiontBlock  withAddress:(NSStringBlock) addressBlock
{
self.locationBlock = [locaiontBlock copy];
self.addressBlock = [addressBlock copy];
[self startLocation];
}


- (void) getLocationCoordinate:(LocationBlock) locaiontBlock
{
self.locationBlock = [locaiontBlock copy];
[self startLocation];
}
<strong>CLLocationManager 代理方法</strong>
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];

CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks,NSError *error)
{
if (placemarks.count > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
_lastCity = [NSString stringWithFormat:@"%@%@",placemark.administrativeArea,placemark.locality];
[standard setObject:_lastCity forKey:CCLastCity];//省市地址
NSLog(@"______%@",_lastCity);

_lastAddress = [NSString stringWithFormat:@"%@%@%@%@%@%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare,placemark.subThoroughfare];//详细地址
NSLog(@"______%@",_lastAddress);

}
if (_cityBlock) {
_cityBlock(_lastCity);
_cityBlock = nil;
}
if (_addressBlock) {
_addressBlock(_lastAddress);
_addressBlock = nil;
}

}];

_lastCoordinate = CLLocationCoordinate2DMake(newLocation.coordinate.latitude ,newLocation.coordinate.longitude );
if (_locationBlock) {
_locationBlock(_lastCoordinate);
_locationBlock = nil;
}

NSLog(@"%f--%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
[standard setObject:@(newLocation.coordinate.latitude) forKey:CCLastLatitude];
[standard setObject:@(newLocation.coordinate.longitude) forKey:CCLastLongitude];

[manager stopUpdatingLocation];

}

<strong>系统定位</strong>
-(void)startLocation
{
if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
_manager=[[CLLocationManager alloc]init];
_manager.delegate=self;
_manager.desiredAccuracy = kCLLocationAccuracyBest;
[_manager requestAlwaysAuthorization];
_manager.distanceFilter=100;
[_manager startUpdatingLocation];
}
else
{
UIAlertView *alvertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"需要开启定位服务,请到设置->隐私,打开定位服务" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alvertView show];

}

}


3、通过block回调获取经纬度、地理位置等

*/

-(void)getLat
{
__block __weak ViewController *wself = self;

if (IS_IOS8) {

[[CCLocationManager shareLocation] getLocationCoordinate:^(CLLocationCoordinate2D locationCorrrdinate) {

NSLog(@"%f %f",locationCorrrdinate.latitude,locationCorrrdinate.longitude);
[wself setLabelText:[NSString stringWithFormat:@"%f %f",locationCorrrdinate.latitude,locationCorrrdinate.longitude]];

}];
}

}

-(void)getCity
{
__block __weak ViewController *wself = self;

if (IS_IOS8) {

[[CCLocationManager shareLocation]getCity:^(NSString *cityString) {
NSLog(@"%@",cityString);
[wself setLabelText:cityString];

}];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: