码迷,mamicode.com
首页 > 其他好文 > 详细

Pinpointing the location of a Device

时间:2014-06-03 07:15:49      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:c   class   code   a   tar   int   

Problem

You want to find the latitude and longitude of a device.

Solution

Use the CLLocationManager class:

 

 

 

#import "WSYViewController.h"

#import <MapKit/MapKit.h>

@interface WSYViewController ()<CLLocationManagerDelegate>

@property (nonatomic,strong)CLLocationManager * myLocationManager;

@end

 

@implementation WSYViewController

 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

    //we received the new loaction

    NSLog(@"Latitude = %f ",[[locations lastObject] coordinate].latitude);

    NSLog(@"longitude = %f ",[[locations lastObject] coordinate].longitude);

   

}

 

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    NSLog(@"Failed to receive user‘s location ");

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

   

    if ([CLLocationManager locationServicesEnabled]) {

        self.myLocationManager = [[CLLocationManager alloc]init];

        self.myLocationManager.delegate = self;

       

        [self.myLocationManager startUpdatingLocation];

    }else

    {

        //location services are not enabled

        //take appropriate action for instance prompt the user to enable the location services

        NSLog(@"Location services are not enabled");

       

    }

}

@end

 

 

In this code, myLocationManager is a property of type CLLocationManager. The current class is also the delegate of the location manager in this sample code.

Pinpointing the location of a Device,布布扣,bubuko.com

Pinpointing the location of a Device

标签:c   class   code   a   tar   int   

原文地址:http://www.cnblogs.com/ios-mark/p/3760728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!