First, join the core location framework to obtain the geographical location of the device through the classes included in the Framework.
When using the location service, you must add corelocation. Framework
# Impart <corelocation/corelocation. h>
Note:
1. When using the map service, many other devices will consume power. Therefore, after obtaining the device location, you should 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];
Running proxy method
-(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];}
You can use the simulator to debug your own location to obtain the current location.
The output result is:
Location and map: locate and obtain the device location