=====================
day 31 地图与定位
=====================
问题:
1. 百度地图如何查看位置径纬度
2.
1.系统地图与定位
LBS location base service 位置基础服务
LBS: 基于位置的服务 Location Based Service
实际应用:大众点评,陌陌,微信,百度地图
定位
步骤
1.导入库CoreLocation.framework
2.#import <CoreLocation/CoreLocation.h>
// CLLocationManagerDelegate是一个定位位置信息代理
// 它可以实时的告诉我们位置发生了变化 位置变化了就会告诉我们
3.CLLocationManager *_gpsManager; // 定位管理器
创建管理器
_gpsManager = [[CLLocationManager alloc] init];
_gpsManager.delegate = self;
//设置定位精度
_gpsManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
//kCLLocationAccuracyBestForNavigation导航高精度
ios8如何定位
1.在info.plist中添加 Privacy - Location Usage Description , NSLocationAlwaysUsageDescription
2.在代码中 [_manager requestAlwaysAuthorization];
//ios8特有,申请用户授权使用地理位置信息
CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version > 8.0) {
[self.locationManager requestAlwaysAuthorization];
}
开启定位
#if Loc
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"gps没有打开");
return;
}
//开始定位
[gpsManager startUpdatingLocation];
#else
#endif
34.77274892, 113.67591140
协议方法
表示gps位置发生变化了 就调用这个函数 这个函数传过来一个位置数组
// 这个位置数组表示里面最新的所有位置
//定位调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"定位失败");
}
===============================
// 百度地址反编码的网址:
http://api.map.baidu.com/geocoder?output=json&location=39.90558,116.35517&key=dc40f705157725fc98f1fee6a15b6e60
@"http://api.map.baidu.com/geocoder?output=json&location=%f,%f&key=dc40f705157725fc98f1fee6a15b6e60"
地理编码把经纬度转化为地址
地理位置:河南省郑州市金水区纬五路21号河南教育学院->经纬度:113.67591140,34.77274892,
//或者系统自带的
//CLGeocoder 返向地理编码,坐标转换为实际的位置
CLLocation *location = [[CLLocation alloc]initWithLatitude:34.77274892 longitude:113.67591140];
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks lastObject];
NSLog(@"%@,%@",placeMark.name,placeMark.country);
}];
二、地图和大头针
1、导入库MapKit.framework xcode5之后系统自动可以关联
2、#import <MapKit/MapKit.h>
3.创建MKMapView
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
//_mapView.mapType = MKMapTypeSatellite; // 卫星地图
// MKMapTypeStandard标准交通地图
#if 0
// 设置地图初始的区域:
MKCoordinateRegion region;
//经纬度
region.center.longitude = 113.678182;
region.center.latitude = 34.792568;
//缩放比例
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
[_mapView setRegion:region];
#else
//设定地图中心点坐标
CLLocationCoordinate2D lc2d = CLLocationCoordinate2DMake(40.035139,116.311655);
//设置缩放系数 (一般设置成0.01-0.05)
MKCoordinateSpan span = MKCoordinateSpanMake(0.01,0.01);
//显示区域的结构体
MKCoordinateRegion region = MKCoordinateRegionMake(lc2d, span);
//让地图基于划定区域显示
[_mapView setRegion:region];
#endif
// 显示自己的位置
_mapView.showsUserLocation = YES;
[self.view addSubview:_mapView];
=====================================
// 经纬度需要通过地图取得当前位置坐标
CLLocation *myselfLocation = _mapView.userLocation.location;
大头针
自定义大头针
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
//遵守协议
@property (nonatomic, assign) double longitude;
@property (nonatomic, assign) double latitude;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *subname;
@end
@implementation MyAnnotation
- (CLLocationCoordinate2D) coordinate {
return CLLocationCoordinate2DMake(self.latitude, self.longitude);
}
- (NSString *) title {
return self.name;
}
- (NSString *) subtitle {
return self.subname;
}
@end
长按手势
//长按手势
UILongPressGestureRecognizer *lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressedMap:)];
[_mapView addGestureRecognizer:lp];
- (void)longPressedMap:(UILongPressGestureRecognizer *)lp{
if (lp.state ==UIGestureRecognizerStateBegan) {
//拿到手势按在地图上的点坐标
CGPoint pt =[lp locationInView:_mapView];
//convertPoint 要转化的点 toCoordinateFromView:点坐标的来源
//返回值为对应的经纬度坐标(重要)
CLLocationCoordinate2D lc2d = [_mapView convertPoint:pt toCoordinateFromView:_mapView];
MyPin *pin = [[MyPin alloc] initWithTitle:@"新添加" subTitle:@"新添加副标题" lc2d:lc2d];
[_mapView addAnnotation:pin];
}
}
//MKAnnotationView 是所有点标注视图的父类(基类)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
//MKPinAnnotationView 大头针头上的视图
static NSString *viewIde = @"map";
//mapView中有对大头针视图的重用机制
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:viewIde];
if (pinView == nil) {
//创建
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewIde];
//定制显示
//canShowCallout 是否显示顶部视图
pinView.canShowCallout = YES;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,30)];
view.backgroundColor = [UIColor purpleColor];
//定制顶部左侧视图的显示
pinView.leftCalloutAccessoryView = view;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoDark];
//右侧视图
pinView.rightCalloutAccessoryView = btn;
}
return pinView;
}
看房
NSString *url = @"http://ikft.house.qq.com/index.php?guid=863723020164102&devua=appkft_720_1280_HaseeHaseeE50S1_1.2.0_Android17&rn=10&order=5&searchtype=normal&devid=863723020164102&page=%d&appname=QQHouse&mod=appkft&act=searchhouse&channel=28&cityid=1";
2.高德地图
先注册一个账号成为开发者
开发者网址http://lbs.amap.com/
http://lbs.amap.com/console/key/
按照开发指南进行
1.有一个 高德开发者账号
2.申请一个密钥key
3.按照开发指南进行

