HACLocationManager is written in Objective-C, very easy to use and effective class.
Requests are made using blocks. Its syntax is very comfortable and intuitive.
Use singleton design pattern and its compatibility is complete with iOS7 or higher.
This class is not for use as GPS, only for obtain user location in any moment and get geocoding and reverse geocoding.
##Requirements and Dependencies
- iOS >= 7.0
- ARC enabled
- CoreLocation Framework
##Installation
####CocoaPods:
pod 'HACLocationManager'
####Manual install:
- Copy
HACLocationManager.h
andHACLocationManager.m
to your project - Manual install HACLocationManager
##Usage
For iOS 7, it is recommended that you provide a description for how your app uses location services by setting a string for the key NSLocationUsageDescription
in your app's Info.plist
file.
Since iOS 8 it is required to add NSLocationWhenInUseUsageDescription
key to your Info.plist
file. Value for this key will be a description of UIAlertView presented to user while asking for location permission. See Apple documentation for more info.
Basically all you need to do is to add single entry in your Info.plist
file. Add key NSLocationWhenInUseUsageDescription
, and select type String
. The value you enter for this entry will be shown as text in UIAlertView presented to user first time you try to determine his location.
In the end it should look similar to this:
To request permissions location, when you want independently to any operation. This request must always be performed before applying any other. I recommend do it in your AppDelegate.
[[HACLocationManager sharedInstance]requestAuthorizationLocation];
You can configure time out for updates in request, for default it's 5 seconds
HACLocationManager *locationManager = [HACLocationManager sharedInstance];
locationManager.timeoutUpdating = 6;
###Obtain user location Is obtained by locating blocks, based on the location and updates the last location obtained. The first is optional, only if your application requires it.
[locationManager LocationQuery];
locationManager.locationUpdatedBlock = ^(CLLocation *location){
NSLog(@"%@", location);
};
locationManager.locationEndBlock = ^(CLLocation *location){
NSLog(@"%@", location);
};
locationManager.locationErrorBlock = ^(NSError *error){
NSLog(@"%@", error);
};
###Geocoding
[locationManager GeocodingQuery];
Can multiple placemarks are received, so an array is returned
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"];
Can multiple placemarks are received, so an array is returned
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);
};
You can also get the latest location of the user stored persistently for those cases in which the location is not available.
NSLog(@"%@",locationManager.getLastSavedLocation);
You can try changing the parameters in the sample project, changing the parameters of latitude and longitude. The means of transport can also be chosen, walking or automovile, for default it's automovile.
[[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]);
}
}];
Enjoy :D
- Fork it ( https://github.com/[my-github-username]/HACLocationManager/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request