Sometimes small needs, in fact, just need to know the location of the current iOS app, some just want to accurate to the city level, do not need any map.
The following simple implementations are available:
@interface Mainviewcontroller () <cllocationmanagerdelegate>
....
@end
@implementation Mainviewcontroller
-(void) initlocation {
// Initialize the Location Services management Object
self. Locationmanager = [[cllocationmanager alloc] init];
self. Locationmanager. Delegate = self;
self. Locationmanager. Desiredaccuracy = kcllocationaccuracythreekilometers;
self. Locationmanager. Distancefilter = 1000.0f;
}
/*
Where the accuracy of the following several levels, the more accurate, the slower the program callback! The current example is just to the city level, so use kcllocationaccuracythreekilometers
extern const cllocationaccuracy kcllocationaccuracybestfornavigation __osx_available_ Starting (__MAC_10_7,__IPHONE_4_0);
extern const cllocationaccuracy kcllocationaccuracybest;
extern const cllocationaccuracy kcllocationaccuracynearesttenmeters;
extern const cllocationaccuracy kcllocationaccuracyhundredmeters;
extern const cllocationaccuracy kcllocationaccuracykilometer;
extern const cllocationaccuracy kcllocationaccuracythreekilometers;
*/
-(void) Viewwillappear: (BOOL) animated{
// start positioning
if(issetaddress = = FALSE) {
dispatch_async(dispatch_get_main_queue(), ^{
[self initlocation];
[self. Locationmanager requestwheninuseauthorization];
[self. Locationmanager startupdatinglocation];
});
}
}
#pragma mark Core Location delegation method is used to update locations to get latitude and longitude, province, city, street
-(void) Locationmanager: (cllocationmanager *) Manager didupdatelocations:
(nsarray *) Locations
{
cllocation * currlocation = [locations lastobject];
NSLog(@ "latitude=%3.5f, longitude=%3.5f, altitude=%3.5f", currlocation. Coordinate. Latitude, currlocation. Coordinate. Longitude, currlocation. Altitude);
clgeocoder *geocoder = [[clgeocoder alloc] init];
[Geocoder reversegeocodelocation: currlocation
Completionhandler: ^ (nsarray *placemarks, nserror *error) {
if ([Placemarks count] > 0) {
clplacemark *placemark = placemarks[0];
nsdictionary *addressdictionary = placemark. Addressdictionary;
nsstring *address = [addressdictionary objectforkey:(nsstring *) Kabpersonaddressstreetkey];
Address = Address = = Nil ? @ "": Address;
nsstring *state = [addressdictionary objectforkey:(nsstring *) Kabpersonaddressstatekey ];
state = state = = Nil ? @ "": State;
nsstring *city = [addressdictionary objectforkey:(nsstring *) Kabpersonaddresscitykey ];
City = City = Nil ? @ "": City;
NSLog(@ "Place Description:%@ \n%@ \n%@", State, address,city);
self. UserAddress = [[nsstring alloc] initwithformat:@ "%@", city];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@ "setliveshowaddress start");
int ret = setliveshowaddress(city, address);
if(ret = = 0) {
issetaddress = TRUE;
[self. Locationmanager stopupdatinglocation];
}
NSLog(@ "setliveshowaddress end");
});
}
}];
}
-(void) Locationmanager: (cllocationmanager *) Manager didfailwitherror: (nserror *) error
{
NSLog(@ "error:%@", error);
}
@end
Easy iOS location-targeting service