Location and map: locate and obtain the device location

Source: Internet
Author: User

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:



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.