iOS get current city, location, coordinates, latitude

Source: Internet
Author: User

Sometimes it is possible to use some of the features of the map, such as to get the current city, and then return the corresponding data according to the city, without the need for other operations, each time to write a few lines of code, so you do a small package, later may add new features to adapt to the various needs of the app (make it stronger)

GitHub Package Address: https://github.com/iOSSinger/SGLocation

Core code:

How to use:

    //One line of code gets the current city[Loc setgetcity:^ (NSString *City ) {NSLog (@"%@", city);        }]; //One line of code gets the current position[Loc setgetlocation:^ (NSString *Location ) {NSLog (@"%@", location);        }]; //A line of code gets the current coordinates[Loc setgetcoordinate:^(cllocationcoordinate2d coodinate) {NSLog (@"%f-%f", Coodinate.latitude,coodinate.longitude); }];

Implementation of the Code, in fact, the value is passed back with block, without the need to use the proxy

#import "SGLocation.h"@interfaceSglocation () <CLLocationManagerDelegate>@property (nonatomic,strong) Cllocationmanager*Manager;@end@implementationsglocationStaticSglocation *_instance;+ (Instancetype) Allocwithzone: (struct_nszone *) zone{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instance=[Super Allocwithzone:zone];    }); return_instance;}+(instancetype) sharelocation{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instance=[[Self alloc] init];    }); return_instance;}-(Cllocationmanager *) manager{if(_manager = =Nil) {_manager=[[Cllocationmanager alloc] init]; _manager.Delegate=Self ;    [_manager requestwheninuseauthorization]; }    return_manager;}- (void) Setgetcity: (void(^) (NSString *)) getcity{_getcity=getcity; [Self.manager startupdatinglocation];}- (void) Setgetcoordinate: (void(^) (cllocationcoordinate2d)) getcoordinate{_getcoordinate=getcoordinate; [Self.manager startupdatinglocation];}- (void) Setgetlocation: (void(^) (NSString *)) getlocation{_getlocation=getLocation; [Self.manager startupdatinglocation];}- (void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{cllocation*loc =[Locations Lastobject]; //Anti-geo-codingClgeocoder *geocoder =[[Clgeocoder alloc] init]; [Geocoder reversegeocodelocation:loc Completionhandler:^ (Nsarray *placemarks, Nserror *error) {        if(placemarks.count) {//get current CityClplacemark *mark =Placemarks.firstobject; Nsdictionary*dict =[Mark Addressdictionary]; NSString*city = [Dict objectforkey:@" State"]; //City            if(self.getcity) {self.getcity (city);            [Self.manager stopupdatinglocation]; }                        //coordinates            if(self.getcoordinate) {self.getcoordinate (mark.location.coordinate);            [Self.manager stopupdatinglocation]; }                       //Location Information            if(self.getlocation) {self.getlocation (mark.name);            [Self.manager stopupdatinglocation]; }        }    }];}@end

iOS get current city, location, coordinates, latitude

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.