a.下载高德 相关的地图sdk 需要哪些下载哪些 可以下载 地图的demo (会告诉你怎么使用 高德地图里面的函数和功能)

文档介绍:
http://lbs.amap.com/api/ios-sdk/summary/
如果做的是2d 地图下载2d 的sdk MAMapKit.framework
/2d 和3d 只能做一个
3D矢量地图库,解压后得到MAMapKit.framework文件。3D矢量地图效果优,可查看3D楼块,功能全,还支持离线地图,能帮您节省流量。目前暂不支持地图多实例。
• 2D栅格地图库,解压后得到MAMapKit.framework文件。2D栅格地图库体积小,能耗低,支持地图多实例。
• 搜索库,解压后得到AMapSearchKit.framework文件。搜索库功能包含:POI查询、路径规划、地理编码和逆地理编码、公交查询以及输入提示语查询。
b.假设做的是2d地图
* 注册App, 获取AppKey
* 下载SDK
* 解压SDK,把AMap_iOS_API_2DMap_Lib_Vxxx 解压后的MAMapKit.framework 导入项目 [AMap_iOS_API_Guide_Vxxx.zip里面是开发文档]
* 导入AMapKit.framework
* 导入AMap.bundle资源文件
* 导入系统库
* 在AppDelegate中注册Key:
#define kGaoDeAppKey @"b20a0302dc5534feceaa321a22d9f29e"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registGaoDeAppKey];
return YES;
}
- (void)registGaoDeAppKey {
[MAMapServices sharedServices].apiKey = kGaoDeAppKey;
}
* 设置高德地图对象
#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
@interface ViewController () <MAMapViewDelegate>
{
MAMapView *_mapView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createGaoDeMapView];
}
- (void)createGaoDeMapView {
[MAMapServices sharedServices].apiKey = @"b20a0302dc5534feceaa321a22d9f29e";
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
// _mapView.mapType = MAMapTypeSatellite;
_mapView.mapType = MAMapTypeStandard;
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(34.77274892, 113.67591140);
MACoordinateSpan span = MACoordinateSpanMake(0.01, 0.01);
_mapView.region = MACoordinateRegionMake(center, span);
_mapView.delegate = self;
// _mapView.language = MAMapLanguageEn; // MAMapLanguageZhCN
[self.view addSubview:_mapView];
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if (annotationView == nil)
{
annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
}
annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为NO
annotationView.animatesDrop = YES; //设置标注动画显示,默认为NO
annotationView.draggable = YES; //设置标注可以拖动,默认为NO
annotationView.pinColor = MAPinAnnotationColorPurple;
return annotationView;
}
return nil;
}
@end
* 一些功能,要学会看文档
* 搜索功能
解压AMap_iOS_API_Search_Lib_Vxxx.zip,导入AMapSearchKit.framework
#import <AMapSearchKit/AMapSearchKit.h>
遵守协议<AMapSearchDelegate>
AMapSearchAPI *_search;
也可以用cocoapods 自动下载高德地图sdk 自动导入关联的库
4》//导入高德的头文件
#import <MAMapKit/MAMapKit.h>
5》//必须要先申请key 让高德授权
[MAMapServices sharedServices].apiKey = @"12d88055112f529e1731e80f616aea60";
6》创建高德地图视图
3.百度地图
先注册一个账号成为开发者
开发者网址http://developer.baidu.com/
按照开发指南进行
================================
非arc 和arc 的代码进行混编
在非arc 下编译arc的代码 加编译-fobjc-arc
在arc 环境下编译非arc 的代码 加编译参数 -fno-objc-arc
非arc 转arc
步骤:Edit-->Refactor-->Convert to Objective-C ARC...-->Check-->Next-->Save-->(若有提示Continue)
如果不行试试修改下面的配置
在 build setting 搜索 treat waring error 改为 yes
推荐书籍《Objective-C高级编程:iOS与OS X多线程和内存管理》
<ios编程>
day 31 地图与定位
=====================
问题:
1. 百度地图如何查看位置径纬度
2.
1.系统地图与定位
LBS location base service 位置基础服务
LBS: 基于位置的服务 Location Based Service
实际应用:大众点评,陌陌,微信,百度地图
定位
步骤
1.导入库CoreLocation.framework
2.#import <CoreLocation/CoreLocation.h>
// CLLocationManagerDelegate是一个定位位置信息代理
// 它可以实时的告诉我们位置发生了变化 位置变化了就会告诉我们
3.CLLocationManager *_gpsManager; // 定位管理器
创建管理器
_gpsManager = [[CLLocationManager alloc] init];
_gpsManager.delegate = self;
//设置定位精度
_gpsManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
//kCLLocationAccuracyBestForNavigation导航高精度
ios8如何定位
1.在info.plist中添加 Privacy - Location Usage Description , NSLocationAlwaysUsageDescription
2.在代码中 [_manager requestAlwaysAuthorization];
//ios8特有,申请用户授权使用地理位置信息
CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version > 8.0) {
[self.locationManager requestAlwaysAuthorization];
}
开启定位
#if Loc
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"gps没有打开");
return;
}
//开始定位
[gpsManager startUpdatingLocation];
#else
#endif
34.77274892, 113.67591140
协议方法
表示gps位置发生变化了 就调用这个函数 这个函数传过来一个位置数组
// 这个位置数组表示里面最新的所有位置
//定位调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"定位失败");
}
===============================
// 百度地址反编码的网址:
http://api.map.baidu.com/geocoder?output=json&location=39.90558,116.35517&key=dc40f705157725fc98f1fee6a15b6e60
@"http://api.map.baidu.com/geocoder?output=json&location=%f,%f&key=dc40f705157725fc98f1fee6a15b6e60"
地理编码把经纬度转化为地址
地理位置:河南省郑州市金水区纬五路21号河南教育学院->经纬度:113.67591140,34.77274892,
//或者系统自带的
//CLGeocoder 返向地理编码,坐标转换为实际的位置
CLLocation *location = [[CLLocation alloc]initWithLatitude:34.77274892 longitude:113.67591140];
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks lastObject];
NSLog(@"%@,%@",placeMark.name,placeMark.country);
}];
二、地图和大头针
1、导入库MapKit.framework xcode5之后系统自动可以关联
2、#import <MapKit/MapKit.h>
3.创建MKMapView
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
//_mapView.mapType = MKMapTypeSatellite; // 卫星地图
// MKMapTypeStandard标准交通地图
#if 0
// 设置地图初始的区域:
MKCoordinateRegion region;
//经纬度
region.center.longitude = 113.678182;
region.center.latitude = 34.792568;
//缩放比例
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
[_mapView setRegion:region];
#else
//设定地图中心点坐标
CLLocationCoordinate2D lc2d = CLLocationCoordinate2DMake(40.035139,116.311655);
//设置缩放系数 (一般设置成0.01-0.05)
MKCoordinateSpan span = MKCoordinateSpanMake(0.01,0.01);
//显示区域的结构体
MKCoordinateRegion region = MKCoordinateRegionMake(lc2d, span);
//让地图基于划定区域显示
[_mapView setRegion:region];
#endif
// 显示自己的位置
_mapView.showsUserLocation = YES;
[self.view addSubview:_mapView];
=====================================
// 经纬度需要通过地图取得当前位置坐标
CLLocation *myselfLocation = _mapView.userLocation.location;
大头针
自定义大头针
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
//遵守协议
@property (nonatomic, assign) double longitude;
@property (nonatomic, assign) double latitude;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *subname;
@end
@implementation MyAnnotation
- (CLLocationCoordinate2D) coordinate {
return CLLocationCoordinate2DMake(self.latitude, self.longitude);
}
- (NSString *) title {
return self.name;
}
- (NSString *) subtitle {
return self.subname;
}
@end
长按手势
//长按手势
UILongPressGestureRecognizer *lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressedMap:)];
[_mapView addGestureRecognizer:lp];
- (void)longPressedMap:(UILongPressGestureRecognizer *)lp{
if (lp.state ==UIGestureRecognizerStateBegan) {
//拿到手势按在地图上的点坐标
CGPoint pt =[lp locationInView:_mapView];
//convertPoint 要转化的点 toCoordinateFromView:点坐标的来源
//返回值为对应的经纬度坐标(重要)
CLLocationCoordinate2D lc2d = [_mapView convertPoint:pt toCoordinateFromView:_mapView];
MyPin *pin = [[MyPin alloc] initWithTitle:@"新添加" subTitle:@"新添加副标题" lc2d:lc2d];
[_mapView addAnnotation:pin];
}
}
//MKAnnotationView 是所有点标注视图的父类(基类)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
//MKPinAnnotationView 大头针头上的视图
static NSString *viewIde = @"map";
//mapView中有对大头针视图的重用机制
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:viewIde];
if (pinView == nil) {
//创建
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewIde];
//定制显示
//canShowCallout 是否显示顶部视图
pinView.canShowCallout = YES;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,30)];
view.backgroundColor = [UIColor purpleColor];
//定制顶部左侧视图的显示
pinView.leftCalloutAccessoryView = view;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoDark];
//右侧视图
pinView.rightCalloutAccessoryView = btn;
}
return pinView;
}
看房
NSString *url = @"http://ikft.house.qq.com/index.php?guid=863723020164102&devua=appkft_720_1280_HaseeHaseeE50S1_1.2.0_Android17&rn=10&order=5&searchtype=normal&devid=863723020164102&page=%d&appname=QQHouse&mod=appkft&act=searchhouse&channel=28&cityid=1";
2.高德地图
先注册一个账号成为开发者
开发者网址http://lbs.amap.com/
http://lbs.amap.com/console/key/
按照开发指南进行
1.有一个 高德开发者账号
2.申请一个密钥key
3.按照开发指南进行

