四、卫星定位《苹果iOS实例编程入门教程》
该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实例编程入门教程》的更多相关文章
- 六、雪花《苹果iOS实例编程入门教程》
该app为应用的功能为制作一场雪景 现版本 SDK 8.4 Xcode 纲要:- UIImageView 的运用- onTimer 代码运用- onAnimation 代码运用 运行Xcode 选择 ...
- 五、点数器《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...
- 三、图像移动《苹果iOS实例编程入门教程》
该app为应用的功能为动态移动动画 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applicati ...
- 二 、打开地图《苹果iOS实例编程入门教程》
该app为应用的功能为给你的iPhone打开google地图有效地址连接 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Si ...
- 一、午夜倒数《苹果iOS实例编程入门教程》
该app为应用的功能为计算离午夜12:00点的剩余时间 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...
- 七、考反映小游戏《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...
- 【C语言C++编程学习笔记】基础语法,第一个简单的实例编程入门教程!
C语言/C++编程学习:一个简单的实例 让我们来看一个简单的C语言程序.从下面的程序可以看出编写C语言程序的一些基本特征. 如果你能知道该程序将会在显示器上显示一些内容,那说明你还是知道一些的! ...
- PHP面向对象(OOP)编程入门教程
面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行PHP的高级编程,对于提高 PHP编程能力和规划好Web开发构架都是非常有意义的.下面我们就 ...
- VS2010/MFC编程入门教程之目录和总结
鸡啄米的这套VS2010/MFC编程入门教程到此就全部完成了,虽然有些内容还未涉及到,但帮助大家进行VS2010/MFC的入门学习业已足够.以此教程的知识为基础,学习VS2010/MFC较为深入的内容 ...
随机推荐
- PowerDesigner生成sql及HTML格式数据库文档
一.PowerDesigner生成sql问题 生成sql的方法是 Database -->Generate Database (Ctrl + G ) 但是提示 Could not load VB ...
- 跨平台C的IDE
1.JetBrains的新跨平台C++ IDE,CLion已经开始EAP了,不过这货是收费的 http://confluence.jetbrains.com/display/CLION/Early+A ...
- ORA-03113:通信通道的文件结尾解决
今天跟往常一样,登陆PL/SQL,确登陆失败,出现一个错误“ORA-01034”和“ORA-27101”如图: 然后就就通过命令提示符去登陆Oracle,去查看怎么回事,然后问题进一步出现,错误“OR ...
- [Linux] 解压tar.gz文件,解压部分文件
遇到数据库无法查找问题原因,只能找日志,查找日志的时候发现老的日志都被压缩了,只能尝试解压了 数据量比较大,只能在生产解压了,再进行查找 文件名为*.tar.gz,自己博客以前记录过解压方法: h ...
- 在IIS上部署SSL
背景: 在处理DropboxAPI开发时,其重定向的URL地址必须是https的[除了localhost],不得已在自己网站上加了ssl,下面简单介绍下添加自签名证书,毕竟只是临时使用. 1.打开II ...
- GitHub 使用教程图文详解(转)
大纲: 一.前言 二.GitHub简介 三.注册GitHub账号 四.配置GitHub 五.使用GitHub 六.参与GitHub中其它开源项目 七.总结 注,GitHub官网:https://git ...
- 内容分发网络CDN(互联网技术)
内容分发网络(互联网技术)CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更 ...
- 通过ajax访问Tomcat服务器web service接口时出现No 'Access-Control-Allow-Origin' header问题的解决办法
问题描述 通过ajax访问Web服务器(Tomcat7.0.42)中的json web service接口的时候,报以下跨域问题: XMLHttpRequest cannot load http:// ...
- 在ubuntu中安装maven
安装环境 操作系统:ubuntu 14.04.1 server amd64 安装jdk 在安装maven之前,必须确保已经安装过jdk. 安装jdk的方法请参考文章<在ubuntu中安装jdk& ...
- objective-c常用方法列表(总结)
第1章 Objective-C学习环境准备 1.1 Objective-C基础 1.1.1 Objective-C的发展历程 1.1.2 Objective-C语言的特点 1.1.3 技术架构 1.2 ...