码迷,mamicode.com
首页 > 移动开发 > 详细

四、卫星定位《苹果iOS实例编程入门教程》

时间:2015-08-02 00:53:48      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

该app为应用的功能为用iPhone 显示你现在的位置

现版本 SDK 8.4 Xcode

运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 WhereAmI

(1) 点击文件夹WhereAmI -> General->Linked Frameworks and Libraries -> "+"->  搜索 CoreLocation.framework ->add

技术分享

 

 

(2)  打开 ViewController.h 文件,加入下面代码

 

#import <UIKit/UIKit.h>

 

#import <CoreLocation/CoreLocation.h>

 

#import <CoreLocation/CLLocationManagerDelegate.h>

 

 

@interface ViewController : UIViewController <CLLocationManagerDelegate>{

    

    IBOutlet UITextField *altitude;

    

    IBOutlet UITextField *latitude;

    

    IBOutlet UITextField *longitude;

    

    CLLocationManager *locmanager;

    

    BOOL wasFound;

}

 

-(IBAction)update:(id)sender;

 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *) oldLocation ;

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

 

@end

(3)  打开 ViewController.m 文件,加入下面代码

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

-(IBAction)update:(id)sender{

    

    locmanager = [[CLLocationManager alloc]init];

    

    [locmanager setDelegate:self];

    

    [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];

    

    

    locmanager.distanceFilter=10;

    

    NSString *iOSVersion=[UIDevice currentDevice].systemVersion;

    

    //NSLog(@"%@",iOSVersion);

    

    if ((int)iOSVersion >= 8) {

        [locmanager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)

    }

    

    [locmanager startUpdatingLocation];

    

}

 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    

    if(wasFound)return;

    

    wasFound = YES;

    

    CLLocationCoordinate2D loc = [newLocation coordinate];

    

    latitude.text = [NSString stringWithFormat:@"%f",loc.latitude];

    

    longitude.text = [NSString stringWithFormat:@"%f",loc.longitude];

    

    altitude.text = [NSString stringWithFormat:@"%f",newLocation.altitude];

}

 

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

    

    

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

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

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

(3) 设置info.plist

点击info.plist,在右侧添加NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription 

将 Value设置为YES

技术分享

 

 

(4) UIView 界面设置

 点击Main.storyboard

 加入三个Label 在 Attributes 下, Text 内填上"经度",“纬度”,“海拔”;

技术分享

 

 

加入 三个Text Field用于显示 "经度",“纬度”,“海拔”;

鼠标右击Text Field控件 移动鼠标在"Referencing Outlets" 后面圆圈上; 圆圈变为(+); 拖动直线连接到"view controller";
放开鼠标选择键出现 "longitude","latitude","altitude"; 对应着"经度",“纬度”,“海拔”三个Text Field ,分别选上它。

 

选择: File -> Save


最后在 xCode 选择 Build and then Running

(5)真机调试效果图

技术分享

 

本文源于网上博客教程,经过本人修改和测试。原blog地址 http://blog.sina.com.cn/s/blog_5fae23350100e5fi.html

四、卫星定位《苹果iOS实例编程入门教程》

标签:

原文地址:http://www.cnblogs.com/huaixu/p/4694902.html

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