a.下载高德 相关的地图sdk 需要哪些下载哪些 可以下载 地图的demo (会告诉你怎么使用 高德地图里面的函数和功能)

文档介绍:
http://lbs.amap.com/api/ios-sdk/summary/
如果做的是2d 地图下载2d 的sdk MAMapKit.framework
/2d 和3d 只能做一个
3D矢量地图库,解压后得到MAMapKit.framework文件。3D矢量地图效果优,可查看3D楼块,功能全,还支持离线地图,能帮您节省流量。目前暂不支持地图多实例。
• 2D栅格地图库,解压后得到MAMapKit.framework文件。2D栅格地图库体积小,能耗低,支持地图多实例。
• 搜索库,解压后得到AMapSearchKit.framework文件。搜索库功能包含:POI查询、路径规划、地理编码和逆地理编码、公交查询以及输入提示语查询。
b.假设做的是2d地图
* 注册App, 获取AppKey
* 下载SDK
* 解压SDK,把AMap_iOS_API_2DMap_Lib_Vxxx 解压后的MAMapKit.framework 导入项目 [AMap_iOS_API_Guide_Vxxx.zip里面是开发文档]
* 导入AMapKit.framework
* 导入AMap.bundle资源文件
* 导入系统库
* 在AppDelegate中注册Key:
#define kGaoDeAppKey @"b20a0302dc5534feceaa321a22d9f29e"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registGaoDeAppKey];
return YES;
}
- (void)registGaoDeAppKey {
[MAMapServices sharedServices].apiKey = kGaoDeAppKey;
}
* 设置高德地图对象
#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
@interface ViewController () <MAMapViewDelegate>
{
MAMapView *_mapView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createGaoDeMapView];
}
- (void)createGaoDeMapView {
[MAMapServices sharedServices].apiKey = @"b20a0302dc5534feceaa321a22d9f29e";
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
// _mapView.mapType = MAMapTypeSatellite;
_mapView.mapType = MAMapTypeStandard;
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(34.77274892, 113.67591140);
MACoordinateSpan span = MACoordinateSpanMake(0.01, 0.01);
_mapView.region = MACoordinateRegionMake(center, span);
_mapView.delegate = self;
// _mapView.language = MAMapLanguageEn; // MAMapLanguageZhCN
[self.view addSubview:_mapView];
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if (annotationView == nil)
{
annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
}
annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为NO
annotationView.animatesDrop = YES; //设置标注动画显示,默认为NO
annotationView.draggable = YES; //设置标注可以拖动,默认为NO
annotationView.pinColor = MAPinAnnotationColorPurple;
return annotationView;
}
return nil;
}
@end
* 一些功能,要学会看文档
* 搜索功能
解压AMap_iOS_API_Search_Lib_Vxxx.zip,导入AMapSearchKit.framework
#import <AMapSearchKit/AMapSearchKit.h>
遵守协议<AMapSearchDelegate>
AMapSearchAPI *_search;
也可以用cocoapods 自动下载高德地图sdk 自动导入关联的库
4》//导入高德的头文件
#import <MAMapKit/MAMapKit.h>
5》//必须要先申请key 让高德授权
[MAMapServices sharedServices].apiKey = @"12d88055112f529e1731e80f616aea60";
6》创建高德地图视图
3.百度地图
先注册一个账号成为开发者
开发者网址http://developer.baidu.com/
按照开发指南进行
================================
非arc 和arc 的代码进行混编
在非arc 下编译arc的代码 加编译-fobjc-arc
在arc 环境下编译非arc 的代码 加编译参数 -fno-objc-arc
非arc 转arc
步骤:Edit-->Refactor-->Convert to Objective-C ARC...-->Check-->Next-->Save-->(若有提示Continue)
如果不行试试修改下面的配置
在 build setting 搜索 treat waring error 改为 yes
推荐书籍《Objective-C高级编程:iOS与OS X多线程和内存管理》
<ios编程>