您的位置:首页 > 移动开发 > IOS开发

iOS中的定位

2016-05-16 00:00 453 查看
地理编码 与 反地理编码:

[code=plain]#import "ViewController.h"
//先引入库
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()

//创建一个CLGeocoder
@property (nonatomic, strong) CLGeocoder *geocoder;

#pragma mark - 地理编码
//在界面上创建控件
- (IBAction)geocode;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;

#pragma mark - 反地理编码
//在界面上创建控件
- (IBAction)reverseGeocode;
@property (weak, nonatomic) IBOutlet UITextField *longtitudeField;
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

}

- (CLGeocoder *)geocoder
{
if (_geocoder == nil) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}

#pragma mark -地理编码
//地址 ---> 经纬度
- (void)geocode
{
NSString *address = self.addressField.text;
if (self.addressField.text.length == 0) {

return;
}

[self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

if (error || placemarks.count == 0) {
self.addressField.text = @"您输入的位置存在错误";
} else {
for (CLPlacemark *placemark in placemarks) {
NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode);
}

/*
thoroughfare 街道
locality  城市
subLocality  街道
postalCode  邮编
ISOcountryCode  国家编码
country  国家
*/

//显示最前面的地标
CLPlacemark *placemark = [placemarks firstObject];
self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f",placemark.location.coordinate.longitude];
self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f",placemark.location.coordinate.latitude];
self.detailAddressLabel.text = placemark.name;
}
}];
}

#pragma mark -反地理编码

//经纬度 --> 地名
- (void)reverseGeocode
{
CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.latitudeLabel.text doubleValue] longitude:[self.longitudeLabel.text doubleValue]];

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

if (error || placemarks.count == 0) {
self.reverseDetailAddressLabel.text = @"您输入的经纬度存在错误";
} else {

for (CLPlacemark *placemark in placemarks) {
NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode);
}

CLPlacemark *placemark = [placemarks firstObject];
self.reverseDetailAddressLabel.text =  placemark.name;
self.longtitudeField.text = [NSString stringWithFormat:@"%.2f",placemark.location.coordinate.longitude];
self.latitudeField.text = [NSString stringWithFormat:@"%.2f",placemark.location.coordinate.latitude];

}
}];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}


OC库定位:

[code=plain]#import "ViewController.h"
//引入库
#import "CoreLocation/CoreLocation.h"

@interface ViewController ()<CLLocationManagerDelegate>{

CLLocationManager *manager;

}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

if ([CLLocationManager locationServicesEnabled]) {
//可以定位并开始定位
[self.manager startUpdatingLocation];

//传入一个区域
//[self.manager startMonitoringForRegion:<#(nonnull CLRegion *)#>];

}else{

//提醒用户打开定位开关
//检查网络

}

//计算距离
[self countDistance];

}

//定位的方法
-(CLLocationManager *)manager{

if (manager == nil) {
//创建用户管理器,定位用户的位置
manager = [[CLLocationManager alloc]init];
//判断当前的版本
if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0) {
[manager requestAlwaysAuthorization];
[manager requestWhenInUseAuthorization];
}

manager.delegate = self;

//每隔多少米定位一次
manager.distanceFilter = kCLDistanceFilterNone;

//精确度
manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

}

return manager;
}

//计算距离的方法
-(void)countDistance{

CLLocation *lo1 = [[CLLocation alloc]initWithLatitude:41 longitude:115];
CLLocation *lo2 = [[CLLocation alloc]initWithLatitude:41 longitude:116];

CLLocationDistance distance = [lo1 distanceFromLocation:lo2];

NSLog(@"%f", distance);

}

//定位自己的位置
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

CLLocation *location = [locations lastObject];

//纬度  location.coordinate.latitude
//经度  location.coordinate.longitude

NSLog(@"纬度%f------经度%f",location.coordinate.latitude,location.coordinate.longitude);

//停止更新位置(非常耗电)
//    [manager stopUpdatingLocation];

}

//进入某个区域调用
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region{

}

//离开某个区域调用
- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region{

}


iOS地图(MapKit):

[code=plain]#import "ViewController.h"
//先引入库
#import <MapKit/MapKit.h>
//注意引入代理
@interface ViewController ()<MKMapViewDelegate>
//新建一个CLLocationManager
@property (nonatomic,strong) CLLocationManager *manager;

@end

@implementation ViewController

/**
*  !!!!!!!!!注意:要在 Info.plist 中添加 NSLocationWhenInUseUsageDescription ,并把 Boolean 改成 YES
*/

- (void)viewDidLoad {
[super viewDidLoad];

//判断当前版本(8.0以后使用地图需要先定位)
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0) {
self.manager = [[CLLocationManager alloc]init];
[self.manager requestWhenInUseAuthorization];
[self.manager requestAlwaysAuthorization];
}

//初始化地图
MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];

//设置地图的类型
mapView.mapType = MKMapTypeStandard;

//设置跟踪模式
mapView.userTrackingMode = MKUserTrackingModeFollow;

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

[self.view addSubview:mapView];

}

/**
*  更新到用户的位置时就会调用(显示的位置、显示范围改变)
*  userLocation : 大头针模型数据, 对大头针位置的一个封装(这里的userLocation描述的是用来显示用户位置的蓝色大头针)
*/
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

//地图上的大头针上的简介
userLocation.title = @"哈哈哈哈哈哈";
userLocation.subtitle = @"我是介绍";

}

/**
*  地图显示的区域即将改变了就会调用
*/
-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{

NSLog(@"regionWillChangeAnimated");

}

/**
*  地图显示的区域改变了就会调用(显示的位置、显示范围改变)
*/
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

//中心点
CLLocationCoordinate2D center = mapView.region.center;
//跨度
MKCoordinateSpan span = mapView.region.span;

NSLog(@"中心点=(%f,%f), 区域跨度=(%f,%f)", center.longitude,center.latitude,span.longitudeDelta,span.latitudeDelta);

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