标签:
一、系统自带地图开发
iOS地图定位的方式有三种,分别是蜂窝基站定位、wifi定位、GPS定位
1、CoreLocation
1.1 定义manager管理者 注意这个管理者必须设置成全局变量,不然代理方法不会回调,已经被释放
@property (nonatomic, strong) CLLocationManager *locationManager;
//初始化 self.locationManager = [[CLLocationManager alloc] init];
1.2 设置代理为self,然后遵循代理协议
//设置代理
_locationManager.delegate = self;
1.3 设置属性 :distanceFilter 多久定位一次 desiredAccuracy 设置精确度,越精确越费电
//设置属性
self.locationManager.distanceFilter = 100;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
1.4 开启定位
//开始定位
[self.locationManager startUpdatingLocation];
//停止定位,当不使用定位的时候关闭定位
[self.locationManager stopUpdatingLocation];
1.5 定位成功之后回调方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@", locations);
//取出位置对象
CLLocation *location = locations.firstObject;
//取出经纬度
CLLocationCoordinate2D coordinate = location.coordinate;
//打印经纬度
NSLog(@"%f %f", coordinate.longitude, coordinate.latitude);
}
注意:iOS7之前系统自己会自动请求授权,iOS8及以后需要我们主动请求授权,并且需要再info.plist文件中添加两个字段
NSLocationAlwaysUsageDescription 申请前台和后台定位(时时定位)
NSLocationWhenInUseUsageDescription 申请后台定位 (需要的时候定位)
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
//主动请求授权
[self.locationManager requestAlwaysAuthorization];
}
1.6 地理编码和反地理编码
@property (nonatomic, strong) CLGeocoder *geocoder;
self.geocoder =[ [CLGeocoder alloc] init];
//根据地名返回地理坐标
[self.geocoder geocodeAddressString:@"北京" completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"placeMark - %@", placemarks.firstObject
);
CLPlacemark *place = placemarks.lastObject;
//取出经纬度
CGFloat latitude = place.location.coordinate.latitude;
CGFloat lng = place.location.coordinate.longitude;
//遍历得到的地址
for (CLPlacemark *pl in placemarks) {
NSLog(@"经度%f 纬度%f ", pl.location.coordinate.latitude, pl.location.coordinate.longitude);
}
//遍历打印地址中存放的
//CountryCode CN
// Name Beijing
//State Beijing
[place.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@ %@", key, obj);
}];
}];
//根据地理坐标返回地名
CLLocation *cl1 = [[CLLocation alloc] initWithLatitude:114.0 longitude:45.0];
CLLocation *cl2 = [[CLLocation alloc] initWithLatitude:-235 longitude:22];
[self.geocoder reverseGeocodeLocation:cl1 completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"error %@", error);
return ;
}
CLPlacemark *place = placemarks.firstObject;
NSLog(@"place --- %@", place.name);
}];
2、MapKit
2.1
@property (nonatomic, strong) MKMapView *mapView;
self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//设置属性
_mapView.delegate = self;
_mapView.mapType = MKMapTypeStandard;
//跟踪用户的位置
_mapView.userTrackingMode = MKUserTrackingModeFollow;
[self.view addSubview:_mapView];
2.2 代理方法
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"%f %f", userLocation.location.coordinate.longitude, userLocation.location.coordinate.latitude);
// userLocation.title = @"北京";
// userLocation.subtitle = @"北京是个好地方";
CLLocationCoordinate2D coordinate = userLocation.location.coordinate;
//设置地图的中心到用户的位置
self.mapView.centerCoordinate = coordinate;
//设置地图的缩放范围
MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
[mapView setRegion:region animated:YES];
}
2.3 添加大头针
2.4 导航画线
//导航
NSString *start = @"北京";
NSString *end = @"杭州";
[self.geocoder geocodeAddressString:start completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
return ;
}
CLPlacemark *startPlace = placemarks.firstObject;
[self.geocoder geocodeAddressString:end completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
return ;
}
CLPlacemark *endPlace = placemarks.firstObject;
//导航画线
// [self addLineFrom:startPlace to:endPlace];
//系统自带的导航
// [self statrNavigationWithClplacemark:startPlace endCLPlacemark:endPlace];
}];
/**
* 添加导航的线路
*
* @param fromPm 起始位置
* @param toPm 结束位置
*/
- (void)addLineFrom:(CLPlacemark *)startPlace to:(CLPlacemark *)endPlace
{
//添加两个大头针
MyAnnotation *start = [[MyAnnotation alloc] init];
start.title = startPlace.name;
start.coordinate = startPlace.location.coordinate;
[self.mapView addAnnotation:start];
MyAnnotation *end = [[MyAnnotation alloc] init];
end.title = endPlace.name;
end.coordinate = endPlace.location.coordinate;
[self.mapView addAnnotation:end];
//查找路线
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
//设置起点
MKPlacemark *sourcePlace = [[MKPlacemark alloc] initWithPlacemark:startPlace];
request.source = [[MKMapItem alloc] initWithPlacemark:sourcePlace];
//设置终点
MKPlacemark *destination = [[MKPlacemark alloc] initWithPlacemark:endPlace];
request.destination = [[MKMapItem alloc] initWithPlacemark:destination];
//方向对象
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
//计算路线
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
NSLog(@"总共%ld条路线", response.routes.count);
for (MKRoute *route in response.routes) {
[self.mapView addOverlay:route.polyline];
}
}];
}
#pragma mark - 画线
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
renderer.strokeColor = [UIColor redColor];
return renderer;
}
#pragma mark - 系统自带的导航
- (void)statrNavigationWithClplacemark:(CLPlacemark *)startClplacemark endCLPlacemark:(CLPlacemark *)endPlacemark
{
//创建起点和终点
MKPlacemark *start = [[MKPlacemark alloc] initWithPlacemark:startClplacemark];
MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:start];
MKPlacemark *end = [[MKPlacemark alloc] initWithPlacemark:endPlacemark];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:end];
//设置起点和终点的数组数
NSArray *itemArray = @[startItem, endItem];
//设置导航参数
// 地图显示模式
// md[MKLaunchOptionsMapTypeKey] = @(MKMapTypeHybrid);
// 只要调用MKMapItem的open方法, 就可以打开系统自带的地图APP进行导航
// Items: 告诉系统地图APP要从哪到哪
// launchOptions: 启动系统自带地图APP的附加参数(导航的模式/是否需要先交通状况/地图的模式/..)
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
dic[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;
[MKMapItem openMapsWithItems:itemArray launchOptions:dic];
// CLPlacemark *place = [[CLPlacemark alloc] init];
// place.location
// MKPlacemark *pl = [MKPlacemark alloc] initWithPlacemark:<#(CLPlacemark *)#>
//
//
//
// MKMapItem *s = [MKMapItem alloc] initWithPlacemark:<#(MKPlacemark *)#>
//
//
// NSMutableDictionary *dic = [NSMutableDictionary dictionary ];
// dic[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;
//
//
// [MKMapItem openMapsWithItems:@[s] launchOptions:dic];
}
标签:
原文地址:http://www.cnblogs.com/luckBaby/p/4777583.html