该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实例编程入门教程》的更多相关文章

  1. 六、雪花《苹果iOS实例编程入门教程》

    该app为应用的功能为制作一场雪景 现版本 SDK 8.4 Xcode 纲要:- UIImageView 的运用- onTimer 代码运用- onAnimation 代码运用 运行Xcode 选择 ...

  2. 五、点数器《苹果iOS实例编程入门教程》

    该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...

  3. 三、图像移动《苹果iOS实例编程入门教程》

    该app为应用的功能为动态移动动画 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applicati ...

  4. 二 、打开地图《苹果iOS实例编程入门教程》

    该app为应用的功能为给你的iPhone打开google地图有效地址连接 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Si ...

  5. 一、午夜倒数《苹果iOS实例编程入门教程》

    该app为应用的功能为计算离午夜12:00点的剩余时间 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...

  6. 七、考反映小游戏《苹果iOS实例编程入门教程》

    该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...

  7. 【C语言C++编程学习笔记】基础语法,第一个简单的实例编程入门教程!

    C语言/C++编程学习:一个简单的实例 让我们来看一个简单的C语言程序.从下面的程序可以看出编写C语言程序的一些基本特征.   如果你能知道该程序将会在显示器上显示一些内容,那说明你还是知道一些的! ...

  8. PHP面向对象(OOP)编程入门教程

    面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行PHP的高级编程,对于提高 PHP编程能力和规划好Web开发构架都是非常有意义的.下面我们就 ...

  9. VS2010/MFC编程入门教程之目录和总结

    鸡啄米的这套VS2010/MFC编程入门教程到此就全部完成了,虽然有些内容还未涉及到,但帮助大家进行VS2010/MFC的入门学习业已足够.以此教程的知识为基础,学习VS2010/MFC较为深入的内容 ...

随机推荐

  1. 算法系列:Reservoir Sampling

    copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  2. mac os x10.10 安装thrift

    http://thrift.apache.org/docs/install/ 一:安装最新版(自动安装) 最简单的是用homebrew进行安装 安装homebrew 在终端输入ruby -e &quo ...

  3. Golang gopath

    golang 的gopath 至关重要,会影响到我们import package. golang 支持以相对路径的方式import,但是这种方式是不推荐的. 推荐的做法是在gopath中添加我们的项目 ...

  4. 利用Roslyn构建一个简单的C#交互脚本引擎

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 微软的下一代编译器技术Roslyn是一个里程碑的技术,可以给.NET平台带来无限想象空间.比 ...

  5. javascript 获取url参数值

    function getvl(name) { var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s| ...

  6. Process32First 返回FALSE的原因

    一般情况下是不会返回FALSE的,如果发生了,请检查: 1:系统为UNICODE的,一定要设置PROCESSENTRY32的dwSize为sizeof(PROCESSENTRY32)即可..

  7. AIDL

    在介绍跨程序进程间通信AIDL前,先看一下本程序activity与某个服务是怎么绑定在一起进行交互的. 需求:服务有两个方法.分别是播放音乐与停止播放音乐.该程序的活动要访问这两个方法,在activi ...

  8. OGNL表示式使用和值栈

    另外值得参考博客:http://blog.csdn.net/resigshy/article/details/7560573 OGNL是Object Graphic Navigation Langua ...

  9. 如何在Crystal Portlet中正确返回JSON数据给AJAX请求?

    当Crystal Portlet中需要采用Ajax请求,并让后台返回Json数据时,如何才能正确.方便的返回Json数据呢? 以下两种方法均可: 方法一:Ajax请求时,采用RenderURL,对应P ...

  10. Liferay 6.2 改造系列之十二:修改Portal设置页面表单内容

    将Portal设置页面中无用的内容删除: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of ...