首页 > 其他 > 详细

Pinpointing the location of a Device

时间:2014-06-03 07:13:41      阅读:352      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!