Objective-C 緯度・経度・時間取得表示

iOSでGPSから位置情報を取得し表示します。

Info.plist

ユーザに承認を得るメッセージをInfo.plistで定義しておく必要があります。

2項目

<key>NSLocationAlwaysUsageDescription</key>
<string>位置情報に常に使われます。</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>情報を通知するために使用します。</string>

ViewController.m

#import "ViewController.h"

#import  <CoreLocation/CoreLocation.h>


@interface ViewController () <CLLocationManagerDelegate>


@property (nonatomic, retain) CLLocationManager *locationManager;

// 緯度

@property (weak, nonatomic) IBOutlet UILabel *labelLatitude;

// 経度

@property (weak, nonatomic) IBOutlet UILabel *labelLongitude;

// 時間

@property (weak, nonatomic) IBOutlet UILabel *labelTime;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    // ユーザから位置情報の利用について承認

    if (nil == self.locationManager) {

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

        // iOS 8以上

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

            // NSLocationWhenInUseUsageDescriptionに設定したメッセージでユーザに確認

            [ self.locationManager requestWhenInUseAuthorization];

            // NSLocationAlwaysUsageDescriptionに設定したメッセージでユーザに確認

            //[locationManager requestAlwaysAuthorization];

        }

    }

    

    

    if (nil == self.locationManager)

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

    self.locationManager.delegate = self;


    // 更新間隔はdistanceFilterプロパティ

    //self.locationManager.distanceFilter = 500;

    

    // 情報の更新を開始すれば、位置情報を取得

    [self.locationManager startUpdatingLocation];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


// GPSで位置情報の更新があったときに呼ばれる

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

    

    CLLocation* location = [locations lastObject];

    // 時間

    NSDate* timestamp = location.timestamp;

    

    NSLog(@"緯度 %+.6f, 経度 %+.6f\n", location.coordinate.latitude,

                                      location.coordinate.longitude);

    

    // 緯度 %+.6f

    self.labelLatitude.text = [NSString stringWithFormat:@"%+.6f",  

                                        location.coordinate.latitude];

    // 経度 %+.6f

    self.labelLongitude.text = [NSString stringWithFormat:@"%+.6f",

                                        location.coordinate.longitude];

    // 日時時間

    NSDateFormatter *df = [[NSDateFormatter alloc] init];

    [df setDateFormat:@"yyyy/MM/dd HH:mm:ss"];

    self.labelTime.text = [df stringFromDate:timestamp];

    

}


@end

GitHub GpsLocation

▫️参考ページ

GPSから位置情報を取得する

  

目 次

 

コメントをお書きください

コメント: 3
  • #1

    Annabell Friley (月曜日, 23 1月 2017 00:31)


    This is the right website for everyone who hopes to understand this topic. You realize so much its almost hard to argue with you (not that I actually would want to�HaHa). You certainly put a brand new spin on a topic which has been discussed for ages. Excellent stuff, just excellent!

  • #2

    Brendon Dameron (月曜日, 23 1月 2017 11:01)


    My partner and I absolutely love your blog and find most of your post's to be precisely what I'm looking for. Do you offer guest writers to write content for you personally? I wouldn't mind composing a post or elaborating on some of the subjects you write about here. Again, awesome web log!

  • #3

    Mitsue Gagne (水曜日, 01 2月 2017 03:02)


    This excellent website truly has all the info I wanted concerning this subject and didn't know who to ask.