您的位置:首页 > 产品设计 > UI/UE

UI:地图和定位

2015-10-03 23:05 399 查看
参考学习链接

各种IOS设备可以使用 Core Location 框架确定它的物理位置。core location 主要使用三种技术来实现功能。GPS、蜂窝基站三角网络定位、 wifi 定位服务。这三种技术都会非常的消耗电能,所以在使用Core Location 的时候,要注意,除非必要,要尽量少对你的位置进行轮询。使用 core location 的时候,通过指定所需的最低精度级别,可以减少不必要的电能损耗。

获取定位

if ([CLLocationManagerlocationServicesEnabled]) {
[CLLocationManager authorizationStatus];

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

[self.locationManager startUpdatingLocation];

[self.locationManager requestWhenInUseAuthorization];
}

// 1.设置地图类型

self.mapView.mapType = MKMapTypeStandard;

// 2.设置跟踪模式(MKUserTrackingModeFollow == 跟踪)

self.mapView.userTrackingMode = MKUserTrackingModeFollow;

// 3.设置代理(监控地图的相关行为:比如显示的区域发生了改变)
self.mapView.delegate = self;

// 设置经纬度

CLLocationCoordinate2D ddd = CLLocationCoordinate2DMake(23, 116);
//
// [self.mapView setCenterCoordinate:ddd];

// 设置跨度
[self.mapView setRegion:MKCoordinateRegionMake(ddd, MKCoordinateSpanMake(1, 1)) animated:YES];

归位
- (IBAction)guiweiAction:(id)sender {

[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}

代理方法 显示系统自带的大头针
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

userLocation.title = @"天朝帝都";

userLocation.subtitle = @"帝都是个牛逼的地方";
}

自定义大头针类
#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface HMAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end

// 创建两个大头针

HMAnnotation *anno1 = [[HMAnnotation alloc] init];

anno1.coordinate = CLLocationCoordinate2DMake(39, 119);

anno1.title = @"帝都";

anno1.subtitle = @"帝都帝都帝都帝都帝都";

// 添加一个大头针模型(模型:描述大头针的信息)

[self.mapView addAnnotation:anno1];

HMAnnotation *anno2 = [[HMAnnotation alloc] init];

anno2.coordinate = CLLocationCoordinate2DMake(23, 116);

anno2.title = @"广东";

anno2.subtitle = @"广东广东广东广东广东";
[self.mapView addAnnotation:anno2];

创建100个大头针
for (int i = 0; i<100; i++) {

CLLocationDegrees latitude = 23 + arc4random_uniform(20);

CLLocationDegrees longitude = 73.2 + arc4random_uniform(50);

HMAnnotation *anno = [[HMAnnotation alloc] init];

anno.coordinate = CLLocationCoordinate2DMake(latitude, longitude);

[self.mapView addAnnotation:anno];
}

大图片的大头针
// 带图片的大头针

HMAnnotation *tg1 = [[HMAnnotation alloc] init];

tg1.title = @"xxx大饭店";

tg1.subtitle = @"全场一律15折,会员20折";

tg1.icon = @"category_1";

tg1.coordinate = CLLocationCoordinate2DMake(37, 116);

[self.mapView addAnnotation:tg1];

HMAnnotation *tg2 = [[HMAnnotation alloc] init];

tg2.title = @"xxx影院";

tg2.subtitle = @"最新大片:美国队长2,即将上映。。。";

tg2.icon = @"category_5";

tg2.coordinate = CLLocationCoordinate2DMake(29, 110);
[self.mapView addAnnotation:tg2];

设置带图片的大头针
//- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(HMAnnotation *)annotation
//{
// // 1.先从缓存池中取出可以循环利用的大头针控件
// static NSString *ID = @"anno";
// MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
//
// // 2.缓存池中没有可以循环利用的大头针控件
// if (annotationView == nil) {
// // 传入循环利用标识来创建大头针控件
// annotationView = [[MKAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ID];
// // 显示标题和子标题
// annotationView.canShowCallout = YES;
// }
//
// // 3.传递模型(更新大头针数据,覆盖掉之前的旧数据)
// annotationView.annotation = annotation;
//
// // 4.设置图片
// annotationView.image = [UIImage imageNamed:annotation.icon];
//
// return annotationView;
//}

导航

@property (nonatomic, strong) CLGeocoder *geocoder;

- (IBAction)startNavigation;

@property (nonatomic, strong) MKPlacemark *sourceMKPm;
@property (nonatomic, strong) MKPlacemark *destinationMKPm;

先划线
- (void)viewDidLoad

{

[super viewDidLoad];

self.mapView.delegate = self;

NSString *sourceAddress = @"广州";

NSString *destinationAddress = @"北京";

[self.geocoder geocodeAddressString:sourceAddress completionHandler:^(NSArray *placemarks, NSError *error) {

CLPlacemark *gzPm = [placemarks firstObject];

if (gzPm == nil) return;

// 添加广州大头针

HMAnnotation *gzAnno = [[HMAnnotation alloc] init];

gzAnno.coordinate = gzPm.location.coordinate;

gzAnno.title = sourceAddress;

gzAnno.subtitle = gzPm.name;

[self.mapView addAnnotation:gzAnno];

[self.geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) {

CLPlacemark *bjPm = [placemarks firstObject];

if (bjPm == nil) return;

// 添加北京大头针

HMAnnotation *bjAnno = [[HMAnnotation alloc] init];

bjAnno.coordinate = bjPm.location.coordinate;

bjAnno.title = destinationAddress;

bjAnno.subtitle = bjPm.name;

[self.mapView addAnnotation:bjAnno];

[self drawLineWithSourceCLPm:gzPm destinationCLPm:bjPm];

}];

}];
}

- (void)drawLineWithSourceCLPm:(CLPlacemark *)sourceCLPm destinationCLPm:(CLPlacemark *)destinationCLPm

{

if (sourceCLPm == nil || destinationCLPm == nil) return;

// 1.初始化方向请求

// 方向请求

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

// 设置起点

MKPlacemark *sourceMKPm = [[MKPlacemark alloc] initWithPlacemark:sourceCLPm];

request.source = [[MKMapItem alloc] initWithPlacemark:sourceMKPm];

self.sourceMKPm = sourceMKPm;

// 设置终点

MKPlacemark *destinationMKPm = [[MKPlacemark alloc] initWithPlacemark:destinationCLPm];

request.destination = [[MKMapItem alloc] initWithPlacemark:destinationMKPm];

self.destinationMKPm = destinationMKPm;

// 2.根据请求创建方向

MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

// 3.执行请求

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

if (error) return;

for (MKRoute *route in response.routes) {

// 添加路线遮盖(传递路线的遮盖模型数据)

[self.mapView addOverlay:route.polyline];

}

}];

// 遮盖 overlay
}

#pragma mark - 代理方法
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay

{

MKPolylineRenderer *redender = [[MKPolylineRenderer alloc] initWithOverlay:overlay];

redender.lineWidth = 5;

redender.strokeColor = [UIColor blueColor];

return redender;
}

开始导航
- (IBAction)startNavigation {

if (self.sourceMKPm == nil || self.destinationMKPm == nil) return;

// 起点

MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:self.sourceMKPm];

// 终点

MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:self.destinationMKPm];

// 存放起点和终点

NSArray *items = @[sourceItem, destinationItem];

// 参数

NSMutableDictionary *options = [NSMutableDictionary dictionary];

// 导航模式:驾驶导航

options[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;

// 是否要显示路况

options[MKLaunchOptionsShowsTrafficKey] = @YES;

// 打开苹果官方的导航应用

[MKMapItem openMapsWithItems:items launchOptions:options];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: