H:/0914/01_发短信,打电话,发邮件,取MIMETYPE_ViewController.h
//
// ViewController.h
// 01-小功能
//
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (IBAction)click;
@end
H:/0914/01_发短信,打电话,发邮件,取MIMETYPE_ViewController.m
// ViewController.m
// 01-发短信,打电话,发邮件,取MIMETYPE
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
// 发短信框架
#import <MessageUI/MessageUI.h>
@interface ViewController () <MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>
{
// WebView加载资源,打电话时用到,推荐使用这种方式拨号
UIWebView *_webView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - 打电话
- (void)call1
{
// 打电话方式1:使用app打开URL,缺点:无法回到原来的应用,停在通讯录了
NSURL *url = [NSURL URLWithString:@"tel://10010"];
[[UIApplication sharedApplication] openURL:url];
}
- (void)call2
{
// 使用telprompt打电话,会弹出一个提示框(且能自动回到原来应用)
// 私有API,尽量不要用
NSURL *url = [NSURL URLWithString:@"telprompt://10010"];
[[UIApplication sharedApplication] openURL:url];
}
- (void)call3
{
// 当使用WebView打电话时,不要添加WebView到界面上,否则挂断电话时无法回到原来的界面
_webView = [[UIWebView alloc] init];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL
URLWithString:@"tel://10010"]]];
}
#pragma mark - 发短信
- (void)msg1
{
// 使用app直接openURL方式发短信的缺点:1,不能指定内容,2,无法回到app界面
NSURL *url = [NSURL URLWithString:@"sms://10010"];
[[UIApplication sharedApplication] openURL:url];
}
- (void)msg2
{
// 要添加发短信框架MessageUI.h,实例化短信制作控制器
MFMessageComposeViewController *vc = [[MFMessageComposeViewController
alloc] init];
// 设置短信内容
vc.body = @"哈哈哈哈";
// 设置收件人,群发
vc.recipients = @[@"10010", @"10086"];
// 设置代理对象为当前控制器,遵守代理协议<MFMessageComposeViewControllerDelegate>
vc.messageComposeDelegate = self;
// presentViewController展现短信编辑控制器
[self presentViewController:vc animated:YES completion:nil];
}
/*
MessageComposeResult枚举:
1,取消了短信,MessageComposeResultCancelled,
2,发送了短信,MessageComposeResultSent,
3,发送失败了,MessageComposeResultFailed
*/
#pragma mark 代理方法,当短信操作结束的时候调用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
// dismissViewControllerAnimated关闭短信编辑控制器
[controller dismissViewControllerAnimated:YES completion:nil];
NSLog(@"操作完毕:%d", result);
}
#pragma mark - 直接app的打开url,发邮件,缺点:无法回到原界面,且不能指定body
- (void)mail1
{
NSURL *url = [NSURL URLWithString:@"mailto://10010@qq.com"];
[[UIApplication sharedApplication] openURL:url];
}
- (void)mail2
{
// 1,要添加第3方框架MessageUI.h,实例化mail制作控制器
MFMailComposeViewController *vc = [[MFMailComposeViewController
alloc] init];
// 设置主题
[vc setSubject:@"关于成立诗社"];
// 设置邮件内容,是HTML文本
[vc setMessageBody:@"<font color=\"red\">详情请点击</font>" isHTML:YES];
// 设置收件人
[vc setToRecipients:@[@"宝玉@qq.com"]];
// 抄送Cc
[vc setCcRecipients:@[@"黛玉@qq.com", @"宝钗@qq.com", @"湘云@qq.com"]];
// 密送Bc
[vc setBccRecipients:@[@"妙玉@qq.com"]];
// html text/html
// png image/png
// jpg image/jpeg
// mp4 video/mp4
NSURL *url = [[NSBundle mainBundle] URLForResource:@"test"
withExtension:@"png"];
NSData *data = [NSData dataWithContentsOfURL:url];
// 添加附件
[vc addAttachmentData:data mimeType:@"image/png" fileName:@"test.png"];
NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"test2"
withExtension:@"png"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
// 添加附件
[vc addAttachmentData:data2 mimeType:@"image/png" fileName:@"test2.png"];
// 设置代理对象为当前控制器,遵守代理协议<MFMailComposeViewControllerDelegate>
vc.mailComposeDelegate = self;
// presentViewController展现mail控制器
[self presentViewController:vc animated:YES completion:nil];
}
// 代理方法,当mail结束的时候调用,参数是MFMailComposeResult枚举
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// 关闭邮件界面
[controller dismissViewControllerAnimated:YES completion:nil];
NSLog(@"邮件操作完毕:%d", result);
}
#pragma mark - 请求,返回的response中获取文件MIME-TYPE
- (void)mimeType
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"CocoaChina"
withExtension:@"zip"];
//NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/未命名.html"];
NSURLRequest *requets = [NSURLRequest requestWithURL:url];
// 请求完毕后,返回的所有数据都封装在repoonse
NSURLResponse *repoonse = nil;
[NSURLConnection sendSynchronousRequest:requets
returningResponse:&repoonse error:nil];
NSString *mimeType = [repoonse MIMEType];
NSLog(@"%@", mimeType);
}
- (IBAction)click {
// NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];
// 调用safari
// [[UIApplication sharedApplication] openURL:url];
[self mimeType];
}
@end
H:/0914/02_通讯录_AppDelegate.m
//
// AppDelegate.m
// 02-通讯录访问
//
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import "AppDelegate.h"
// 访问通讯录,需导入第3方框架
#import <AddressBook/AddressBook.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 首次运行的时候,调用自定义方法进行授权
[self addressBookAccess];
return YES;
}
- (void)addressBookAccess
{
// 获取授权状态
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 判断授权状态
switch (status) {
case kABAuthorizationStatusNotDetermined: // 未授权
{ // 一个ABAddressBookRef实例,代表整个通讯录
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 未授权
ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"授权成功");
} else {
NSLog(@"授权失败");
}
});
break;
}
case kABAuthorizationStatusAuthorized: // 已授权
{
NSLog(@"已授权");
break;
}
default:
NSLog(@"拒绝访问");
break;
}
}
@end
H:/0914/02_通讯录_ViewController.m
// ViewController.m
// 02-通讯录访问
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
// 访问通讯录,需导入第3方框架
#import <AddressBook/AddressBook.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 必须先获得状态,只有授权之后,才可进行其他操作
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 获得所有的联系人数据
if (status == kABAuthorizationStatusAuthorized) {
[self getPersonImage];
}
}
// 自定义方法,获得所有联系人
#pragma mark - 查找所有的联系人 (简单属性,1对1)
- (void)findAllPersons
{
// CF的内存管理原则:只要函数中包含了Create、Copy,就要release相应的实例
// 1.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获得通讯录ABAddressBookRef实例中的所有联系人数组
CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book);
// 3.遍历所有的联系人数据
int count = CFArrayGetCount(persons);
for (int i = 0; i<count; i++) {
// 获取一条通讯录记录(一个联系人 或 一个组)
ABRecordRef person = CFArrayGetValueAtIndex(persons, i);
// 外国人:名姓
// 获得用户的firstname (名)
CFStringRef firstaname = ABRecordCopyValue(person, kABPersonFirstNameProperty);
// 获得用户的lasttname (姓)
CFStringRef lastname = ABRecordCopyValue(person, kABPersonLastNameProperty);
// 打印姓名,即lasttaname+firstaname
NSLog(@"%@ %@", lastname, firstaname);
// 释放资源,CF的内存原则:函数名Create、Copy,就要release
CFRelease(firstaname);
CFRelease(lastname);
}
// 释放资源,CF的内存原则:函数名Create、Copy,就要release
CFRelease(book);
CFRelease(persons);
}
// __bridge可以C(CoreFoundation)对象和OC即NS(Foundation)对象互转
#pragma mark - 查找所有的联系人 (简单属性,1对1)
- (void)findAllPersons2
{
// 1.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获得通讯录ABAddressBookRef实例中的所有联系人数组
NSArray *persons = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(book);
// 3.遍历所有的联系人数据
int count = persons.count;
for (int i = 0; i<count; i++) {
// 获取一条通讯录记录(一个联系人\一个组)
ABRecordRef person = (__bridge ABRecordRef)persons[i];
// 获得用户的firstname (名)
NSString *firstaname = (__bridge NSString *)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
// 获得用户的lasttname (姓)
NSString *lastname = (__bridge NSString *)ABRecordCopyValue(person,
kABPersonLastNameProperty);
NSLog(@"%@ %@", lastname, firstaname);
}
// 释放资源 CF中的create和copy出来的对象都要手动释放
CFRelease(book);
}
#pragma mark 复杂属性(,1 VS N)
- (void)findAllPersons3
{
// 1.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获得通讯录ABAddressBookRef实例中的所有联系人数组
CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book);
// 3.遍历所有的联系人数据
int count = CFArrayGetCount(persons);
for (int i = 0; i<count; i++) {
// 获取一条通讯录记录(一个联系人\一个组)
ABRecordRef person = CFArrayGetValueAtIndex(persons, i);
//---------------------获得所有的地址---------------------------
ABMultiValueRef addresses = ABRecordCopyValue(person,
kABPersonAddressProperty);
// 获得第0个地址,是个字典
CFDictionaryRef address0 = ABMultiValueCopyValueAtIndex(addresses, 0);
// 获得第0个地址的国家,从字典中根据Key
CFStringRef conutry = CFDictionaryGetValue(address0,
kABPersonAddressCountryKey);
NSLog(@"%@", conutry);
//-----------------------获得邮件-------------------------
ABMultiValueRef email = ABRecordCopyValue(person,
kABPersonEmailProperty);
// 获得邮箱类型
CFStringRef label0 = ABMultiValueCopyLabelAtIndex(email, 0);
CFStringRef label1 = ABMultiValueCopyLabelAtIndex(email, 1);
// 获得本地化标签
CFStringRef newLable0 = ABAddressBookCopyLocalizedLabel(label0);
CFStringRef newLable1 = ABAddressBookCopyLocalizedLabel(label1);
// 获得邮箱地址值
CFStringRef value0 = ABMultiValueCopyValueAtIndex(email, 0);
CFStringRef value1 = ABMultiValueCopyValueAtIndex(email, 1);
NSLog(@"%@ - %@", newLable0, value0);
NSLog(@"%@ - %@", newLable1, value1);
// 一大堆CF对象,等着要CFRelease,但是,不能CFRelease(NULL);
}
// 释放资源
CFRelease(book);
CFRelease(persons);
}
#pragma mark - 添加联系人,4步曲
- (void)addPerson
{
// 1.创建一个新的联系人,create copy 对应release
ABRecordRef person = ABPersonCreate();
// 2.设置联系人的属性
ABRecordSetValue(person, kABPersonLastNameProperty, @"林", NULL);
ABRecordSetValue(person, kABPersonFirstNameProperty, @"黛玉", NULL);
// 3.添加联系人到通讯录中,先取得实例ABAddressBookRef
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookAddRecord(book, person, NULL);
// 4.保存修改
if(ABAddressBookHasUnsavedChanges(book)) {
// 如果有尚未保存的修改,就保存
ABAddressBookSave(book, NULL);
}
// 5.释放资源
CFRelease(person);
CFRelease(book);
}
#pragma mark - 修改联系人(全部人的姓都变为“刘”)
- (void)updatePersons
{
// 1.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获得通讯录ABAddressBookRef实例中的所有联系人数组
CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book);
// 3.遍历所有的联系人数据
int count = CFArrayGetCount(persons);
for (int i = 0; i<count; i++) {
// 获取一条通讯录记录(一个联系人\一个组)
ABRecordRef person = CFArrayGetValueAtIndex(persons, i);
// 修改记录的姓~
ABRecordSetValue(person, kABPersonLastNameProperty, @"刘", NULL);
}
// 保存修改
ABAddressBookSave(book, NULL);
// 释放资源
CFRelease(book);
CFRelease(persons);
}
#pragma mark - 查询所有的组
- (void)findGroups
{
// 1.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获取组
CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(book);
int count = CFArrayGetCount(groups);
for (int i = 0; i<count; i++) {
// 组也是一条记录
ABRecordRef group = CFArrayGetValueAtIndex(groups, i);
// 取得组名
CFStringRef name = ABRecordCopyValue(group, kABGroupNameProperty);
NSLog(@"%@", name);
// CF中create copy出来的都要release
CFRelease(name);
}
// CF中create copy出来的都要release
CFRelease(groups);
CFRelease(book);
}
#pragma mark - 添加组
- (void)addGroup
{
// 1.创建一个新组,一条记录
ABRecordRef group = ABGroupCreate();
// 设置组名
ABRecordSetValue(group, kABGroupNameProperty, @"大学同学", NULL);
// 2.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 同添加一条记录一样
ABAddressBookAddRecord(book, group, NULL);
// 3.保存
ABAddressBookSave(book, NULL);
CFRelease(group);
CFRelease(book);
}
#pragma mark - 添加含有联系人的组,1,加组保存,2,加人保存,3,组添加成员,保存
- (void)addGroup2
{
// 1.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
/*
2.添加一个新的组(先保存一遍)
*/
ABRecordRef group = ABGroupCreate();
// 设置组名
ABRecordSetValue(group, kABGroupNameProperty, @"小学同学5", NULL);
ABAddressBookAddRecord(book, group, NULL);
ABAddressBookSave(book, NULL);
/*
3.添加一个新的人(再保存一遍)
*/
ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person, kABPersonLastNameProperty, @"李5", NULL);
ABRecordSetValue(person, kABPersonFirstNameProperty, @"lili5", NULL);
ABAddressBookAddRecord(book, person, NULL);
ABAddressBookSave(book, NULL);
// 4.最后,关键一步,添加联系人到指定的组中,(最后还要保存一遍)
ABGroupAddMember(group, person, NULL);
ABAddressBookSave(book, NULL);
// CF中create copy出来的对象都要CFRelease
CFRelease(person);
CFRelease(group);
CFRelease(book);
}
// 给所有的person设置头像imageData
- (void)setPersonImage
{
// 1.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获得通讯录ABAddressBookRef实例中的所有联系人数组
CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book);
// 3.遍历所有的联系人数据
int count = CFArrayGetCount(persons);
// 加载头像数据
UIImage *image = [UIImage imageNamed:@"test2.png"];
// 压缩image为Data
NSData *data = UIImagePNGRepresentation(image);
for (int i = 0; i<count; i++) {
// 获取一条通讯录记录(一个联系人\一个组)
ABRecordRef person = CFArrayGetValueAtIndex(persons, i);
// 设置头像
ABPersonSetImageData(person, (__bridge CFDataRef)data, NULL);
}
ABAddressBookSave(book, NULL);
// 释放资源
CFRelease(book);
CFRelease(persons);
}
// 获取第1个人的头像ImageData
- (void)getPersonImage
{
// 1.获得通讯录ABAddressBookRef实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获得通讯录ABAddressBookRef实例中的所有联系人数组
CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book);
ABRecordRef person = CFArrayGetValueAtIndex(persons, 0);
// 如果第1个人他有头像,取出头像imageData
if (ABPersonHasImageData(person)) {
NSData *data = (__bridge NSData *)(ABPersonCopyImageData(person));
_imageView.image = [UIImage imageWithData:data];
// [data writeToFile:@"/Users/apple/Desktop/abc.png" atomically:YES];
}
// 释放资源
CFRelease(book);
CFRelease(persons);
}
@end
H:/0914/03_地图_MyAnnotation.h
//
// MyAnnotation.h
// 03-定位和地图
//
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
// 自定义大头针,ID类型,但必须遵守协议<MKAnnotation>
#import <Foundation/Foundation.h>
// 自定义大头针,需要导入第3方框架
#import <MapKit/MapKit.h>
// 遵守协议
@interface MyAnnotation : NSObject <MKAnnotation>
// 协议里面规定的成员,2D坐标,结构体,包含经度和纬度
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
// 协议里面,下面这三个成员是可选的
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *icon;
@end
H:/0914/03_地图_MyAnnotation.m
//
// MyAnnotation.m
// 03-定位和地图
//
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import "MyAnnotation.h"
@implementation MyAnnotation
@end
H:/0914/03_地图_ViewController.h
//
// ViewController.h
// 03-定位和地图
//
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import <UIKit/UIKit.h>
// 地图,需要导入第3方框架
#import <MapKit/MapKit.h>
// 遵守代理协议
@interface ViewController : UIViewController <MKMapViewDelegate>
// 成员MKMapView
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
H:/0914/03_地图_ViewController.m
// ViewController.m
// 03-定位和地图
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
// 地图,需要导入第3方框架
#import <CoreLocation/CoreLocation.h>
#import "MyAnnotation.h"
@interface ViewController ()
@end
@implementation ViewController
// mapKit的基本用法
- (void)mapKitBaseUse
{
/*1.设置地图的显示模式
可以通过设置MKMapView的mapViewType设置地图类型
MKMapTypeStandard 普通地图
MKMapTypeSatellite 卫星云图
MKMapTypeHybrid 普通地图覆盖于卫星云图之上*/
_mapView.mapType = MKMapTypeHybrid;
// 2.自动追踪(第一次使用定位功能需要授权,
// 可在Info.plst中添加Privacy - Location Usage Description,说明定位的目的)
[_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
// 3.设置代理为当前控制器,遵守协议<MKMapViewDelegate>
_mapView.delegate = self;
/*
MapView会将一些事件传递给它的代理(遵守MKMapViewDelegate协议),
代理方法如下:
WillStartLoadingMap: 当地图界面将要加载时调用
viewForAnnotation: 当地图上有一些动画效果展示\加载时调用
WillStartLocatingUser:当准备进行一个位置定位时调用
regionDidChangeAnimated: 当显示的区域发生变化时调用
didUpdateUserLocation:当用户位置发生变化时调用*/
}
#pragma mark mapView代理方法
// 用户位置修改了就会调用,调用频率相当高
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation
{
// 1.取出userLocation里面的位置CLLocation
CLLocation *location = userLocation.location;
// 2.取出CLLocation里面的经纬度CLLocationCoordinate2D
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"用户位置修改了---%@", userLocation.location);
}
// 40.05996089, 116.34331900
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.设置代理(必须最先设置代理,再添加大头针)
_mapView.delegate = self;
// 1.添加一个自定义的大头针
MyAnnotation *anno = [[MyAnnotation alloc] init];
// 大头针要定在哪个地方
anno.coordinate = CLLocationCoordinate2DMake(40.05996089, 116.34331900);
anno.title = @"天朝";
anno.subtitle = @"好地方!!!";
anno.icon = @"head0.png";
[_mapView addAnnotation:anno];
// 2.添加多个自定义大头针
MyAnnotation *anno1 = [[MyAnnotation alloc] init];
anno1.icon = @"head0.png";
anno1.coordinate = CLLocationCoordinate2DMake(30.05996089, 80.34331900);
MyAnnotation *anno2 = [[MyAnnotation alloc] init];
anno2.icon = @"head1.png";
anno2.coordinate = CLLocationCoordinate2DMake(20.05996089, 100.34331900);
MyAnnotation *anno3 = [[MyAnnotation alloc] init];
anno3.icon = @"head2.png";
anno3.coordinate = CLLocationCoordinate2DMake(50.05996089, 95.34331900);
[_mapView addAnnotations:@[anno1, anno2, anno3]];
}
#pragma mark 代理方法,自定义大头针view,重要~~~
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(MyAnnotation *)annotation
{
// 同tableView的cell重用
static NSString *ID = @"anno";
MKAnnotationView *view = [mapView
dequeueReusableAnnotationViewWithIdentifier:ID];
if (view == nil) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:ID];
// MKPinAnnotationView 子类,可修改大头针的颜色
//((MKPinAnnotationView *)view).pinColor = MKPinAnnotationColorGreen;
}
// 设置MKAnnotationView的独一无二显示数据
view.annotation = annotation;
view.image = [UIImage imageNamed:annotation.icon];
return view;
}
@end
H:/0914/04_定位_MyAnnotation.h
//
// MyAnnotation.h
// 04-定位和地图
//
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
// 自定义大头针,ID类型,但必须遵守协议<MKAnnotation>
#import <Foundation/Foundation.h>
// 自定义大头针,需要导入第3方框架
#import <MapKit/MapKit.h>
// 遵守协议
@interface MyAnnotation : NSObject <MKAnnotation>
// 协议里面规定的成员,2D坐标,结构体,包含经度和纬度
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
// 协议里面,下面这三个成员是可选的
// Title and subtitle for use by selection UI.
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
H:/0914/04_定位_MyAnnotation.m
//
// MyAnnotation.m
// 04-定位和地图
//
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import "MyAnnotation.h"
@implementation MyAnnotation
@end
H:/0914/04_定位_MyLocation.gpx
<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode">
<wpt lat="67.331705" lon="80.030237">
<name>Cupertino</name>
</wpt>
</gpx>
H:/0914/04_定位_ViewController.h
//
// ViewController.h
// 04-定位和地图
//
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController
// 成员mapView
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
H:/0914/04_定位_ViewController.m
// ViewController.m
// 04-定位和地图
// Created by apple on 13-9-14.
// Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
// 需导入第3方框架
#import <CoreLocation/CoreLocation.h>
#import "MyAnnotation.h"
@interface ViewController () <CLLocationManagerDelegate>
{
// 定位管理器
CLLocationManager *_mgr;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 实例化 CLGeocoder,目的是根据地名,反向解析坐标
CLGeocoder *geo = [[CLGeocoder alloc] init];
// 全球搜索“广州”
[geo geocodeAddressString:@"西三旗"
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"%d", placemarks.count);
// 获得第1个标记
CLPlacemark *placemark = placemarks[0];
// 添加1个大头针
MyAnnotation *anno = [[MyAnnotation alloc] init];
// 为大头针设置坐标位置
anno.coordinate = placemark.location.coordinate;
// mapView中添加大头针
[_mapView addAnnotation:anno];
// 设置地图中心点,是刚才解析出来的坐标
[_mapView setCenterCoordinate:anno.coordinate animated:YES];
}];
}
// CLLocationManager的基本用法
- (void)baseUse
{
// 实例化位置管理器
_mgr = [[CLLocationManager alloc] init];
// 设置位置管理器的代理对象为当前控制器
_mgr.delegate = self;
// 设置期望的精度
_mgr.desiredAccuracy = kCLLocationAccuracyBest;
// 监听用户进入或者离开某个区域
// [_mgr startMonitoringForRegion:<#(CLRegion *)#>];
// startUpdatingLocation开始跟踪用户的位置
[_mgr startUpdatingLocation];
}
#pragma mark location代理方法,用户的位置更新就会调用
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
// locations数组一般只有一个成员就是当前的位置
// 获得当前的位置,包含经度和纬度和调用的时间
CLLocation *loc = locations[0];
NSLog(@"%@", loc);
}
@end