ios的 定位 及高德地图定位

最近休息了一段时间。老有朋友问我定位怎么做的。由于每个需求不同。总结如下。希望用到的朋友能做个参考。

关于定位可分为 普通定位和地图定位。

普通定位只需要显示城市信息或者位置信息。

地图定位需要显示并且标注大头针在地图。

其实原理是一样的。只是展示方式不同而已。

废话不多说了  附上代码

 - (id)init
{
    self = [super init];
    if (self) {
        //初始化定位
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = 1000.0;
        _locationSucsess = NO;
    }
    return self;
}

其次还需要考虑到版本不同的情况下

ios8的版本需要设置plist文件。

添加字段:

NSLocationAlawaysUsageDescription

NSlocationWhenInUseDescription

对应实现代码:

[locationManager requestAlwaysAuthorization];

- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

  如果定位失败回掉的方法。
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
   
    NSLog(@"locError:%@", error);
    _locationSucsess = NO;
    [manager stopUpdatingLocation];
    NSLog(@"Error: %@",[error localizedDescription]);
     [[NSNotificationCenter defaultCenter]postNotificationName:@"failToLocation" object:nil];
    switch([error code]) {
        case kCLErrorDenied:
            //Access denied by user(发通知给getaround页面)
            NSLog(@"Access to Location Services denied by user");
          
            //Do something...
            break;
        case kCLErrorLocationUnknown:
            //Probably temporary...
            NSLog( @"Location data unavailable");
            //Do something else...
            break;
        default:
           NSLog( @"An unknown error has occurred");
            break;
    }
}

另外需要实现一个协议内的方法

- (void)locationManager:(CLLocationManager *)manager
 didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;


到现在基本完成一半了。


最重要的需要提一点则是 关于地理编码 和逆向地理编码的。

经纬度信息转为位置信息。

位置信息转为经纬度信息。


这个可以根据具体需要的时候进行使用。

#pragma mark -- 根据地理位置信息获取经纬度 CLGeocoder
-(void)GetLatitudeAndLongitudeBasedOnLocation:(NSString*)loncation{
   
    CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
    [myGeocoder geocodeAddressString:loncation completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if ([placemarks count] > 0 && error == nil) {
             NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
             CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
             NSLog(@"coordAdressLongitude = %f", firstPlacemark.location.coordinate.longitude);
             NSLog(@"coordAdressLatitude = %f", firstPlacemark.location.coordinate.latitude);
                          
         }
         else if ([placemarks count] == 0 && error == nil) {
             NSLog(@"Found no placemarks.");
         }
         else if (error != nil) {
             NSLog(@"An error occurred = %@", error); }
     }];
   
}

#pragma mark -- 根据经纬度获取地理位置详细信息  CLGeocoder
-(void)GetmoreInformationInAccordanceWithTheLatitude:(CLLocationDegrees)latitude AndLongitudeLocation:(CLLocationDegrees)longitude{
   
    CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    CLGeocoder *geocoder=[[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:newLocation
                   completionHandler:^(NSArray *placemarks,
                                       NSError *error)
     {
         CLPlacemark *placemark=[placemarks objectAtIndex:0];
         NSLog(@"地址:%@\n country:%@\n postalCode:%@\n ISOcountryCode:%@\n ocean:%@\n inlandWater:%@\n locality:%@\n subLocality:%@ \n administrativeArea:%@\n subAdministrativeArea:%@\n thoroughfare:%@\n subThoroughfare:%@\n",
               placemark.name,
               placemark.country,
               placemark.postalCode,
               placemark.ISOcountryCode,
               placemark.ocean,
               placemark.inlandWater,
               placemark.administrativeArea,
               placemark.subAdministrativeArea,
               placemark.locality,
               placemark.subLocality,
               placemark.thoroughfare,
               placemark.subThoroughfare);
        
         
     }];
}

如果打印地理位置信息。则已经完成了定位操作。 其余的则需要根据自己的需求进行 将获取的信息存储并相应赋值即可。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值