First, add the core location framework. You can use the classes contained in the Framework to obtain the geographical location of the device.
When using location service, you need to add corelocation. Framework
# Impart <corelocation/corelocation. h>
Note:
1. When using the map service, more device power will be consumed. Therefore, after obtaining the location of the device, stop positioning to save light.
2. Set the location service as a global variable
. H file
#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong,nonatomic) CLLocationManager *locationManager;@end
In the. M file
// Initialize the location service self. locationmanager = [[cllocationmanager alloc] init]; // The cllocationmanager object is required to return all information [_ locationmanager setdistancefilter: kcldistancefilternone]; // you can specify the longitude parameter [_ locationmanager setdesiredaccuracy: // sets the proxy; _ locationmanager. delegate = self; // start real-time locating [_ locationmanager startupdatinglocation];
Proxy method of execution
-(Void) locationmanager :( cllocationmanager *) manager didupdatelocations :( nsarray *) locations {cllocation * newlocation = [locations objectatindex: 0]; // obtain new longitude and latitude metrics coordinate = newlocation. coordinate; nslog (@ "longitude: % F, latitude: % F", coordinate. longpolling, coordinate. latitude); [Manager stopupdatinglocation];}
Use the simulator to debug and customize the location to get the current location
The final output result is: