Location and map: locate and obtain the device location

Source: Internet
Author: User

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

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.