测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
Released最后发布 | 2015 年 6 月 |
由 “litoarias” 维护。
HACLocationManager 是用 Objective-C 编写的,非常易于使用和高效类。
请求使用 blocks 进行。其语法非常舒适且直观。采用单例设计模式,与 iOS7 或更高版本完全兼容。此类不应用于 GPS,仅用于在任何时候获取用户位置并获取地理编码和反向地理编码。
HACLocationManager.h
和 HACLocationManager.m
复制到您的项目对于 iOS 7,建议您在应用的 Info.plist
文件中设置一个字符串键 NSLocationUsageDescription
,以提供如何使用位置服务的描述。
从 iOS 8 开始,需要将 NSLocationWhenInUseUsageDescription
键添加到您的 Info.plist
文件中。此键的值将用于显示在请求位置权限时向用户呈现 UIAlertView 的描述。有关更多信息,请参阅 Apple 文档。
基本操作是在您的 Info.plist
文件中添加单个条目。添加 NSLocationWhenInUseUsageDescription
键,并选择类型为 String
。您为此条目输入的值将在第一次尝试确定用户位置时作为文本显示在 UIAlertView 中。最后,它应该看起来像这样
当您想要独立于任何操作请求位置权限时,请请求权限。此请求必须始终在其他操作之前执行。我建议在 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
git checkout -b my-new-feature
)git commit -am '添加一些功能'
)git push origin my-new-feature
)