iOS应用开发-地图定位
来源:互联网 发布:淘宝店铺上架教程视频 编辑:程序博客网 时间:2024/05/17 19:15
iOS开发中经常会涉及到地图开发,比较长见的如打车软件、点评软件、团购软件、旅游软件还有许多社交软件,都会涉及。地图开发主要有以下几个步骤:
导入markit、action包-》生成MKMapView对象-》设置MKMapView代理-》实现代理。
一、显示以某个区域为中心的地图
1、生成MKMapView对象
_mapView = [[MKMapViewalloc] initWithFrame:CGRectMake(0,64, width, height - 64 - 49 - 50)];
2、设置MapView代理方法:
_mapView.delegate =self;
[self.viewaddSubview:_mapView];
3、定义一个坐标
CLLocationCoordinate2D coordinate = {31.19316,121.43154};
4、设置缩放级别
MKCoordinateSpan span = {0.005,0.005};
MKCoordinateRegion region = {coordinate, span};
[_mapView setRegion:region];
二、定位用户位置开发路线:MKMapView设置showsUserLocation=YES可以显示定位=》创建CLLocationManager =》开始定位=》实现定位代理方法。
1.创建CLLocationManager方法
self.locationManager = [[CLLocationManageralloc] init];
2.设置CLLocationManager代理
self.locationManager.delegate =self;
首先需在target info中增加NSLocationWhenInUseUsageDescription 字段,其次请求用户授权
if ([[UIDevicecurrentDevice].systemVersiondoubleValue] >= 8.0) {// 如果系统设备>= 8.0
[self.locationManagerrequestWhenInUseAuthorization] ;
}
然后在授权状态改变的代理方法中,处理授权状态的改变。
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status ==kCLAuthorizationStatusNotDetermined) {
NSLog(@"等待用户授权");
}elseif (status ==kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
4. [self.locationManagerstartUpdatingLocation];
} else {
NSLog(@"授权失败");
}
}
4.开始定为操作
[self.locationManager startUpdatingLocation];
5.实现定位代理方法。定位成功或失败根据代理来设置
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//定位完成后停止定位
[self.locationManagerstopUpdatingLocation];
NSLog(@"%@", error.description);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.locationManagerstopUpdatingLocation];
self.currentLocation = newLocation;
NSString locationStr = [NSStringstringWithFormat:@"(%f,%f)", newLocation.coordinate.latitude,
newLocation.coordinate.longitude];
}
两点距离: CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];通常会出现于 你与那个地方相距多远。
完整代码:
#import <UIKit/UIKit.h>
#import "BLBaseViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationViewController:UIViewController
<CLLocationManagerDelegate,MKMapViewDelegate>
{
MKMapView *_mapView;
UILabel *_locationLabel;
NSMutableArray *_annotations;
}
@property (nonatomic,strong) CLLocationManager *locationManager;
@end
@interface LocationViewController ()
@end
@implementation LocationViewController
- (void)viewDidLoad {
[superviewDidLoad];
CGFloat width =self.view.bounds.size.width;
CGFloat height =self.view.bounds.size.height;
_mapView = [[MKMapViewalloc] initWithFrame:CGRectMake(0,64, width, height - 64 - 49 - 50)];
_mapView.showsUserLocation =YES;
_mapView.delegate =self;
[self.viewaddSubview:_mapView];
CLLocationCoordinate2D coordinate = {31.19316,121.43154};
MKCoordinateSpan span = {0.05,0.05};
MKCoordinateRegion region = {coordinate, span};
[_mapView setRegion:region];
_locationLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,_mapView.frame.origin.y +_mapView.frame.size.height, width,50)];
_locationLabel.textAlignment =NSTextAlignmentCenter;
_locationLabel.backgroundColor = [UIColorcyanColor];
_locationLabel.text =@"user's location.";
[self.viewaddSubview:_locationLabel];
UIBarButtonItem *locationButton
= [[UIBarButtonItemalloc] initWithTitle:@"定位"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(locationButtonClicked:)];
self.navigationItem.leftBarButtonItem = locationButton;
}- (void)locationButtonClicked:(id)sender
{
if (self.locationManager ==nil) {
self.locationManager = [[CLLocationManageralloc] init];
self.locationManager.delegate =self;
}
if ([[UIDevicecurrentDevice].systemVersiondoubleValue] >= 8.0) {// 如果系统设备>= 8.0
[self.locationManagerrequestWhenInUseAuthorization] ;
} else {
// 2. 开始定位
[self.locationManagerstartUpdatingLocation] ;
}
}
#pragma mark - CLLocationManagerDelegate methods
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status ==kCLAuthorizationStatusNotDetermined) {
NSLog(@"等待用户授权");
}elseif (status ==kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManagerstartUpdatingLocation];
} else {
NSLog(@"授权失败");
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[self.locationManagerstopUpdatingLocation];
NSLog(@"%@", error.description);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.locationManagerstopUpdatingLocation];
self.currentLocation = newLocation;
_locationLabel.text = [NSStringstringWithFormat:@"(%f,%f)", newLocation.coordinate.latitude,
newLocation.coordinate.longitude];
}
- iOS应用开发-地图定位
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS使用内置地图,定位应用开发
- iOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- iOS地图和定位的应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- IOS之地图和定位应用开发
- 防盗链的开发
- 移动王国的诱惑【经济学人】
- test4.1
- Android Matrix
- 基于Socket的局域网简易聊天室
- iOS应用开发-地图定位
- BZOJ 1122 [POI2008]账本BBB 神题
- EPEL&&Yum Priorities
- XML文件在TwinCAT里面的作用
- CGS flip 学习总结
- 找出一组数据中多次出现的所有数字(空间要求最低)
- 世界时区表
- wince 内存泄露的解决方法
- 大话移动app测试_阅读