HACLocationManager 1.0.3

HACLocationManager 1.0.3

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released最后发布2015 年 6 月

“litoarias” 维护。



Logo

HACLocationManager 是用 Objective-C 编写的,非常易于使用和高效类。
请求使用 blocks 进行。其语法非常舒适且直观。采用单例设计模式,与 iOS7 或更高版本完全兼容。此类不应用于 GPS,仅用于在任何时候获取用户位置并获取地理编码和反向地理编码。

要求和依赖

  • iOS >= 7.0
  • 启用 ARC
  • CoreLocation 框架

安装

手动安装

  • HACLocationManager.hHACLocationManager.m 复制到您的项目
  • 手动安装 HACLocationManager

用法

请求访问位置服务的权限

iOS 7

对于 iOS 7,建议您在应用的 Info.plist 文件中设置一个字符串键 NSLocationUsageDescription,以提供如何使用位置服务的描述。

iOS 8

从 iOS 8 开始,需要将 NSLocationWhenInUseUsageDescription 键添加到您的 Info.plist 文件中。此键的值将用于显示在请求位置权限时向用户呈现 UIAlertView 的描述。有关更多信息,请参阅 Apple 文档

基本操作是在您的 Info.plist 文件中添加单个条目。添加 NSLocationWhenInUseUsageDescription 键,并选择类型为 String。您为此条目输入的值将在第一次尝试确定用户位置时作为文本显示在 UIAlertView 中。最后,它应该看起来像这样

Added entry to Info.plist

请求权限

当您想要独立于任何操作请求位置权限时,请请求权限。此请求必须始终在其他操作之前执行。我建议在 AppDelegate 中进行此操作。

[[HACLocationManager sharedInstance]requestAuthorizationLocation];
实例类 & 设置超时

您可以为请求中的更新配置超时时间,默认为5秒。

HACLocationManager *locationManager = [HACLocationManager sharedInstance];
locationManager.timeoutUpdating = 6;

获取用户位置

通过定位区块获取,并更新最后获取的位置。第一个是可选的,只有当您的应用需要时才使用。

请求获取位置
[locationManager LocationQuery];
更新位置
locationManager.locationUpdatedBlock = ^(CLLocation *location){
  NSLog(@"%@", location);
};
结束更新位置
locationManager.locationEndBlock = ^(CLLocation *location){
  NSLog(@"%@", location);
};
未获取到位置
locationManager.locationErrorBlock = ^(NSError *error){
  NSLog(@"%@", error);
};

地理编码

请求用户位置地理编码
[locationManager GeocodingQuery];
获取用户地址

可以接收多个地点标记,所以返回一个数组。

locationManager.geocodingBlock = ^(NSArray *placemarks){

    CLPlacemark *placemark = (CLPlacemark *)placemarks[0];

    NSLog(@"%@",[NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
       placemark.subThoroughfare ? placemark.subThoroughfare : @"",
       placemark.thoroughfare ? placemark.thoroughfare : @"",
       placemark.postalCode ? placemark.postalCode : @"",
       placemark.locality ? placemark.locality : @"",
       placemark.administrativeArea ? placemark.administrativeArea : @"",
       placemark.country ? placemark.country : @""]);
};
未获取到地址
locationManager.geocodingErrorBlock = ^(NSError *error){
  NSLog(@"%@", error);
};

反向地理编码

请求反向地理编码
[locationManager ReverseGeocodingQueryWithText:@"1755 Embarcadero Road Palo Alto, CA 94303"];
响应

可以接收多个地点标记,所以返回一个数组。

locationManager.reverseGeocodingBlock = ^(NSArray *placemarks){
  for (int i = 0; i < [placemarks count]; i++){
     CLPlacemark * thisPlacemark = [placemarks objectAtIndex:i];
     NSLog(@"%@", thisPlacemark);
  }
};
失败区块
locationManager.reverseGeocodingErrorBlock = ^(NSError *error){
  NSLog("%@", error);
};

获取最后存储的位置

您还可以获取用户在无法获取位置的情况下存储的最新位置。

NSLog(@"%@",locationManager.getLastSavedLocation);

获取两点之间的路线。

您可以在示例项目中尝试更改参数,更改纬度和经度的参数。也可以选择交通方式,步行或汽车,默认为汽车。

[[HACLocationManager sharedInstance]RoutesBetweenTwoPointsWithUserLat:40.4376751
                                                                    lngUser:-3.7044201
                                                                    latDest:40.0619721
                                                                    lngDest:-2.1480249
                                                               transporType:automovile
                                                          onCompletionBlock:^(NSArray * routes, NSError *error){
        if(!error){

            [routes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                MKRoute *route = obj;

                MKPolyline *line = [route polyline];

                [self.mapView addOverlay:line];

                NSLog(@"Rout Name : %@",route.name);

                NSLog(@"Total Distance (in Meters) :%f",route.distance);

                NSArray *steps = [route steps];

                NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);

                [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                    NSLog(@"Rout Instruction : %@",[obj instructions]);

                    NSLog(@"Rout Distance : %f",[obj distance]);

                }];
            }];
        }else{
            NSLog(@"%@", [error localizedDescription]);
        }

    }];

享受 :D

贡献

  1. Fork它(https://github.com/[我的github用户名]/HACLocationManager/fork
  2. 创建您的功能分支(git checkout -b my-new-feature
  3. 提交您的更改(git commit -am '添加一些功能'
  4. 推送到分支(git push origin my-new-feature
  5. 创建一个新的Pull Request