ios-get latitude and longitude

Source: Internet
Author: User

Today to a small task, is to obtain latitude and longitude, originally thought very simple, but unexpectedly engaged for several hours, rub, The original XCODE5 after the original sdk.60 location method can not be used, I surf the internet for a lot of information, the results are pits, not what variables, is broken point can not enter the method, the best fire, carefully study the SDK documentation, and then combined with the online method, wrote this a simple way to obtain latitude and longitude, we look at the good.

 

First to import the Corelocation.frame package

. h file

? #import <uikit/uikit.h> #import <corelocation/corelocation.h>   @interface VIEWCONTROLLER:UIVIEWCONTROLLER<CLLOCATIONMANAGERDELEGATE>   @property ( nonatomic @property ( nonatomic //ISO 5.0 and 5.0 SDK versions enable   @end /tbody>
1 2 3 4 5 6 7 8 9

. m file

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {     [super viewDidLoad];         self.locationMgr = [[CLLocationManager alloc] init];    //设置代理     self.locationMgr.delegate = self;         // 设置定位精度     // kCLLocationAccuracyNearestTenMeters:精度10米     // kCLLocationAccuracyHundredMeters:精度100 米     // kCLLocationAccuracyKilometer:精度1000 米     // kCLLocationAccuracyThreeKilometers:精度3000米     // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度     // kCLLocationAccuracyBestForNavigation:导航情况下最高精度,一般要有外接电源时才能使用     self.locationMgr.desiredAccuracy = kCLLocationAccuracyBest;         // distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序     // 它的单位是米,这里设置为至少移动1000再通知委托处理更新;     self.locationMgr.distanceFilter = 1000.0f;         //开始定位     [self.locationMgr startUpdatingLocation];     }// iso 6.0以上SDK版本使用,包括6.0。 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {     CLLocation *cl = [locations objectAtIndex:0];     NSLog(@"纬度--%f",cl.coordinate.latitude);     NSLog(@"经度--%f",cl.coordinate.longitude);}//获取定位失败回调方法 #pragma mark - location Delegate - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {     NSLog(@"Location error!"); }@end

to test with a real machine, the output is as follows:

There is a better way to welcome everyone's advice.

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.