Easy iOS location-targeting service

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.