系统地图的用法
2015-03-21 21:23
190 查看
1, 引用框架 CoreGraphics.framework MapKit.framework CoreLocation.framework
2 导入主头文件 iOS5之后不需要手动导入
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
(1)MapKit :用于地图展示
(2)CoreLocation :用于地理定位
CoreLocation框架使用须知
(1) Corelocation框架中所有数据类型的前缀都是CL
(2) Corelocation中使用的CLLocationManager对象来做用户定位
这是初始化
locationManager = [[CLLocationManager alloc] init];//初始化
locationManager.delegate = self;//委托自己
locationManager.desiredAccuracy = kCLLocationAccuracyBest;//精度设定
[locationManager startUpdatingLocation];//开启位置更新
定义经纬坐标
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude=21.238928; 经度
theCoordinate.longitude=113.313353;纬度
//
// ViewController.m
// MapStudy
//
// Created by lanouhn on 15/3/21.
// Copyright (c) 2015年 niutiantian. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Annotation.h"
@interface ViewController ()<MKMapViewDelegate, CLLocationManagerDelegate[CDATA[]]>
{
//用来做用户定位
CLLocationManager *locationManeger;
CLLocation *location;
CLGeocoder *geocoder; //反向地理编码
CLLocationCoordinate2D coordinate; //地理位置坐标
MKMapView *_mapView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//添加地图
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
[self.view addSubview:_mapView];
//定位管理器
locationManeger = [[CLLocationManager alloc] init];
//定位管理器没有打开
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"请打开定位");
return;
}
//请求用户授权
//kCLAuthorizationStatusNotDetermined 未授权
//kCLAuthorizationStatusAuthorizedWhenInUse 授权
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[locationManeger requestWhenInUseAuthorization];
} else if ([CLLocationManager authorizationStatus]== kCLAuthorizationStatusAuthorizedWhenInUse) {
//设置代理
locationManeger.delegate = self;
locationManeger.desiredAccuracy = kCLLocationAccuracyBest;//精度设定
[locationManeger startUpdatingLocation]; //开启位置更新
}
_mapView.userTrackingMode = YES;//用户追踪当前位置
//设置地图类型
_mapView.mapType = MKMapTypeStandard;
//coordinate.latitude coordinate.latitude 经度 纬度
// CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.latitude ];
}
#pragma mark - MKMapViewDelegate
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
location = [locations firstObject];
coordinate = location.coordinate;
//地理位置反编码
geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks firstObject];
NSLog(@"%@", placeMark.addressDictionary);
}];
//添加标记大头针
[self addAnnotation];
}
- (void)addAnnotation {
NSLog(@"%f", coordinate.latitude);
Annotation *annotation1 = [[Annotation alloc] init];
annotation1.title = @"CMJ Studio";
annotation1.subTitle = @"Kenshin Cui's Studios";
annotation1.coordinate = coordinate;
[_mapView addAnnotation:annotation1];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[locationManeger stopUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject<MKAnnotation[CDATA[]]>
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@end
2 导入主头文件 iOS5之后不需要手动导入
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
(1)MapKit :用于地图展示
(2)CoreLocation :用于地理定位
CoreLocation框架使用须知
(1) Corelocation框架中所有数据类型的前缀都是CL
(2) Corelocation中使用的CLLocationManager对象来做用户定位
这是初始化
locationManager = [[CLLocationManager alloc] init];//初始化
locationManager.delegate = self;//委托自己
locationManager.desiredAccuracy = kCLLocationAccuracyBest;//精度设定
[locationManager startUpdatingLocation];//开启位置更新
定义经纬坐标
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude=21.238928; 经度
theCoordinate.longitude=113.313353;纬度
//
// ViewController.m
// MapStudy
//
// Created by lanouhn on 15/3/21.
// Copyright (c) 2015年 niutiantian. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Annotation.h"
@interface ViewController ()<MKMapViewDelegate, CLLocationManagerDelegate[CDATA[]]>
{
//用来做用户定位
CLLocationManager *locationManeger;
CLLocation *location;
CLGeocoder *geocoder; //反向地理编码
CLLocationCoordinate2D coordinate; //地理位置坐标
MKMapView *_mapView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//添加地图
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
[self.view addSubview:_mapView];
//定位管理器
locationManeger = [[CLLocationManager alloc] init];
//定位管理器没有打开
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"请打开定位");
return;
}
//请求用户授权
//kCLAuthorizationStatusNotDetermined 未授权
//kCLAuthorizationStatusAuthorizedWhenInUse 授权
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[locationManeger requestWhenInUseAuthorization];
} else if ([CLLocationManager authorizationStatus]== kCLAuthorizationStatusAuthorizedWhenInUse) {
//设置代理
locationManeger.delegate = self;
locationManeger.desiredAccuracy = kCLLocationAccuracyBest;//精度设定
[locationManeger startUpdatingLocation]; //开启位置更新
}
_mapView.userTrackingMode = YES;//用户追踪当前位置
//设置地图类型
_mapView.mapType = MKMapTypeStandard;
//coordinate.latitude coordinate.latitude 经度 纬度
// CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.latitude ];
}
#pragma mark - MKMapViewDelegate
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
location = [locations firstObject];
coordinate = location.coordinate;
//地理位置反编码
geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks firstObject];
NSLog(@"%@", placeMark.addressDictionary);
}];
//添加标记大头针
[self addAnnotation];
}
- (void)addAnnotation {
NSLog(@"%f", coordinate.latitude);
Annotation *annotation1 = [[Annotation alloc] init];
annotation1.title = @"CMJ Studio";
annotation1.subTitle = @"Kenshin Cui's Studios";
annotation1.coordinate = coordinate;
[_mapView addAnnotation:annotation1];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[locationManeger stopUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject<MKAnnotation[CDATA[]]>
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@end
相关文章推荐
- 系统表收集,及用法收集
- 8月30日图雄网站隆重推出三维地图服务系统!!!
- 电子地图系统说明(目录)
- HWND_BROADCAST的一个用法——修改环境变量,立即通知系统
- 转】基于GeoServer的电子地图系统说明(四):空间数据库实现
- 【转】基于GeoServer的电子地图系统说明(一):目录
- 一些有用的系统存储过程及用法
- 51地图用法
- VC制作地图及公交查询系统
- 电子地图查询系统_v1.0_源代码(VB6+MAPX5)
- SQL数据库的系统数据类型table的用法举例
- 基于AJAX技术的WebGis系统实践 (3. 三维城市地图系统实体建设)
- MS SQL入门基础:系统表及其用法
- 一个游戏地图系统的实现设想
- 系统函数SHFileOperation的用法
- 基于AJAX技术的WebGis系统实践 (3. 三维城市地图系统实体建设)
- 关于几款系统恢复常用工具的用法介绍
- linux系统find命令一些用法,没有xargs的
- 【转】基于GeoServer的电子地图系统说明(三):PostgreSQL/PostGIS安装
- 【转】基于GeoServer的电子地图系统说明(五):基于GeoServer的WebGIS的实现